@@ -0,0 +1,1593 @@
+/*%0A * To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.%0A */%0Apackage doublex.lib.locks;%0A%0Aimport java.util.AbstractMap;%0Aimport java.util.LinkedHashMap;%0A%0A/**%0A *%0A * @author kenneth.lau%0A * @param %3CK%3E%0A */%0Apublic final class Locks%3CK%3E %7B%0A%0A    private final ILockable%3CK%3E NULL_LOCK = new NullLock%3C%3E();%0A%0A    private final AbstractMap%3CString, ILockable%3CK%3E%3E mLocks = %0A            new LinkedHashMap%3C%3E();%0A%0A    public final ILockable%3CK%3E lock(final String key) %7B%0A        return mLocks.containsKey(key) ? mLocks.get(key) : NULL_LOCK;%0A    %7D%0A%0A    public final boolean isLocked() %7B%0A        return mLocks.values().stream().anyMatch((lock) -%3E (lock.isLocked()));%0A    %7D%0A%0A    public final void tryPutLock(final String key, final ILockable%3CK%3E lock) %7B%0A        if (canPutLock(key, lock)) putLock(key, lock);%0A    %7D%0A%0A    public final ILockable%3CK%3E triedTakenLock(final String key) %7B%0A        return (canTakeLock(key)) ? takenLock(key) : NULL_LOCK;%0A    %7D%0A%0A    private boolean canPutLock(final String key, final ILockable%3CK%3E lock) %7B%0A        return !mLocks.containsKey(key) && !lock.isLocked();%0A    %7D%0A%0A    private void putLock(final String key, final ILockable%3CK%3E lock) %7B%0A        mLocks.put(key, lock);%0A    %7D%0A%0A    private boolean canTakeLock(final String key) %7B%0A        return mLocks.containsKey(key) && !mLocks.get(key).isLocked();%0A    %7D%0A%0A    private ILockable%3CK%3E takenLock(final String key) %7B%0A        final ILockable%3CK%3E lock = mLocks.get(key);%0A        mLocks.remove(key);%0A        return lock;%0A    %7D%0A%0A%7D%0A

Create Locks.java