Skip to content
Browse files

Make Keyword and Symbol implement IHashEq and reorder instanceof test…

…s in Util.hasheq to acknowledge for common cases.

Signed-off-by: Rich Hickey <richhickey@gmail.com>
  • Loading branch information...
1 parent 6fd7b77 commit 94e7f39171a418d09356bd2c7865697f5b21281e @cgrand cgrand committed with richhickey
Showing with 15 additions and 7 deletions.
  1. +5 −1 src/jvm/clojure/lang/Keyword.java
  2. +5 −1 src/jvm/clojure/lang/Symbol.java
  3. +5 −5 src/jvm/clojure/lang/Util.java
View
6 src/jvm/clojure/lang/Keyword.java
@@ -21,7 +21,7 @@
import java.lang.ref.SoftReference;
-public class Keyword implements IFn, Comparable, Named, Serializable {
+public class Keyword implements IFn, Comparable, Named, Serializable, IHashEq {
private static ConcurrentHashMap<Symbol, Reference<Keyword>> table = new ConcurrentHashMap();
static final ReferenceQueue rq = new ReferenceQueue();
@@ -78,6 +78,10 @@ public final int hashCode(){
return hash;
}
+public int hasheq() {
+ return hash;
+}
+
public String toString(){
if(_str == null)
_str = (":" + sym).intern();
View
6 src/jvm/clojure/lang/Symbol.java
@@ -16,7 +16,7 @@
import java.io.ObjectStreamException;
-public class Symbol extends AFn implements IObj, Comparable, Named, Serializable{
+public class Symbol extends AFn implements IObj, Comparable, Named, Serializable, IHashEq{
//these must be interned strings!
final String ns;
final String name;
@@ -87,6 +87,10 @@ public int hashCode(){
return hash;
}
+public int hasheq() {
+ return hash;
+}
+
public IObj withMeta(IPersistentMap meta){
return new Symbol(meta, ns, name);
}
View
10 src/jvm/clojure/lang/Util.java
@@ -116,18 +116,18 @@ static public int hash(Object o){
return o.hashCode();
}
-static public int hasheq(Object o){
+public static int hasheq(Object o){
if(o == null)
return 0;
+ if(o instanceof IHashEq)
+ return dohasheq((IHashEq) o);
if(o instanceof Number)
return Numbers.hasheq((Number)o);
- else if(o instanceof IHashEq)
- return dohasheq(o);
return o.hashCode();
}
-public static int dohasheq(Object o) {
- return ((IHashEq)o).hasheq();
+private static int dohasheq(IHashEq o) {
+ return o.hasheq();
}
static public int hashCombine(int seed, int hash){

0 comments on commit 94e7f39

Please sign in to comment.
Something went wrong with that request. Please try again.