view src/clojure/lang/Keyword.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 /* rich Mar 29, 2006 10:39:05 AM */
13 package clojure.lang;
15 import java.io.ObjectStreamException;
16 import java.io.Serializable;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.lang.ref.ReferenceQueue;
19 import java.lang.ref.SoftReference;
22 public final class Keyword implements IFn, Comparable, Named, Serializable {
24 private static ConcurrentHashMap<Symbol, SoftReference<Keyword>> table = new ConcurrentHashMap();
25 static final ReferenceQueue rq = new ReferenceQueue();
26 public final Symbol sym;
27 final int hash;
29 public static Keyword intern(Symbol sym){
30 Util.clearCache(rq, table);
31 Keyword k = new Keyword(sym);
32 SoftReference<Keyword> existingRef = table.putIfAbsent(sym, new SoftReference<Keyword>(k,rq));
33 if(existingRef == null)
34 return k;
35 Keyword existingk = existingRef.get();
36 if(existingk != null)
37 return existingk;
38 //entry died in the interim, do over
39 return intern(sym);
40 }
42 public static Keyword intern(String ns, String name){
43 return intern(Symbol.intern(ns, name));
44 }
46 public static Keyword intern(String nsname){
47 return intern(Symbol.intern(nsname));
48 }
50 private Keyword(Symbol sym){
51 this.sym = sym;
52 hash = sym.hashCode() + 0x9e3779b9;
53 }
55 public final int hashCode(){
56 return hash;
57 }
59 public String toString(){
60 return ":" + sym;
61 }
63 public Object throwArity(){
64 throw new IllegalArgumentException("Wrong number of args passed to keyword: "
65 + toString());
66 }
68 public Object call() throws Exception{
69 return throwArity();
70 }
72 public void run(){
73 throw new UnsupportedOperationException();
74 }
76 public Object invoke() throws Exception{
77 return throwArity();
78 }
80 public int compareTo(Object o){
81 return sym.compareTo(((Keyword) o).sym);
82 }
85 public String getNamespace(){
86 return sym.getNamespace();
87 }
89 public String getName(){
90 return sym.getName();
91 }
93 private Object readResolve() throws ObjectStreamException{
94 return intern(sym);
95 }
97 /**
98 * Indexer implements IFn for attr access
99 *
100 * @param obj - must be IPersistentMap
101 * @return the value at the key or nil if not found
102 * @throws Exception
103 */
104 final public Object invoke(Object obj) throws Exception{
105 if(obj instanceof ILookup)
106 return ((ILookup)obj).valAt(this);
107 return RT.get(obj, this);
108 }
110 final public Object invoke(Object obj, Object notFound) throws Exception{
111 if(obj instanceof ILookup)
112 return ((ILookup)obj).valAt(this,notFound);
113 return RT.get(obj, this, notFound);
114 }
116 public Object invoke(Object arg1, Object arg2, Object arg3) throws Exception{
117 return throwArity();
118 }
120 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4) throws Exception{
121 return throwArity();
122 }
124 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5) throws Exception{
125 return throwArity();
126 }
128 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6) throws Exception{
129 return throwArity();
130 }
132 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7)
133 throws Exception{
134 return throwArity();
135 }
137 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
138 Object arg8) throws Exception{
139 return throwArity();
140 }
142 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
143 Object arg8, Object arg9) throws Exception{
144 return throwArity();
145 }
147 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
148 Object arg8, Object arg9, Object arg10) throws Exception{
149 return throwArity();
150 }
152 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
153 Object arg8, Object arg9, Object arg10, Object arg11) throws Exception{
154 return throwArity();
155 }
157 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
158 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12) throws Exception{
159 return throwArity();
160 }
162 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
163 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13)
164 throws Exception{
165 return throwArity();
166 }
168 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
169 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14)
170 throws Exception{
171 return throwArity();
172 }
174 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
175 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
176 Object arg15) throws Exception{
177 return throwArity();
178 }
180 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
181 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
182 Object arg15, Object arg16) throws Exception{
183 return throwArity();
184 }
186 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
187 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
188 Object arg15, Object arg16, Object arg17) throws Exception{
189 return throwArity();
190 }
192 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
193 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
194 Object arg15, Object arg16, Object arg17, Object arg18) throws Exception{
195 return throwArity();
196 }
198 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
199 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
200 Object arg15, Object arg16, Object arg17, Object arg18, Object arg19) throws Exception{
201 return throwArity();
202 }
204 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
205 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
206 Object arg15, Object arg16, Object arg17, Object arg18, Object arg19, Object arg20)
207 throws Exception{
208 return throwArity();
209 }
211 public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4, Object arg5, Object arg6, Object arg7,
212 Object arg8, Object arg9, Object arg10, Object arg11, Object arg12, Object arg13, Object arg14,
213 Object arg15, Object arg16, Object arg17, Object arg18, Object arg19, Object arg20,
214 Object... args)
215 throws Exception{
216 return throwArity();
217 }
220 public Object applyTo(ISeq arglist) throws Exception{
221 return AFn.applyToHelper(this, arglist);
222 }
225 }