diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/lang/Util.java	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,116 @@
     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 Apr 19, 2008 */
    1.15 +
    1.16 +package clojure.lang;
    1.17 +
    1.18 +import java.math.BigInteger;
    1.19 +import java.util.Map;
    1.20 +import java.util.concurrent.ConcurrentHashMap;
    1.21 +import java.lang.ref.SoftReference;
    1.22 +import java.lang.ref.ReferenceQueue;
    1.23 +import java.lang.ref.Reference;
    1.24 +
    1.25 +public class Util{
    1.26 +static public boolean equiv(Object k1, Object k2){
    1.27 +	if(k1 == k2)
    1.28 +		return true;
    1.29 +	if(k1 != null)
    1.30 +		{
    1.31 +		if(k1 instanceof Number && k2 instanceof Number)
    1.32 +			return Numbers.equiv(k1, k2);
    1.33 +		else if(k1 instanceof IPersistentCollection || k2 instanceof IPersistentCollection)
    1.34 +			return pcequiv(k1,k2);
    1.35 +		return k1.equals(k2);
    1.36 +		}
    1.37 +	return false;
    1.38 +}
    1.39 +
    1.40 +static public boolean pcequiv(Object k1, Object k2){
    1.41 +	if(k1 instanceof IPersistentCollection)
    1.42 +		return ((IPersistentCollection)k1).equiv(k2);
    1.43 +	return ((IPersistentCollection)k2).equiv(k1);
    1.44 +}
    1.45 +
    1.46 +static public boolean equals(Object k1, Object k2){
    1.47 +	if(k1 == k2)
    1.48 +		return true;
    1.49 +	return k1 != null && k1.equals(k2);
    1.50 +}
    1.51 +
    1.52 +static public boolean identical(Object k1, Object k2){
    1.53 +	return k1 == k2;
    1.54 +}
    1.55 +
    1.56 +static public Class classOf(Object x){
    1.57 +	if(x != null)
    1.58 +		return x.getClass();
    1.59 +	return null;
    1.60 +}
    1.61 +
    1.62 +static public int compare(Object k1, Object k2){
    1.63 +	if(k1 == k2)
    1.64 +		return 0;
    1.65 +	if(k1 != null)
    1.66 +		{
    1.67 +		if(k2 == null)
    1.68 +			return 1;
    1.69 +		if(k1 instanceof Number)
    1.70 +			return Numbers.compare((Number) k1, (Number) k2);
    1.71 +		return ((Comparable) k1).compareTo(k2);
    1.72 +		}
    1.73 +	return -1;
    1.74 +}
    1.75 +
    1.76 +static public int hash(Object o){
    1.77 +	if(o == null)
    1.78 +		return 0;
    1.79 +	return o.hashCode();
    1.80 +}
    1.81 +
    1.82 +static public int hashCombine(int seed, int hash){
    1.83 +	//a la boost
    1.84 +	seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    1.85 +	return seed;
    1.86 +}
    1.87 +
    1.88 +static public boolean isPrimitive(Class c){
    1.89 +	return c != null && c.isPrimitive() && !(c == Void.TYPE);
    1.90 +}
    1.91 +
    1.92 +static public boolean isInteger(Object x){
    1.93 +	return x instanceof Integer
    1.94 +			|| x instanceof Long
    1.95 +			|| x instanceof BigInteger;
    1.96 +}
    1.97 +
    1.98 +static public Object ret1(Object ret, Object nil){
    1.99 +		return ret;
   1.100 +}
   1.101 +
   1.102 +static public ISeq ret1(ISeq ret, Object nil){
   1.103 +		return ret;
   1.104 +}
   1.105 +
   1.106 +static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, SoftReference<V>> cache){
   1.107 +		//cleanup any dead entries
   1.108 +	if(rq.poll() != null)
   1.109 +		{
   1.110 +		while(rq.poll() != null)
   1.111 +			;
   1.112 +		for(Map.Entry<K, SoftReference<V>> e : cache.entrySet())
   1.113 +			{
   1.114 +			if(e.getValue().get() == null)
   1.115 +				cache.remove(e.getKey(), e.getValue());
   1.116 +			}
   1.117 +		}
   1.118 +}
   1.119 +}