view src/clojure/lang/ATransientMap.java @ 10:ef7dbbd6452c

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