com.harium.etyl.util.concurrency.ConcurrentSet Maven / Gradle / Ivy
package com.harium.etyl.util.concurrency;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class ConcurrentSet {
private Set set;
private Map itemActions;
private boolean locked = false;
public ConcurrentSet() {
super();
set = new HashSet();
itemActions = new HashMap();
}
public void add(T t) {
if(!locked) {
set.add(t);
} else {
itemActions.put(t, ItemAction.ADD_ITEM);
}
}
public void remove(T t) {
if(!locked) {
set.remove(t);
} else {
itemActions.put(t, ItemAction.REMOVE_ITEM);
}
}
public Set lock() {
locked = true;
return set;
}
public void unlock() {
locked = false;
if(!itemActions.isEmpty()) {
for(Entry entry: itemActions.entrySet()) {
ItemAction action = entry.getValue();
if(ItemAction.ADD_ITEM.equals(action)) {
set.add(entry.getKey());
} else if(ItemAction.REMOVE_ITEM.equals(action)) {
set.remove(entry.getKey());
}
}
itemActions.clear();
}
}
public Set getSet() {
return set;
}
}