diff options
author | Christophe Grand <christophe@cgrand.net> | 2009-08-05 12:24:00 +0200 |
---|---|---|
committer | Rich Hickey <richhickey@gmail.com> | 2009-08-05 11:05:15 -0400 |
commit | df15b8b3a5878ae089f7355dba2a3251a556322e (patch) | |
tree | 7617cea27a79fd6d69f9a387e8294e0e2c0a50de /src | |
parent | 618c3c264a0f6b89c039162ee946887107f91efd (diff) |
extracted ATransientMap from TransientHashMap
Signed-off-by: Rich Hickey <richhickey@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/jvm/clojure/lang/ATransientMap.java | 46 | ||||
-rw-r--r-- | src/jvm/clojure/lang/PersistentHashMap.java | 39 |
2 files changed, 47 insertions, 38 deletions
diff --git a/src/jvm/clojure/lang/ATransientMap.java b/src/jvm/clojure/lang/ATransientMap.java new file mode 100644 index 00000000..334168f8 --- /dev/null +++ b/src/jvm/clojure/lang/ATransientMap.java @@ -0,0 +1,46 @@ +/** + * Copyright (c) Rich Hickey. All rights reserved. + * The use and distribution terms for this software are covered by the + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) + * which can be found in the file epl-v10.html at the root of this distribution. + * By using this software in any fashion, you are agreeing to be bound by + * the terms of this license. + * You must not remove this notice, or any other, from this software. + **/ + +package clojure.lang; + +import java.util.Map; + +public abstract class ATransientMap extends AFn implements ITransientMap { + abstract void ensureEditable(); + + public ITransientMap conj(Object o) { + ensureEditable(); + 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)); + } + + ITransientMap 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; + } + + public Object valAt(Object key) { + return valAt(key, null); + } +} diff --git a/src/jvm/clojure/lang/PersistentHashMap.java b/src/jvm/clojure/lang/PersistentHashMap.java index 83f7b6f1..bcad8be0 100644 --- a/src/jvm/clojure/lang/PersistentHashMap.java +++ b/src/jvm/clojure/lang/PersistentHashMap.java @@ -195,7 +195,7 @@ public TransientHashMap asTransient() { return new TransientHashMap(this); } -static final class TransientHashMap extends AFn implements ITransientMap { +static final class TransientHashMap extends ATransientMap { AtomicReference<Thread> edit; INode root; int count; @@ -227,31 +227,6 @@ static final class TransientHashMap extends AFn implements ITransientMap { return this; } - public ITransientMap conj(Object o) { - ensureEditable(); - 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)); - } - - ITransientMap 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; - } - public IPersistentMap persistent() { ensureEditable(); edit.set(null); @@ -262,10 +237,6 @@ static final class TransientHashMap extends AFn implements ITransientMap { return root.find(Util.hash(key), key); } - public Object valAt(Object key) { - return valAt(key, null); - } - public Object valAt(Object key, Object notFound) { ensureEditable(); IMapEntry e = entryAt(key); @@ -274,14 +245,6 @@ static final class TransientHashMap extends AFn implements ITransientMap { return notFound; } - public Object invoke(Object arg1) throws Exception{ - return valAt(arg1); - } - - public Object invoke(Object arg1, Object notFound) throws Exception{ - return valAt(arg1, notFound); - } - public int count() { ensureEditable(); return count; |