rlm@10
|
1 /**
|
rlm@10
|
2 * Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
3 * The use and distribution terms for this software are covered by the
|
rlm@10
|
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
5 * which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
6 * By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
7 * the terms of this license.
|
rlm@10
|
8 * You must not remove this notice, or any other, from this software.
|
rlm@10
|
9 **/
|
rlm@10
|
10
|
rlm@10
|
11 package clojure.lang;
|
rlm@10
|
12
|
rlm@10
|
13 import java.util.Map;
|
rlm@10
|
14
|
rlm@10
|
15 import clojure.lang.PersistentHashMap.INode;
|
rlm@10
|
16
|
rlm@10
|
17 abstract class ATransientMap extends AFn implements ITransientMap {
|
rlm@10
|
18 abstract void ensureEditable();
|
rlm@10
|
19 abstract ITransientMap doAssoc(Object key, Object val);
|
rlm@10
|
20 abstract ITransientMap doWithout(Object key);
|
rlm@10
|
21 abstract Object doValAt(Object key, Object notFound);
|
rlm@10
|
22 abstract int doCount();
|
rlm@10
|
23 abstract IPersistentMap doPersistent();
|
rlm@10
|
24
|
rlm@10
|
25 public ITransientMap conj(Object o) {
|
rlm@10
|
26 ensureEditable();
|
rlm@10
|
27 if(o instanceof Map.Entry)
|
rlm@10
|
28 {
|
rlm@10
|
29 Map.Entry e = (Map.Entry) o;
|
rlm@10
|
30
|
rlm@10
|
31 return assoc(e.getKey(), e.getValue());
|
rlm@10
|
32 }
|
rlm@10
|
33 else if(o instanceof IPersistentVector)
|
rlm@10
|
34 {
|
rlm@10
|
35 IPersistentVector v = (IPersistentVector) o;
|
rlm@10
|
36 if(v.count() != 2)
|
rlm@10
|
37 throw new IllegalArgumentException("Vector arg to map conj must be a pair");
|
rlm@10
|
38 return assoc(v.nth(0), v.nth(1));
|
rlm@10
|
39 }
|
rlm@10
|
40
|
rlm@10
|
41 ITransientMap ret = this;
|
rlm@10
|
42 for(ISeq es = RT.seq(o); es != null; es = es.next())
|
rlm@10
|
43 {
|
rlm@10
|
44 Map.Entry e = (Map.Entry) es.first();
|
rlm@10
|
45 ret = ret.assoc(e.getKey(), e.getValue());
|
rlm@10
|
46 }
|
rlm@10
|
47 return ret;
|
rlm@10
|
48 }
|
rlm@10
|
49
|
rlm@10
|
50 public final Object invoke(Object arg1) throws Exception{
|
rlm@10
|
51 return valAt(arg1);
|
rlm@10
|
52 }
|
rlm@10
|
53
|
rlm@10
|
54 public final Object invoke(Object arg1, Object notFound) throws Exception{
|
rlm@10
|
55 return valAt(arg1, notFound);
|
rlm@10
|
56 }
|
rlm@10
|
57
|
rlm@10
|
58 public final Object valAt(Object key) {
|
rlm@10
|
59 return valAt(key, null);
|
rlm@10
|
60 }
|
rlm@10
|
61
|
rlm@10
|
62 public final ITransientMap assoc(Object key, Object val) {
|
rlm@10
|
63 ensureEditable();
|
rlm@10
|
64 return doAssoc(key, val);
|
rlm@10
|
65 }
|
rlm@10
|
66
|
rlm@10
|
67 public final ITransientMap without(Object key) {
|
rlm@10
|
68 ensureEditable();
|
rlm@10
|
69 return doWithout(key);
|
rlm@10
|
70 }
|
rlm@10
|
71
|
rlm@10
|
72 public final IPersistentMap persistent() {
|
rlm@10
|
73 ensureEditable();
|
rlm@10
|
74 return doPersistent();
|
rlm@10
|
75 }
|
rlm@10
|
76
|
rlm@10
|
77 public final Object valAt(Object key, Object notFound) {
|
rlm@10
|
78 ensureEditable();
|
rlm@10
|
79 return doValAt(key, notFound);
|
rlm@10
|
80 }
|
rlm@10
|
81
|
rlm@10
|
82 public final int count() {
|
rlm@10
|
83 ensureEditable();
|
rlm@10
|
84 return doCount();
|
rlm@10
|
85 }
|
rlm@10
|
86 }
|