rlm@10: /** rlm@10: * Copyright (c) Rich Hickey. All rights reserved. rlm@10: * The use and distribution terms for this software are covered by the rlm@10: * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) rlm@10: * which can be found in the file epl-v10.html at the root of this distribution. rlm@10: * By using this software in any fashion, you are agreeing to be bound by rlm@10: * the terms of this license. rlm@10: * You must not remove this notice, or any other, from this software. rlm@10: **/ rlm@10: rlm@10: /* rich Apr 19, 2008 */ rlm@10: rlm@10: package clojure.lang; rlm@10: rlm@10: import java.math.BigInteger; rlm@10: import java.util.Map; rlm@10: import java.util.concurrent.ConcurrentHashMap; rlm@10: import java.lang.ref.SoftReference; rlm@10: import java.lang.ref.ReferenceQueue; rlm@10: import java.lang.ref.Reference; rlm@10: rlm@10: public class Util{ rlm@10: static public boolean equiv(Object k1, Object k2){ rlm@10: if(k1 == k2) rlm@10: return true; rlm@10: if(k1 != null) rlm@10: { rlm@10: if(k1 instanceof Number && k2 instanceof Number) rlm@10: return Numbers.equiv(k1, k2); rlm@10: else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection) rlm@10: return pcequiv(k1,k2); rlm@10: return k1.equals(k2); rlm@10: } rlm@10: return false; rlm@10: } rlm@10: rlm@10: static public boolean pcequiv(Object k1, Object k2){ rlm@10: if(k1 instanceof IPersistentCollection) rlm@10: return ((IPersistentCollection)k1).equiv(k2); rlm@10: return ((IPersistentCollection)k2).equiv(k1); rlm@10: } rlm@10: rlm@10: static public boolean equals(Object k1, Object k2){ rlm@10: if(k1 == k2) rlm@10: return true; rlm@10: return k1 != null && k1.equals(k2); rlm@10: } rlm@10: rlm@10: static public boolean identical(Object k1, Object k2){ rlm@10: return k1 == k2; rlm@10: } rlm@10: rlm@10: static public Class classOf(Object x){ rlm@10: if(x != null) rlm@10: return x.getClass(); rlm@10: return null; rlm@10: } rlm@10: rlm@10: static public int compare(Object k1, Object k2){ rlm@10: if(k1 == k2) rlm@10: return 0; rlm@10: if(k1 != null) rlm@10: { rlm@10: if(k2 == null) rlm@10: return 1; rlm@10: if(k1 instanceof Number) rlm@10: return Numbers.compare((Number) k1, (Number) k2); rlm@10: return ((Comparable) k1).compareTo(k2); rlm@10: } rlm@10: return -1; rlm@10: } rlm@10: rlm@10: static public int hash(Object o){ rlm@10: if(o == null) rlm@10: return 0; rlm@10: return o.hashCode(); rlm@10: } rlm@10: rlm@10: static public int hashCombine(int seed, int hash){ rlm@10: //a la boost rlm@10: seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2); rlm@10: return seed; rlm@10: } rlm@10: rlm@10: static public boolean isPrimitive(Class c){ rlm@10: return c != null && c.isPrimitive() && !(c == Void.TYPE); rlm@10: } rlm@10: rlm@10: static public boolean isInteger(Object x){ rlm@10: return x instanceof Integer rlm@10: || x instanceof Long rlm@10: || x instanceof BigInteger; rlm@10: } rlm@10: rlm@10: static public Object ret1(Object ret, Object nil){ rlm@10: return ret; rlm@10: } rlm@10: rlm@10: static public ISeq ret1(ISeq ret, Object nil){ rlm@10: return ret; rlm@10: } rlm@10: rlm@10: static public void clearCache(ReferenceQueue rq, ConcurrentHashMap> cache){ rlm@10: //cleanup any dead entries rlm@10: if(rq.poll() != null) rlm@10: { rlm@10: while(rq.poll() != null) rlm@10: ; rlm@10: for(Map.Entry> e : cache.entrySet()) rlm@10: { rlm@10: if(e.getValue().get() == null) rlm@10: cache.remove(e.getKey(), e.getValue()); rlm@10: } rlm@10: } rlm@10: } rlm@10: }