From f005187a72affc9de013d8b3d2b3f4d1e391659e Mon Sep 17 00:00:00 2001 From: Rui Hu Date: Wed, 13 Mar 2024 17:34:11 +0800 Subject: [PATCH] Add option to fast forward sleeping --- build.gradle.kts | 1 + padlock-impl/build.gradle.kts | 1 + .../cleverthis/interview/padlock/PadlockImpl.java | 15 ++------------- .../cleverthis/interview/padlock/PadlockTest.java | 6 +++--- 4 files changed, 7 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index f7a6c34..818ae5b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -27,4 +27,5 @@ java { tasks.test { useJUnitPlatform() + jvmArgs = listOf("-Dfast=true") } \ No newline at end of file diff --git a/padlock-impl/build.gradle.kts b/padlock-impl/build.gradle.kts index 946892a..66d7554 100644 --- a/padlock-impl/build.gradle.kts +++ b/padlock-impl/build.gradle.kts @@ -21,4 +21,5 @@ java { tasks.test { useJUnitPlatform() + jvmArgs = listOf("-Dfast=true") } \ No newline at end of file diff --git a/padlock-impl/src/main/java/com/cleverthis/interview/padlock/PadlockImpl.java b/padlock-impl/src/main/java/com/cleverthis/interview/padlock/PadlockImpl.java index 30614c2..3495967 100644 --- a/padlock-impl/src/main/java/com/cleverthis/interview/padlock/PadlockImpl.java +++ b/padlock-impl/src/main/java/com/cleverthis/interview/padlock/PadlockImpl.java @@ -27,7 +27,7 @@ import static com.cleverthis.interview.padlock.Utils.ensureSleep; * After create, the input buffer is empty, you have to initialize. */ public class PadlockImpl { - private final boolean debug; + private final boolean fast = Boolean.parseBoolean(System.getProperty("fast")); private final int numpadSize; private final Integer[] inputBuffer; private final Integer[] correctPasscode; @@ -38,17 +38,6 @@ public class PadlockImpl { * @param numpadSize The number of buttons on the numpad of this lock. */ public PadlockImpl(int numpadSize) { - this(numpadSize, false); - } - - /** - * Create a padlock instance. - * - * @param numpadSize The number of buttons on the numpad of this lock. - * @param debug Will skip sleep if is true - */ - PadlockImpl(int numpadSize, boolean debug) { - this.debug = debug; if (numpadSize < 1) throw new IllegalArgumentException("numpadSize must be a positive number"); this.numpadSize = numpadSize; this.inputBuffer = new Integer[numpadSize]; @@ -73,7 +62,7 @@ public class PadlockImpl { * @return The old value, null if not initialized. */ public synchronized Integer writeInputBuffer(int address, int keyIndex) { - if (!debug) ensureSleep(1000); + if (!fast) ensureSleep(1000); if (keyIndex < 0 || keyIndex >= numpadSize) throw new IllegalArgumentException( "keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex); diff --git a/padlock-impl/src/test/java/com/cleverthis/interview/padlock/PadlockTest.java b/padlock-impl/src/test/java/com/cleverthis/interview/padlock/PadlockTest.java index b935727..5902d43 100644 --- a/padlock-impl/src/test/java/com/cleverthis/interview/padlock/PadlockTest.java +++ b/padlock-impl/src/test/java/com/cleverthis/interview/padlock/PadlockTest.java @@ -22,7 +22,7 @@ class PadlockTest { @Test void testInstantiationRest() { - PadlockImpl padlock = new PadlockImpl(5, true); + PadlockImpl padlock = new PadlockImpl(5); for (int i = 0; i < 5; i++) { // ensure input buffer is uninitialized // should return null when first set @@ -34,7 +34,7 @@ class PadlockTest { @Test void testRejectInvalidInput() { - PadlockImpl padlock = new PadlockImpl(5, true); + PadlockImpl padlock = new PadlockImpl(5); for (int i = 0; i < 3; i++) { padlock.writeInputBuffer(i, i); } @@ -54,7 +54,7 @@ class PadlockTest { @Test void testRejectInvalidInputBufferAddressAndValue() { - PadlockImpl padlock = new PadlockImpl(5, true); + PadlockImpl padlock = new PadlockImpl(5); // test address assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-1, 1)); assertThrows(ArrayIndexOutOfBoundsException.class, () -> padlock.writeInputBuffer(-10, 1));