/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package doublex.lib.locks;

/**
 *
 * @author DoubleX
 * @param <K>
 */
public final class CountedLock<K> implements ICountable, ILockable<K> {

    private final int mMaxKeyMismatchCount;
    private final ILockable<K> mLock;

    private int mKeyMismatchCount = 0;

    public CountedLock(final int maxKeyMismatchCount, final ILockable<K> lock) {
        mMaxKeyMismatchCount = maxKeyMismatchCount;
        mLock = lock;
    }

    @Override
    public final boolean isLocked() {
        return mLock.isLocked();
    }

    @Override
    public final void lock() {
        mLock.lock();
    }

    @Override
    public final void tryUnlock(final K key) {
        if (isMaxKeyMismatchCountReached()) return;
        mLock.tryUnlock(key);
        if (isLocked()) addKeyMismatchCount();
    }

    @Override
    public final void tryResetCount() {
        if (!isLocked()) resetKeyMismatchCount();
    }

    @Override
    public final int count() {
        return mKeyMismatchCount;
    }

    private boolean isMaxKeyMismatchCountReached() {
        return count() >= mMaxKeyMismatchCount;
    }

    private void addKeyMismatchCount() {
        mKeyMismatchCount++;
    }

    private void resetKeyMismatchCount() {
        mKeyMismatchCount = 0;
    }

}

@@ -327,52 +327,8 @@
  %7B%0A%0A
-    private final int mMaxKeyMismatchCount;%0A
     
@@ -395,20 +395,54 @@
 tchCount
- = 0
+;%0A    private int mMaxKeyMismatchCount
 ;%0A%0A    p
@@ -513,24 +513,57 @@
 %3CK%3E lock) %7B%0A
+        resetKeyMismatchCount();%0A
         mMax
@@ -656,22 +656,16 @@
  public 
-final 
 boolean 
@@ -738,30 +738,24 @@
 %0A    public 
-final 
 void lock() 
@@ -802,38 +802,32 @@
 ride%0A    public 
-final 
 void tryUnlock(f
@@ -854,16 +854,21 @@
   if (is
+Reach
 MaxKeyMi
@@ -882,26 +882,43 @@
 ount
-Reached()) return;
+()) %7B%0A            return;%0A        %7D
 %0A   
@@ -967,16 +967,30 @@
 ocked())
+ %7B%0A           
  addKeyM
@@ -997,32 +997,42 @@
 ismatchCount();%0A
+        %7D%0A
     %7D%0A%0A    @Over
@@ -1051,33 +1051,129 @@
 lic 
-f
 in
-al void tryReset
+t count() %7B%0A        return mKeyMismatchCount;%0A    %7D%0A%0A    @Override%0A    public void tryChangeMaxCount(final int max
 Count
-(
 ) %7B%0A
@@ -1188,45 +1188,97 @@
 if (
-!isLocked()) resetKeyMismatchCount();
+canChangeMaxCount(maxCount)) %7B%0A            changeMaxKeyMismatchCount(maxCount);%0A        %7D
 %0A   
@@ -1310,19 +1310,22 @@
 lic 
-final int c
+void tryResetC
 ount
@@ -1333,32 +1333,60 @@
 ) %7B%0A        
-return m
+if (!isLocked()) %7B%0A            reset
 KeyMismatchC
@@ -1381,33 +1381,45 @@
 KeyMismatchCount
-;
+();%0A        %7D
 %0A    %7D%0A%0A    priv
@@ -1432,16 +1432,21 @@
 olean is
+Reach
 MaxKeyMi
@@ -1460,15 +1460,8 @@
 ount
-Reached
 () %7B
@@ -1585,32 +1585,393 @@
 Count++;%0A    %7D%0A%0A
+    private boolean canChangeMaxCount(final int maxCount) %7B%0A        return !isLocked() && isExceedCurKeyMismatchCount(maxCount);%0A    %7D%0A%0A    private boolean isExceedCurKeyMismatchCount(final int maxCount) %7B%0A        return maxCount %3E count();%0A    %7D%0A%0A    private void changeMaxKeyMismatchCount(final int maxCount) %7B%0A        mMaxKeyMismatchCount = maxCount;%0A    %7D%0A%0A
     private void

Update CountedLock.java