rlm@10
|
1 /**
|
rlm@10
|
2 * Copyright (c) Rich Hickey. All rights reserved.
|
rlm@10
|
3 * The use and distribution terms for this software are covered by the
|
rlm@10
|
4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
5 * which can be found in the file epl-v10.html at the root of this distribution.
|
rlm@10
|
6 * By using this software in any fashion, you are agreeing to be bound by
|
rlm@10
|
7 * the terms of this license.
|
rlm@10
|
8 * You must not remove this notice, or any other, from this software.
|
rlm@10
|
9 **/
|
rlm@10
|
10
|
rlm@10
|
11 /* rich Mar 25, 2006 11:42:47 AM */
|
rlm@10
|
12
|
rlm@10
|
13 package clojure.lang;
|
rlm@10
|
14
|
rlm@10
|
15 import java.io.Serializable;
|
rlm@10
|
16 import java.io.ObjectStreamException;
|
rlm@10
|
17
|
rlm@10
|
18
|
rlm@10
|
19 public class Symbol extends AFn implements IObj, Comparable, Named, Serializable{
|
rlm@10
|
20 //these must be interned strings!
|
rlm@10
|
21 final String ns;
|
rlm@10
|
22 final String name;
|
rlm@10
|
23 final int hash;
|
rlm@10
|
24 final IPersistentMap _meta;
|
rlm@10
|
25
|
rlm@10
|
26 public String toString(){
|
rlm@10
|
27 if(ns != null)
|
rlm@10
|
28 return ns + "/" + name;
|
rlm@10
|
29 return name;
|
rlm@10
|
30 }
|
rlm@10
|
31
|
rlm@10
|
32 public String getNamespace(){
|
rlm@10
|
33 return ns;
|
rlm@10
|
34 }
|
rlm@10
|
35
|
rlm@10
|
36 public String getName(){
|
rlm@10
|
37 return name;
|
rlm@10
|
38 }
|
rlm@10
|
39
|
rlm@10
|
40 static public Symbol intern(String ns, String name){
|
rlm@10
|
41 return new Symbol(ns == null ? null : ns.intern(), name.intern());
|
rlm@10
|
42 }
|
rlm@10
|
43
|
rlm@10
|
44 static public Symbol intern(String nsname){
|
rlm@10
|
45 int i = nsname.lastIndexOf('/');
|
rlm@10
|
46 if(i == -1 || nsname.equals("/"))
|
rlm@10
|
47 return new Symbol(null, nsname.intern());
|
rlm@10
|
48 else
|
rlm@10
|
49 return new Symbol(nsname.substring(0, i).intern(), nsname.substring(i + 1).intern());
|
rlm@10
|
50 }
|
rlm@10
|
51
|
rlm@10
|
52 static public Symbol create(String name_interned){
|
rlm@10
|
53 return new Symbol(null, name_interned);
|
rlm@10
|
54 }
|
rlm@10
|
55
|
rlm@10
|
56 static public Symbol create(String ns_interned, String name_interned){
|
rlm@10
|
57 return new Symbol(ns_interned, name_interned);
|
rlm@10
|
58 }
|
rlm@10
|
59
|
rlm@10
|
60 private Symbol(String ns_interned, String name_interned){
|
rlm@10
|
61 this.name = name_interned;
|
rlm@10
|
62 this.ns = ns_interned;
|
rlm@10
|
63 this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
|
rlm@10
|
64 this._meta = null;
|
rlm@10
|
65 }
|
rlm@10
|
66
|
rlm@10
|
67 public boolean equals(Object o){
|
rlm@10
|
68 if(this == o)
|
rlm@10
|
69 return true;
|
rlm@10
|
70 if(!(o instanceof Symbol))
|
rlm@10
|
71 return false;
|
rlm@10
|
72
|
rlm@10
|
73 Symbol symbol = (Symbol) o;
|
rlm@10
|
74
|
rlm@10
|
75 //identity compares intended, names are interned
|
rlm@10
|
76 return name == symbol.name && ns == symbol.ns;
|
rlm@10
|
77 }
|
rlm@10
|
78
|
rlm@10
|
79 public int hashCode(){
|
rlm@10
|
80 return hash;
|
rlm@10
|
81 }
|
rlm@10
|
82
|
rlm@10
|
83 public IObj withMeta(IPersistentMap meta){
|
rlm@10
|
84 return new Symbol(meta, ns, name);
|
rlm@10
|
85 }
|
rlm@10
|
86
|
rlm@10
|
87 private Symbol(IPersistentMap meta, String ns, String name){
|
rlm@10
|
88 this.name = name;
|
rlm@10
|
89 this.ns = ns;
|
rlm@10
|
90 this._meta = meta;
|
rlm@10
|
91 this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
|
rlm@10
|
92 }
|
rlm@10
|
93
|
rlm@10
|
94 public int compareTo(Object o){
|
rlm@10
|
95 Symbol s = (Symbol) o;
|
rlm@10
|
96 if(this.equals(o))
|
rlm@10
|
97 return 0;
|
rlm@10
|
98 if(this.ns == null && s.ns != null)
|
rlm@10
|
99 return -1;
|
rlm@10
|
100 if(this.ns != null)
|
rlm@10
|
101 {
|
rlm@10
|
102 if(s.ns == null)
|
rlm@10
|
103 return 1;
|
rlm@10
|
104 int nsc = this.ns.compareTo(s.ns);
|
rlm@10
|
105 if(nsc != 0)
|
rlm@10
|
106 return nsc;
|
rlm@10
|
107 }
|
rlm@10
|
108 return this.name.compareTo(s.name);
|
rlm@10
|
109 }
|
rlm@10
|
110
|
rlm@10
|
111 private Object readResolve() throws ObjectStreamException{
|
rlm@10
|
112 return intern(ns, name);
|
rlm@10
|
113 }
|
rlm@10
|
114
|
rlm@10
|
115 public Object invoke(Object obj) throws Exception{
|
rlm@10
|
116 return RT.get(obj, this);
|
rlm@10
|
117 }
|
rlm@10
|
118
|
rlm@10
|
119 public Object invoke(Object obj, Object notFound) throws Exception{
|
rlm@10
|
120 return RT.get(obj, this, notFound);
|
rlm@10
|
121 }
|
rlm@10
|
122
|
rlm@10
|
123 public IPersistentMap meta(){
|
rlm@10
|
124 return _meta;
|
rlm@10
|
125 }
|
rlm@10
|
126 }
|