Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
9:35cf337adfcf | 10:ef7dbbd6452c |
---|---|
1 /** | |
2 * Copyright (c) Rich Hickey. All rights reserved. | |
3 * The use and distribution terms for this software are covered by the | |
4 * 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 by | |
7 * the terms of this license. | |
8 * You must not remove this notice, or any other, from this software. | |
9 **/ | |
10 | |
11 /* rich Apr 19, 2008 */ | |
12 | |
13 package clojure.lang; | |
14 | |
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; | |
21 | |
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 } | |
36 | |
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 } | |
42 | |
43 static public boolean equals(Object k1, Object k2){ | |
44 if(k1 == k2) | |
45 return true; | |
46 return k1 != null && k1.equals(k2); | |
47 } | |
48 | |
49 static public boolean identical(Object k1, Object k2){ | |
50 return k1 == k2; | |
51 } | |
52 | |
53 static public Class classOf(Object x){ | |
54 if(x != null) | |
55 return x.getClass(); | |
56 return null; | |
57 } | |
58 | |
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 } | |
72 | |
73 static public int hash(Object o){ | |
74 if(o == null) | |
75 return 0; | |
76 return o.hashCode(); | |
77 } | |
78 | |
79 static public int hashCombine(int seed, int hash){ | |
80 //a la boost | |
81 seed ^= hash + 0x9e3779b9 + (seed << 6) + (seed >> 2); | |
82 return seed; | |
83 } | |
84 | |
85 static public boolean isPrimitive(Class c){ | |
86 return c != null && c.isPrimitive() && !(c == Void.TYPE); | |
87 } | |
88 | |
89 static public boolean isInteger(Object x){ | |
90 return x instanceof Integer | |
91 || x instanceof Long | |
92 || x instanceof BigInteger; | |
93 } | |
94 | |
95 static public Object ret1(Object ret, Object nil){ | |
96 return ret; | |
97 } | |
98 | |
99 static public ISeq ret1(ISeq ret, Object nil){ | |
100 return ret; | |
101 } | |
102 | |
103 static public <K,V> void clearCache(ReferenceQueue rq, ConcurrentHashMap<K, SoftReference<V>> cache){ | |
104 //cleanup any dead entries | |
105 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 } |