Skip to content

Instantly share code, notes, and snippets.

@kriszyp
Created March 3, 2010 00:39
Show Gist options
  • Save kriszyp/320167 to your computer and use it in GitHub Desktop.
Save kriszyp/320167 to your computer and use it in GitHub Desktop.
public static void addListener(PropertyChangeSetListener listener) {
Map<ObjectId, Set<String>> readSet = readSets.get();
Map<ObjectId, Set<String>> watchSet = watchSets.get(listener);
if (watchSet == null){
synchronized(watchSets){
watchSets.put(listener,watchSet = new HashMap<ObjectId, Set<String>>());
}
}
for (Map.Entry<ObjectId, Set<String>> entry : readSet.entrySet()) {
ObjectId key = entry.getKey();
Set<String> value = entry.getValue();
if (watchSet.containsKey(key) && value != FullSet.instance)
watchSet.get(key).addAll(value);
else{
if(watchSet.containsKey(key) && key instanceof Query && ((Query)key).conditionFunction != null){
ObjectId oldQuery = null;
// find the old query (and it's conditionFunction)
for(Map.Entry<ObjectId, Set<String>> watchEntry : watchSet.entrySet()){
oldQuery = watchEntry.getKey();
if(oldQuery.equals(key)){
break;
}
}
final Function newFunction = ((Query)key).conditionFunction;
final Function oldFunction = ((Query)oldQuery).conditionFunction;
// create a new function that combines the other two
((Query)key).conditionFunction = new BaseFunction(){
@Override
public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
return ScriptRuntime.toBoolean(newFunction.call(cx, scope, thisObj, args)) ||
ScriptRuntime.toBoolean(oldFunction.call(cx, scope, thisObj, args));
}
};
}
watchSet.put(key,value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment