annotate 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
rev   line source
rlm@10 1 /**
rlm@10 2 * Copyright (c) Rich Hickey. All rights reserved.
rlm@10 3 * The use and distribution terms for this software are covered by the
rlm@10 4 * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 5 * which can be found in the file epl-v10.html at the root of this distribution.
rlm@10 6 * By using this software in any fashion, you are agreeing to be bound by
rlm@10 7 * the terms of this license.
rlm@10 8 * You must not remove this notice, or any other, from this software.
rlm@10 9 **/
rlm@10 10
rlm@10 11 /* rich Apr 19, 2008 */
rlm@10 12
rlm@10 13 package clojure.lang;
rlm@10 14
rlm@10 15 import java.math.BigInteger;
rlm@10 16 import java.util.Map;
rlm@10 17 import java.util.concurrent.ConcurrentHashMap;
rlm@10 18 import java.lang.ref.SoftReference;
rlm@10 19 import java.lang.ref.ReferenceQueue;
rlm@10 20 import java.lang.ref.Reference;
rlm@10 21
rlm@10 22 public class Util{
rlm@10 23 static public boolean equiv(Object k1, Object k2){
rlm@10 24 if(k1 == k2)
rlm@10 25 return true;
rlm@10 26 if(k1 != null)
rlm@10 27 {
rlm@10 28 if(k1 instanceof Number && k2 instanceof Number)
rlm@10 29 return Numbers.equiv(k1, k2);
rlm@10 30 else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection)
rlm@10 31 return pcequiv(k1,k2);
rlm@10 32 return k1.equals(k2);
rlm@10 33 }
rlm@10 34 return false;
rlm@10 35 }
rlm@10 36
rlm@10 37 static public boolean pcequiv(Object k1, Object k2){
rlm@10 38 if(k1 instanceof IPersistentCollection)
rlm@10 39 return ((IPersistentCollection)k1).equiv(k2);
rlm@10 40 return ((IPersistentCollection)k2).equiv(k1);
rlm@10 41 }
rlm@10 42
rlm@10 43 static public boolean equals(Object k1, Object k2){
rlm@10 44 if(k1 == k2)
rlm@10 45 return true;
rlm@10 46 return k1 != null && k1.equals(k2);
rlm@10 47 }
rlm@10 48
rlm@10 49 static public boolean identical(Object k1, Object k2){
rlm@10 50 return k1 == k2;
rlm@10 51 }
rlm@10 52
rlm@10 53 static public Class classOf(Object x){
rlm@10 54 if(x != null)
rlm@10 55 return x.getClass();
rlm@10 56 return null;
rlm@10 57 }
rlm@10 58
rlm@10 59 static public int compare(Object k1, Object k2){
rlm@10 60 if(k1 == k2)
rlm@10 61 return 0;
rlm@10 62 if(k1 != null)
rlm@10 63 {
rlm@10 64 if(k2 == null)
rlm@10 65 return 1;
rlm@10 66 if(k1 instanceof Number)
rlm@10 67 return Numbers.compare((Number) k1, (Number) k2);
rlm@10 68 return ((Comparable) k1).compareTo(k2);
rlm@10 69 }
rlm@10 70 return -1;
rlm@10 71 }
rlm@10 72
rlm@10 73 static public int hash(Object o){
rlm@10 74 if(o == null)
rlm@10 75 return 0;
rlm@10 76 return o.hashCode();
rlm@10 77 }
rlm@10 78
rlm@10 79 static public int hashCombine(int seed, int hash){
rlm@10 80 //a la boost
rlm@10 81 seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
rlm@10 82 return seed;
rlm@10 83 }
rlm@10 84
rlm@10 85 static public boolean isPrimitive(Class c){
rlm@10 86 return c != null && c.isPrimitive() && !(c == Void.TYPE);
rlm@10 87 }
rlm@10 88
rlm@10 89 static public boolean isInteger(Object x){
rlm@10 90 return x instanceof Integer
rlm@10 91 || x instanceof Long
rlm@10 92 || x instanceof BigInteger;
rlm@10 93 }
rlm@10 94
rlm@10 95 static public Object ret1(Object ret, Object nil){
rlm@10 96 return ret;
rlm@10 97 }
rlm@10 98
rlm@10 99 static public ISeq ret1(ISeq ret, Object nil){
rlm@10 100 return ret;
rlm@10 101 }
rlm@10 102
rlm@10 103 static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, SoftReference<V>> cache){
rlm@10 104 //cleanup any dead entries
rlm@10 105 if(rq.poll() != null)
rlm@10 106 {
rlm@10 107 while(rq.poll() != null)
rlm@10 108 ;
rlm@10 109 for(Map.Entry<K, SoftReference<V>> e : cache.entrySet())
rlm@10 110 {
rlm@10 111 if(e.getValue().get() == null)
rlm@10 112 cache.remove(e.getKey(), e.getValue());
rlm@10 113 }
rlm@10 114 }
rlm@10 115 }
rlm@10 116 }