/
Symbol.java
136 lines (111 loc) · 2.96 KB
/
Symbol.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
/**
* 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 Mar 25, 2006 11:42:47 AM */
package clojure.lang;
import java.io.Serializable;
import java.io.ObjectStreamException;
public class Symbol extends AFn implements IObj, Comparable, Named, Serializable, IHashEq{
final String ns;
final String name;
private int _hasheq;
final IPersistentMap _meta;
transient String _str;
public String toString(){
if(_str == null){
if(ns != null)
_str = (ns + "/" + name);
else
_str = name;
}
return _str;
}
public String getNamespace(){
return ns;
}
public String getName(){
return name;
}
// the create thunks preserve binary compatibility with code compiled
// against earlier version of Clojure and can be removed (at some point).
static public Symbol create(String ns, String name) {
return Symbol.intern(ns, name);
}
static public Symbol create(String nsname) {
return Symbol.intern(nsname);
}
static public Symbol intern(String ns, String name){
return new Symbol(ns, name);
}
static public Symbol intern(String nsname){
int i = nsname.indexOf('/');
if(i == -1 || nsname.equals("/"))
return new Symbol(null, nsname);
else
return new Symbol(nsname.substring(0, i), nsname.substring(i + 1));
}
private Symbol(String ns_interned, String name_interned){
this.name = name_interned;
this.ns = ns_interned;
this._meta = null;
}
public boolean equals(Object o){
if(this == o)
return true;
if(!(o instanceof Symbol))
return false;
Symbol symbol = (Symbol) o;
return Util.equals(ns,symbol.ns) && name.equals(symbol.name);
}
public int hashCode(){
return Util.hashCombine(name.hashCode(), Util.hash(ns));
}
public int hasheq() {
if(_hasheq == 0){
_hasheq = Util.hashCombine(Murmur3.hashUnencodedChars(name), Util.hash(ns));
}
return _hasheq;
}
public IObj withMeta(IPersistentMap meta){
return new Symbol(meta, ns, name);
}
private Symbol(IPersistentMap meta, String ns, String name){
this.name = name;
this.ns = ns;
this._meta = meta;
}
public int compareTo(Object o){
Symbol s = (Symbol) o;
if(this.equals(o))
return 0;
if(this.ns == null && s.ns != null)
return -1;
if(this.ns != null)
{
if(s.ns == null)
return 1;
int nsc = this.ns.compareTo(s.ns);
if(nsc != 0)
return nsc;
}
return this.name.compareTo(s.name);
}
private Object readResolve() throws ObjectStreamException{
return intern(ns, name);
}
public Object invoke(Object obj) {
return RT.get(obj, this);
}
public Object invoke(Object obj, Object notFound) {
return RT.get(obj, this, notFound);
}
public IPersistentMap meta(){
return _meta;
}
}