From 499413f09a985bcf1d0a57c6a2908c37920d57c3 Mon Sep 17 00:00:00 2001 From: Rui Hu Date: Thu, 14 Mar 2024 10:52:13 +0800 Subject: [PATCH] Add performance counter --- .../interview/padlock/PadlockImpl.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) 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 3495967..f9ccaea 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 @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicLong; import static com.cleverthis.interview.padlock.Utils.ensureSleep; @@ -31,6 +32,9 @@ public class PadlockImpl { private final int numpadSize; private final Integer[] inputBuffer; private final Integer[] correctPasscode; + // performance counter + private final AtomicLong writeCounter = new AtomicLong(0); + private final AtomicLong checkCounter = new AtomicLong(0); /** * Create a padlock instance. @@ -66,6 +70,7 @@ public class PadlockImpl { if (keyIndex < 0 || keyIndex >= numpadSize) throw new IllegalArgumentException( "keyIndex out of range. Keypad size: " + numpadSize + ", keyIndex: " + keyIndex); + writeCounter.incrementAndGet(); Integer oldValue = inputBuffer[address]; inputBuffer[address] = keyIndex; return oldValue; @@ -87,10 +92,24 @@ public class PadlockImpl { "Passcode invalid: contain duplicated value. " + Arrays.toString(inputBuffer)); uniqueTestArr[i] = true; } + checkCounter.incrementAndGet(); // if no exception, means: // every digit is unique, and every digit is initialized // aka this is a valid code // now compare with our answer return Arrays.equals(correctPasscode, inputBuffer); } -} \ No newline at end of file + + public long getWriteCounter() { + return writeCounter.get(); + } + + public long getCheckCounter() { + return checkCounter.get(); + } + + public void resetCounter() { + writeCounter.set(0); + checkCounter.set(0); + } +}