Add debug flag, fix broken gradle config

This commit is contained in:
Rui Hu 2024-03-12 17:18:16 +08:00
parent 64fe5e4adb
commit 7739296926
2 changed files with 18 additions and 1 deletions

View File

@ -14,6 +14,11 @@ dependencies {
testImplementation("org.junit.jupiter:junit-jupiter") testImplementation("org.junit.jupiter:junit-jupiter")
} }
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.test { tasks.test {
useJUnitPlatform() useJUnitPlatform()
} }

View File

@ -27,6 +27,7 @@ import static com.cleverthis.interview.padlock.Utils.ensureSleep;
* After create, the input buffer is empty, you have to initialize. * After create, the input buffer is empty, you have to initialize.
*/ */
public class PadlockImpl { public class PadlockImpl {
private final boolean debug;
private final int numpadSize; private final int numpadSize;
private final Integer[] inputBuffer; private final Integer[] inputBuffer;
private final Integer[] correctPasscode; private final Integer[] correctPasscode;
@ -37,6 +38,17 @@ public class PadlockImpl {
* @param numpadSize The number of buttons on the numpad of this lock. * @param numpadSize The number of buttons on the numpad of this lock.
*/ */
public PadlockImpl(int numpadSize) { 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"); if (numpadSize < 1) throw new IllegalArgumentException("numpadSize must be a positive number");
this.numpadSize = numpadSize; this.numpadSize = numpadSize;
this.inputBuffer = new Integer[numpadSize]; this.inputBuffer = new Integer[numpadSize];
@ -59,7 +71,7 @@ public class PadlockImpl {
* @return The old value, null if not initialized. * @return The old value, null if not initialized.
*/ */
public synchronized Integer writeInputBuffer(int address, int keyIndex) { public synchronized Integer writeInputBuffer(int address, int keyIndex) {
ensureSleep(1000); if (!debug) ensureSleep(1000);
if (keyIndex < 0 || keyIndex >= numpadSize) if (keyIndex < 0 || keyIndex >= numpadSize)
throw new IllegalArgumentException( throw new IllegalArgumentException(
"keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex); "keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex);