Mercurial > lasercutter
view src/clojure/lang/Util.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 the4 * 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 by7 * the terms of this license.8 * You must not remove this notice, or any other, from this software.9 **/11 /* rich Apr 19, 2008 */13 package clojure.lang;15 import java.math.BigInteger;16 import java.util.Map;17 import java.util.concurrent.ConcurrentHashMap;18 import java.lang.ref.SoftReference;19 import java.lang.ref.ReferenceQueue;20 import java.lang.ref.Reference;22 public class Util{23 static public boolean equiv(Object k1, Object k2){24 if(k1 == k2)25 return true;26 if(k1 != null)27 {28 if(k1 instanceof Number && k2 instanceof Number)29 return Numbers.equiv(k1, k2);30 else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection)31 return pcequiv(k1,k2);32 return k1.equals(k2);33 }34 return false;35 }37 static public boolean pcequiv(Object k1, Object k2){38 if(k1 instanceof IPersistentCollection)39 return ((IPersistentCollection)k1).equiv(k2);40 return ((IPersistentCollection)k2).equiv(k1);41 }43 static public boolean equals(Object k1, Object k2){44 if(k1 == k2)45 return true;46 return k1 != null && k1.equals(k2);47 }49 static public boolean identical(Object k1, Object k2){50 return k1 == k2;51 }53 static public Class classOf(Object x){54 if(x != null)55 return x.getClass();56 return null;57 }59 static public int compare(Object k1, Object k2){60 if(k1 == k2)61 return 0;62 if(k1 != null)63 {64 if(k2 == null)65 return 1;66 if(k1 instanceof Number)67 return Numbers.compare((Number) k1, (Number) k2);68 return ((Comparable) k1).compareTo(k2);69 }70 return -1;71 }73 static public int hash(Object o){74 if(o == null)75 return 0;76 return o.hashCode();77 }79 static public int hashCombine(int seed, int hash){80 //a la boost81 seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);82 return seed;83 }85 static public boolean isPrimitive(Class c){86 return c != null && c.isPrimitive() && !(c == Void.TYPE);87 }89 static public boolean isInteger(Object x){90 return x instanceof Integer91 || x instanceof Long92 || x instanceof BigInteger;93 }95 static public Object ret1(Object ret, Object nil){96 return ret;97 }99 static public ISeq ret1(ISeq ret, Object nil){100 return ret;101 }103 static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, SoftReference<V>> cache){104 //cleanup any dead entries105 if(rq.poll() != null)106 {107 while(rq.poll() != null)108 ;109 for(Map.Entry<K, SoftReference<V>> e : cache.entrySet())110 {111 if(e.getValue().get() == null)112 cache.remove(e.getKey(), e.getValue());113 }114 }115 }116 }