co.easimart.LockSet Maven / Gradle / Ivy
package co.easimart;
import java.util.Collection;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.locks.Lock;
/** package */ class LockSet {
private static WeakHashMap stableIds = new WeakHashMap();
private static long nextStableId = 0L;
private final Set locks;
public LockSet(Collection locks) {
this.locks = new TreeSet(new Comparator() {
@Override
public int compare(Lock lhs, Lock rhs) {
Long lhsId = getStableId(lhs);
Long rhsId = getStableId(rhs);
return lhsId.compareTo(rhsId);
}
});
this.locks.addAll(locks);
}
private static Long getStableId(Lock lock) {
synchronized (stableIds) {
if (stableIds.containsKey(lock)) {
return stableIds.get(lock);
}
long id = nextStableId++;
stableIds.put(lock, id);
return id;
}
}
public void lock() {
for (Lock l : locks) {
l.lock();
}
}
public void unlock() {
for (Lock l : locks) {
l.unlock();
}
}
}