Example #1
0
 public void notifyWatches(Object oldval, Object newval) {
   IPersistentMap ws = watches;
   if (ws.count() > 0) {
     for (ISeq s = ws.seq(); s != null; s = s.next()) {
       Map.Entry e = (Map.Entry) s.first();
       IFn fn = (IFn) e.getValue();
       if (fn != null) fn.invoke(e.getKey(), this, oldval, newval);
     }
   }
 }
Example #2
0
  public static boolean mapEquals(IPersistentMap m1, Object obj) {
    if (m1 == obj) return true;
    if (!(obj instanceof Map)) return false;
    Map m = (Map) obj;

    if (m.size() != m1.count()) return false;

    for (ISeq s = m1.seq(); s != null; s = s.next()) {
      Map.Entry e = (Map.Entry) s.first();
      boolean found = m.containsKey(e.getKey());

      if (!found || !Util.equals(e.getValue(), m.get(e.getKey()))) return false;
    }

    return true;
  }
Example #3
0
 public static int mapHash(IPersistentMap m) {
   int hash = 0;
   for (ISeq s = m.seq(); s != null; s = s.next()) {
     Map.Entry e = (Map.Entry) s.first();
     hash +=
         (e.getKey() == null ? 0 : e.getKey().hashCode())
             ^ (e.getValue() == null ? 0 : e.getValue().hashCode());
   }
   return hash;
 }
Example #4
0
  public IPersistentCollection cons(Object o) {
    if (o instanceof Map.Entry) {
      Map.Entry e = (Map.Entry) o;

      return assoc(e.getKey(), e.getValue());
    } else if (o instanceof IPersistentVector) {
      IPersistentVector v = (IPersistentVector) o;
      if (v.count() != 2)
        throw new IllegalArgumentException("Vector arg to map conj must be a pair");
      return assoc(v.nth(0), v.nth(1));
    }

    IPersistentMap ret = this;
    for (ISeq es = RT.seq(o); es != null; es = es.next()) {
      Map.Entry e = (Map.Entry) es.first();
      ret = ret.assoc(e.getKey(), e.getValue());
    }
    return ret;
  }
Example #5
0
 public synchronized IRef removeWatch(Object key) {
   watches = watches.without(key);
   return this;
 }
Example #6
0
 public synchronized IRef addWatch(Object key, IFn callback) {
   watches = watches.assoc(key, callback);
   return this;
 }