Mercurial > lasercutter
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/lang/Symbol.java Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,126 @@ 1.4 +/** 1.5 + * Copyright (c) Rich Hickey. All rights reserved. 1.6 + * The use and distribution terms for this software are covered by the 1.7 + * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.8 + * which can be found in the file epl-v10.html at the root of this distribution. 1.9 + * By using this software in any fashion, you are agreeing to be bound by 1.10 + * the terms of this license. 1.11 + * You must not remove this notice, or any other, from this software. 1.12 + **/ 1.13 + 1.14 +/* rich Mar 25, 2006 11:42:47 AM */ 1.15 + 1.16 +package clojure.lang; 1.17 + 1.18 +import java.io.Serializable; 1.19 +import java.io.ObjectStreamException; 1.20 + 1.21 + 1.22 +public class Symbol extends AFn implements IObj, Comparable, Named, Serializable{ 1.23 +//these must be interned strings! 1.24 +final String ns; 1.25 +final String name; 1.26 +final int hash; 1.27 +final IPersistentMap _meta; 1.28 + 1.29 +public String toString(){ 1.30 + if(ns != null) 1.31 + return ns + "/" + name; 1.32 + return name; 1.33 +} 1.34 + 1.35 +public String getNamespace(){ 1.36 + return ns; 1.37 +} 1.38 + 1.39 +public String getName(){ 1.40 + return name; 1.41 +} 1.42 + 1.43 +static public Symbol intern(String ns, String name){ 1.44 + return new Symbol(ns == null ? null : ns.intern(), name.intern()); 1.45 +} 1.46 + 1.47 +static public Symbol intern(String nsname){ 1.48 + int i = nsname.lastIndexOf('/'); 1.49 + if(i == -1 || nsname.equals("/")) 1.50 + return new Symbol(null, nsname.intern()); 1.51 + else 1.52 + return new Symbol(nsname.substring(0, i).intern(), nsname.substring(i + 1).intern()); 1.53 +} 1.54 + 1.55 +static public Symbol create(String name_interned){ 1.56 + return new Symbol(null, name_interned); 1.57 +} 1.58 + 1.59 +static public Symbol create(String ns_interned, String name_interned){ 1.60 + return new Symbol(ns_interned, name_interned); 1.61 +} 1.62 + 1.63 +private Symbol(String ns_interned, String name_interned){ 1.64 + this.name = name_interned; 1.65 + this.ns = ns_interned; 1.66 + this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns)); 1.67 + this._meta = null; 1.68 +} 1.69 + 1.70 +public boolean equals(Object o){ 1.71 + if(this == o) 1.72 + return true; 1.73 + if(!(o instanceof Symbol)) 1.74 + return false; 1.75 + 1.76 + Symbol symbol = (Symbol) o; 1.77 + 1.78 + //identity compares intended, names are interned 1.79 + return name == symbol.name && ns == symbol.ns; 1.80 +} 1.81 + 1.82 +public int hashCode(){ 1.83 + return hash; 1.84 +} 1.85 + 1.86 +public IObj withMeta(IPersistentMap meta){ 1.87 + return new Symbol(meta, ns, name); 1.88 +} 1.89 + 1.90 +private Symbol(IPersistentMap meta, String ns, String name){ 1.91 + this.name = name; 1.92 + this.ns = ns; 1.93 + this._meta = meta; 1.94 + this.hash = Util.hashCombine(name.hashCode(), Util.hash(ns)); 1.95 +} 1.96 + 1.97 +public int compareTo(Object o){ 1.98 + Symbol s = (Symbol) o; 1.99 + if(this.equals(o)) 1.100 + return 0; 1.101 + if(this.ns == null && s.ns != null) 1.102 + return -1; 1.103 + if(this.ns != null) 1.104 + { 1.105 + if(s.ns == null) 1.106 + return 1; 1.107 + int nsc = this.ns.compareTo(s.ns); 1.108 + if(nsc != 0) 1.109 + return nsc; 1.110 + } 1.111 + return this.name.compareTo(s.name); 1.112 +} 1.113 + 1.114 +private Object readResolve() throws ObjectStreamException{ 1.115 + return intern(ns, name); 1.116 +} 1.117 + 1.118 +public Object invoke(Object obj) throws Exception{ 1.119 + return RT.get(obj, this); 1.120 +} 1.121 + 1.122 +public Object invoke(Object obj, Object notFound) throws Exception{ 1.123 + return RT.get(obj, this, notFound); 1.124 +} 1.125 + 1.126 +public IPersistentMap meta(){ 1.127 + return _meta; 1.128 +} 1.129 +}