view src/clojure/lang/Symbol.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 25, 2006 11:42:47 AM */
13 package clojure.lang;
15 import java.io.Serializable;
16 import java.io.ObjectStreamException;
19 public class Symbol extends AFn implements IObj, Comparable, Named, Serializable{
20 //these must be interned strings!
21 final String ns;
22 final String name;
23 final int hash;
24 final IPersistentMap _meta;
26 public String toString(){
27 if(ns != null)
28 return ns + "/" + name;
29 return name;
30 }
32 public String getNamespace(){
33 return ns;
34 }
36 public String getName(){
37 return name;
38 }
40 static public Symbol intern(String ns, String name){
41 return new Symbol(ns == null ? null : ns.intern(), name.intern());
42 }
44 static public Symbol intern(String nsname){
45 int i = nsname.lastIndexOf('/');
46 if(i == -1 || nsname.equals("/"))
47 return new Symbol(null, nsname.intern());
48 else
49 return new Symbol(nsname.substring(0, i).intern(), nsname.substring(i + 1).intern());
50 }
52 static public Symbol create(String name_interned){
53 return new Symbol(null, name_interned);
54 }
56 static public Symbol create(String ns_interned, String name_interned){
57 return new Symbol(ns_interned, name_interned);
58 }
60 private Symbol(String ns_interned, String name_interned){
61 this.name = name_interned;
62 this.ns = ns_interned;
63 this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
64 this._meta = null;
65 }
67 public boolean equals(Object o){
68 if(this == o)
69 return true;
70 if(!(o instanceof Symbol))
71 return false;
73 Symbol symbol = (Symbol) o;
75 //identity compares intended, names are interned
76 return name == symbol.name && ns == symbol.ns;
77 }
79 public int hashCode(){
80 return hash;
81 }
83 public IObj withMeta(IPersistentMap meta){
84 return new Symbol(meta, ns, name);
85 }
87 private Symbol(IPersistentMap meta, String ns, String name){
88 this.name = name;
89 this.ns = ns;
90 this._meta = meta;
91 this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns));
92 }
94 public int compareTo(Object o){
95 Symbol s = (Symbol) o;
96 if(this.equals(o))
97 return 0;
98 if(this.ns == null && s.ns != null)
99 return -1;
100 if(this.ns != null)
101 {
102 if(s.ns == null)
103 return 1;
104 int nsc = this.ns.compareTo(s.ns);
105 if(nsc != 0)
106 return nsc;
107 }
108 return this.name.compareTo(s.name);
109 }
111 private Object readResolve() throws ObjectStreamException{
112 return intern(ns, name);
113 }
115 public Object invoke(Object obj) throws Exception{
116 return RT.get(obj, this);
117 }
119 public Object invoke(Object obj, Object notFound) throws Exception{
120 return RT.get(obj, this, notFound);
121 }
123 public IPersistentMap meta(){
124 return _meta;
125 }
126 }