Mercurial > lasercutter
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/ATransientMap.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,86 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +package clojure.lang; 1.15 + 1.16 +import java.util.Map; 1.17 + 1.18 +import clojure.lang.PersistentHashMap.INode; 1.19 + 1.20 +abstract class ATransientMap extends AFn implements ITransientMap { 1.21 + abstract void ensureEditable(); 1.22 + abstract ITransientMap doAssoc(Object key, Object val); 1.23 + abstract ITransientMap doWithout(Object key); 1.24 + abstract Object doValAt(Object key, Object notFound); 1.25 + abstract int doCount(); 1.26 + abstract IPersistentMap doPersistent(); 1.27 + 1.28 + public ITransientMap conj(Object o) { 1.29 + ensureEditable(); 1.30 + if(o instanceof Map.Entry) 1.31 + { 1.32 + Map.Entry e = (Map.Entry) o; 1.33 + 1.34 + return assoc(e.getKey(), e.getValue()); 1.35 + } 1.36 + else if(o instanceof IPersistentVector) 1.37 + { 1.38 + IPersistentVector v = (IPersistentVector) o; 1.39 + if(v.count() != 2) 1.40 + throw new IllegalArgumentException("Vector arg to map conj must be a pair"); 1.41 + return assoc(v.nth(0), v.nth(1)); 1.42 + } 1.43 + 1.44 + ITransientMap ret = this; 1.45 + for(ISeq es = RT.seq(o); es != null; es = es.next()) 1.46 + { 1.47 + Map.Entry e = (Map.Entry) es.first(); 1.48 + ret = ret.assoc(e.getKey(), e.getValue()); 1.49 + } 1.50 + return ret; 1.51 + } 1.52 + 1.53 + public final Object invoke(Object arg1) throws Exception{ 1.54 + return valAt(arg1); 1.55 + } 1.56 + 1.57 + public final Object invoke(Object arg1, Object notFound) throws Exception{ 1.58 + return valAt(arg1, notFound); 1.59 + } 1.60 + 1.61 + public final Object valAt(Object key) { 1.62 + return valAt(key, null); 1.63 + } 1.64 + 1.65 + public final ITransientMap assoc(Object key, Object val) { 1.66 + ensureEditable(); 1.67 + return doAssoc(key, val); 1.68 + } 1.69 + 1.70 + public final ITransientMap without(Object key) { 1.71 + ensureEditable(); 1.72 + return doWithout(key); 1.73 + } 1.74 + 1.75 + public final IPersistentMap persistent() { 1.76 + ensureEditable(); 1.77 + return doPersistent(); 1.78 + } 1.79 + 1.80 + public final Object valAt(Object key, Object notFound) { 1.81 + ensureEditable(); 1.82 + return doValAt(key, notFound); 1.83 + } 1.84 + 1.85 + public final int count() { 1.86 + ensureEditable(); 1.87 + return doCount(); 1.88 + } 1.89 +}