Move sleep skip to utils class
This commit is contained in:
parent
499413f09a
commit
72ae4c0136
@ -28,7 +28,6 @@ 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 fast = Boolean.parseBoolean(System.getProperty("fast"));
|
|
||||||
private final int numpadSize;
|
private final int numpadSize;
|
||||||
private final Integer[] inputBuffer;
|
private final Integer[] inputBuffer;
|
||||||
private final Integer[] correctPasscode;
|
private final Integer[] correctPasscode;
|
||||||
@ -66,7 +65,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) {
|
||||||
if (!fast) ensureSleep(1000);
|
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);
|
||||||
|
@ -1,12 +1,24 @@
|
|||||||
package com.cleverthis.interview.padlock;
|
package com.cleverthis.interview.padlock;
|
||||||
|
|
||||||
class Utils {
|
class Utils {
|
||||||
|
/**
|
||||||
|
* Check if the sleep is disabled.
|
||||||
|
* User can use `-Dfast=true` in the jvm args,
|
||||||
|
* or change it on the fly.
|
||||||
|
* Might waste sometime on checking this flag, but the effect should be minor.
|
||||||
|
* */
|
||||||
|
private static boolean shouldSkipSleep() {
|
||||||
|
return Boolean.parseBoolean(System.getProperty("fast"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Ensure we will wait a given amount of time even if there are interruptions.
|
* Ensure we will wait a given amount of time even if there are interruptions.
|
||||||
|
* Property `-Dfast=true` can disable the sleep.
|
||||||
*
|
*
|
||||||
* @param millis The time you want to sleep, measure in millisecond.
|
* @param millis The time you want to sleep, measure in millisecond.
|
||||||
*/
|
*/
|
||||||
public static void ensureSleep(long millis) {
|
public static void ensureSleep(long millis) {
|
||||||
|
if (shouldSkipSleep()) return;
|
||||||
long endTime = System.currentTimeMillis() + millis;
|
long endTime = System.currentTimeMillis() + millis;
|
||||||
while (endTime > System.currentTimeMillis()) {
|
while (endTime > System.currentTimeMillis()) {
|
||||||
try {
|
try {
|
||||||
|
Loading…
Reference in New Issue
Block a user