forked from clojure/clojure
/
Namespace.java
215 lines (181 loc) · 5.79 KB
/
Namespace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* 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.
**/
/* rich Jan 23, 2008 */
package clojure.lang;
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
public class Namespace extends AReference implements Serializable {
final public Symbol name;
transient final AtomicReference<IPersistentMap> mappings = new AtomicReference<IPersistentMap>();
transient final AtomicReference<IPersistentMap> aliases = new AtomicReference<IPersistentMap>();
final static ConcurrentHashMap<Symbol, Namespace> namespaces = new ConcurrentHashMap<Symbol, Namespace>();
public String toString(){
return name.toString();
}
Namespace(Symbol name){
super(name.meta());
this.name = name;
mappings.set(RT.DEFAULT_IMPORTS);
aliases.set(RT.map());
}
public static ISeq all(){
return RT.seq(namespaces.values());
}
public Symbol getName(){
return name;
}
public IPersistentMap getMappings(){
return mappings.get();
}
public Var intern(Symbol sym){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Object o;
Var v = null;
while((o = map.valAt(sym)) == null)
{
if(v == null)
v = new Var(this, sym);
IPersistentMap newMap = map.assoc(sym, v);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
if(o instanceof Var && ((Var) o).ns == this)
return (Var) o;
throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
}
Object reference(Symbol sym, Object val){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Object o;
while((o = map.valAt(sym)) == null)
{
IPersistentMap newMap = map.assoc(sym, val);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
if(o == val)
return o;
throw new IllegalStateException(sym + " already refers to: " + o + " in namespace: " + name);
}
public static boolean areDifferentInstancesOfSameClassName(Class cls1, Class cls2) {
return (cls1 != cls2) && (cls1.getName().equals(cls2.getName()));
}
Class referenceClass(Symbol sym, Class val){
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't intern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
Class c = (Class) map.valAt(sym);
while((c == null) || (areDifferentInstancesOfSameClassName(c, val)))
{
IPersistentMap newMap = map.assoc(sym, val);
mappings.compareAndSet(map, newMap);
map = getMappings();
c = (Class) map.valAt(sym);
}
if(c == val)
return c;
throw new IllegalStateException(sym + " already refers to: " + c + " in namespace: " + name);
}
public void unmap(Symbol sym) throws Exception{
if(sym.ns != null)
{
throw new IllegalArgumentException("Can't unintern namespace-qualified symbol");
}
IPersistentMap map = getMappings();
while(map.containsKey(sym))
{
IPersistentMap newMap = map.without(sym);
mappings.compareAndSet(map, newMap);
map = getMappings();
}
}
public Class importClass(Symbol sym, Class c){
return referenceClass(sym, c);
}
public Class importClass(Class c){
String n = c.getName();
return importClass(Symbol.intern(n.substring(n.lastIndexOf('.') + 1)), c);
}
public Var refer(Symbol sym, Var var){
return (Var) reference(sym, var);
}
public static Namespace findOrCreate(Symbol name){
Namespace ns = namespaces.get(name);
if(ns != null)
return ns;
Namespace newns = new Namespace(name);
ns = namespaces.putIfAbsent(name, newns);
return ns == null ? newns : ns;
}
public static Namespace remove(Symbol name){
if(name.equals(RT.CLOJURE_NS.name))
throw new IllegalArgumentException("Cannot remove clojure namespace");
return namespaces.remove(name);
}
public static Namespace find(Symbol name){
return namespaces.get(name);
}
public Object getMapping(Symbol name){
return mappings.get().valAt(name);
}
public Var findInternedVar(Symbol symbol){
Object o = mappings.get().valAt(symbol);
if(o != null && o instanceof Var && ((Var) o).ns == this)
return (Var) o;
return null;
}
public IPersistentMap getAliases(){
return aliases.get();
}
public Namespace lookupAlias(Symbol alias){
IPersistentMap map = getAliases();
return (Namespace) map.valAt(alias);
}
public void addAlias(Symbol alias, Namespace ns){
if (alias == null || ns == null)
throw new NullPointerException("Expecting Symbol + Namespace");
IPersistentMap map = getAliases();
while(!map.containsKey(alias))
{
IPersistentMap newMap = map.assoc(alias, ns);
aliases.compareAndSet(map, newMap);
map = getAliases();
}
// you can rebind an alias, but only to the initially-aliased namespace.
if(!map.valAt(alias).equals(ns))
throw new IllegalStateException("Alias " + alias + " already exists in namespace "
+ name + ", aliasing " + map.valAt(alias));
}
public void removeAlias(Symbol alias) throws Exception{
IPersistentMap map = getAliases();
while(map.containsKey(alias))
{
IPersistentMap newMap = map.without(alias);
aliases.compareAndSet(map, newMap);
map = getAliases();
}
}
private Object readResolve() throws ObjectStreamException {
// ensures that serialized namespaces are "deserialized" to the
// namespace in the present runtime
return findOrCreate(name);
}
}