Mercurial > lasercutter
diff src/clojure/lang/Namespace.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/Namespace.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,243 @@ 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 +/* rich Jan 23, 2008 */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.io.ObjectStreamException; 1.19 +import java.io.Serializable; 1.20 +import java.util.concurrent.ConcurrentHashMap; 1.21 +import java.util.concurrent.atomic.AtomicReference; 1.22 + 1.23 +public class Namespace extends AReference implements Serializable { 1.24 +final public Symbol name; 1.25 +transient final AtomicReference<IPersistentMap> mappings = new AtomicReference<IPersistentMap>(); 1.26 +transient final AtomicReference<IPersistentMap> aliases = new AtomicReference<IPersistentMap>(); 1.27 + 1.28 +final static ConcurrentHashMap<Symbol, Namespace> namespaces = new ConcurrentHashMap<Symbol, Namespace>(); 1.29 + 1.30 +public String toString(){ 1.31 + return name.toString(); 1.32 +} 1.33 + 1.34 +Namespace(Symbol name){ 1.35 + super(name.meta()); 1.36 + this.name = name; 1.37 + mappings.set(RT.DEFAULT_IMPORTS); 1.38 + aliases.set(RT.map()); 1.39 +} 1.40 + 1.41 +public static ISeq all(){ 1.42 + return RT.seq(namespaces.values()); 1.43 +} 1.44 + 1.45 +public Symbol getName(){ 1.46 + return name; 1.47 +} 1.48 + 1.49 +public IPersistentMap getMappings(){ 1.50 + return mappings.get(); 1.51 +} 1.52 + 1.53 +public Var intern(Symbol sym){ 1.54 + if(sym.ns != null) 1.55 + { 1.56 + throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); 1.57 + } 1.58 + IPersistentMap map = getMappings(); 1.59 + Object o; 1.60 + Var v = null; 1.61 + while((o = map.valAt(sym)) == null) 1.62 + { 1.63 + if(v == null) 1.64 + v = new Var(this, sym); 1.65 + IPersistentMap newMap = map.assoc(sym, v); 1.66 + mappings.compareAndSet(map, newMap); 1.67 + map = getMappings(); 1.68 + } 1.69 + if(o instanceof Var && ((Var) o).ns == this) 1.70 + return (Var) o; 1.71 + 1.72 + if(v == null) 1.73 + v = new Var(this, sym); 1.74 + 1.75 + warnOrFailOnReplace(sym, o, v); 1.76 + 1.77 + 1.78 + while(!mappings.compareAndSet(map, map.assoc(sym, v))) 1.79 + map = getMappings(); 1.80 + 1.81 + return v; 1.82 +} 1.83 + 1.84 +private void warnOrFailOnReplace(Symbol sym, Object o, Object v){ 1.85 + if (o instanceof Var) 1.86 + { 1.87 + Namespace ns = ((Var)o).ns; 1.88 + if (ns == this) 1.89 + return; 1.90 + if (ns != RT.CLOJURE_NS) 1.91 + throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name); 1.92 + } 1.93 + RT.errPrintWriter().println("WARNING: " + sym + " already refers to: " + o + " in namespace: " + name 1.94 + + ", being replaced by: " + v); 1.95 +} 1.96 + 1.97 +Object reference(Symbol sym, Object val){ 1.98 + if(sym.ns != null) 1.99 + { 1.100 + throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); 1.101 + } 1.102 + IPersistentMap map = getMappings(); 1.103 + Object o; 1.104 + while((o = map.valAt(sym)) == null) 1.105 + { 1.106 + IPersistentMap newMap = map.assoc(sym, val); 1.107 + mappings.compareAndSet(map, newMap); 1.108 + map = getMappings(); 1.109 + } 1.110 + if(o == val) 1.111 + return o; 1.112 + 1.113 + warnOrFailOnReplace(sym, o, val); 1.114 + 1.115 + while(!mappings.compareAndSet(map, map.assoc(sym, val))) 1.116 + map = getMappings(); 1.117 + 1.118 + return val; 1.119 + 1.120 +} 1.121 + 1.122 +public static boolean areDifferentInstancesOfSameClassName(Class cls1, Class cls2) { 1.123 + return (cls1 != cls2) && (cls1.getName().equals(cls2.getName())); 1.124 +} 1.125 + 1.126 +Class referenceClass(Symbol sym, Class val){ 1.127 + if(sym.ns != null) 1.128 + { 1.129 + throw new IllegalArgumentException("Can't intern namespace-qualified symbol"); 1.130 + } 1.131 + IPersistentMap map = getMappings(); 1.132 + Class c = (Class) map.valAt(sym); 1.133 + while((c == null) || (areDifferentInstancesOfSameClassName(c, val))) 1.134 + { 1.135 + IPersistentMap newMap = map.assoc(sym, val); 1.136 + mappings.compareAndSet(map, newMap); 1.137 + map = getMappings(); 1.138 + c = (Class) map.valAt(sym); 1.139 + } 1.140 + if(c == val) 1.141 + return c; 1.142 + 1.143 + throw new IllegalStateException(sym + " already refers to: " + c + " in namespace: " + name); 1.144 +} 1.145 + 1.146 +public void unmap(Symbol sym) throws Exception{ 1.147 + if(sym.ns != null) 1.148 + { 1.149 + throw new IllegalArgumentException("Can't unintern namespace-qualified symbol"); 1.150 + } 1.151 + IPersistentMap map = getMappings(); 1.152 + while(map.containsKey(sym)) 1.153 + { 1.154 + IPersistentMap newMap = map.without(sym); 1.155 + mappings.compareAndSet(map, newMap); 1.156 + map = getMappings(); 1.157 + } 1.158 +} 1.159 + 1.160 +public Class importClass(Symbol sym, Class c){ 1.161 + return referenceClass(sym, c); 1.162 + 1.163 +} 1.164 + 1.165 +public Class importClass(Class c){ 1.166 + String n = c.getName(); 1.167 + return importClass(Symbol.intern(n.substring(n.lastIndexOf('.') + 1)), c); 1.168 +} 1.169 + 1.170 +public Var refer(Symbol sym, Var var){ 1.171 + return (Var) reference(sym, var); 1.172 + 1.173 +} 1.174 + 1.175 +public static Namespace findOrCreate(Symbol name){ 1.176 + Namespace ns = namespaces.get(name); 1.177 + if(ns != null) 1.178 + return ns; 1.179 + Namespace newns = new Namespace(name); 1.180 + ns = namespaces.putIfAbsent(name, newns); 1.181 + return ns == null ? newns : ns; 1.182 +} 1.183 + 1.184 +public static Namespace remove(Symbol name){ 1.185 + if(name.equals(RT.CLOJURE_NS.name)) 1.186 + throw new IllegalArgumentException("Cannot remove clojure namespace"); 1.187 + return namespaces.remove(name); 1.188 +} 1.189 + 1.190 +public static Namespace find(Symbol name){ 1.191 + return namespaces.get(name); 1.192 +} 1.193 + 1.194 +public Object getMapping(Symbol name){ 1.195 + return mappings.get().valAt(name); 1.196 +} 1.197 + 1.198 +public Var findInternedVar(Symbol symbol){ 1.199 + Object o = mappings.get().valAt(symbol); 1.200 + if(o != null && o instanceof Var && ((Var) o).ns == this) 1.201 + return (Var) o; 1.202 + return null; 1.203 +} 1.204 + 1.205 + 1.206 +public IPersistentMap getAliases(){ 1.207 + return aliases.get(); 1.208 +} 1.209 + 1.210 +public Namespace lookupAlias(Symbol alias){ 1.211 + IPersistentMap map = getAliases(); 1.212 + return (Namespace) map.valAt(alias); 1.213 +} 1.214 + 1.215 +public void addAlias(Symbol alias, Namespace ns){ 1.216 + if (alias == null || ns == null) 1.217 + throw new NullPointerException("Expecting Symbol + Namespace"); 1.218 + IPersistentMap map = getAliases(); 1.219 + while(!map.containsKey(alias)) 1.220 + { 1.221 + IPersistentMap newMap = map.assoc(alias, ns); 1.222 + aliases.compareAndSet(map, newMap); 1.223 + map = getAliases(); 1.224 + } 1.225 + // you can rebind an alias, but only to the initially-aliased namespace. 1.226 + if(!map.valAt(alias).equals(ns)) 1.227 + throw new IllegalStateException("Alias " + alias + " already exists in namespace " 1.228 + + name + ", aliasing " + map.valAt(alias)); 1.229 +} 1.230 + 1.231 +public void removeAlias(Symbol alias) throws Exception{ 1.232 + IPersistentMap map = getAliases(); 1.233 + while(map.containsKey(alias)) 1.234 + { 1.235 + IPersistentMap newMap = map.without(alias); 1.236 + aliases.compareAndSet(map, newMap); 1.237 + map = getAliases(); 1.238 + } 1.239 +} 1.240 + 1.241 +private Object readResolve() throws ObjectStreamException { 1.242 + // ensures that serialized namespaces are "deserialized" to the 1.243 + // namespace in the present runtime 1.244 + return findOrCreate(name); 1.245 +} 1.246 +}