Mercurial > hg > blitz_condensed
comparison src/com/go/trove/util/Utils.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* ==================================================================== | |
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group | |
3 * ==================================================================== | |
4 * The Tea Software License, Version 1.1 | |
5 * | |
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * | |
12 * 1. Redistributions of source code must retain the above copyright | |
13 * notice, this list of conditions and the following disclaimer. | |
14 * | |
15 * 2. Redistributions in binary form must reproduce the above copyright | |
16 * notice, this list of conditions and the following disclaimer in | |
17 * the documentation and/or other materials provided with the | |
18 * distribution. | |
19 * | |
20 * 3. The end-user documentation included with the redistribution, | |
21 * if any, must include the following acknowledgment: | |
22 * "This product includes software developed by the | |
23 * Walt Disney Internet Group (http://opensource.go.com/)." | |
24 * Alternately, this acknowledgment may appear in the software itself, | |
25 * if and wherever such third-party acknowledgments normally appear. | |
26 * | |
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must | |
28 * not be used to endorse or promote products derived from this | |
29 * software without prior written permission. For written | |
30 * permission, please contact opensource@dig.com. | |
31 * | |
32 * 5. Products derived from this software may not be called "Tea", | |
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet", | |
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior | |
35 * written permission of the Walt Disney Internet Group. | |
36 * | |
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS | |
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 * ==================================================================== | |
49 * | |
50 * For more information about Tea, please see http://opensource.go.com/. | |
51 */ | |
52 | |
53 package com.go.trove.util; | |
54 | |
55 import java.util.*; | |
56 import java.io.Serializable; | |
57 import java.io.ObjectStreamException; | |
58 import java.lang.reflect.InvocationTargetException; | |
59 import java.lang.reflect.Method; | |
60 import java.lang.ref.Reference; | |
61 import java.lang.ref.WeakReference; | |
62 | |
63 /****************************************************************************** | |
64 * Some generic utilities. | |
65 * | |
66 * @author Brian S O'Neill | |
67 * @version | |
68 * <!--$$Revision:--> 28 <!-- $-->, <!--$$JustDate:--> 00/12/18 <!-- $--> | |
69 * @see java.util.Collections | |
70 */ | |
71 public class Utils { | |
72 // The choice of determining whether these constants are public or | |
73 // private is based on the design of the java.util.Collections class. | |
74 // For consistency, the "empty" fields are public, and the comparators are | |
75 // accessed from static methods. | |
76 | |
77 public static final Enumeration EMPTY_ENUMERATION = new EmptyEnum(); | |
78 | |
79 /** This Map is already provided in JDK1.3. */ | |
80 public static final Map EMPTY_MAP = new EmptyMap(); | |
81 | |
82 /** | |
83 * Similar to EMPTY_MAP, except the put operation is supported, but the | |
84 * values are immediately forgotten. | |
85 */ | |
86 public static final Map VOID_MAP = new VoidMap(); | |
87 | |
88 private static final Comparator NULL_LOW_ORDER = new NullLowOrder(); | |
89 | |
90 private static final Comparator NULL_HIGH_ORDER = new NullHighOrder(); | |
91 | |
92 private static final Comparator NULL_EQUAL_ORDER = new NullEqualOrder(); | |
93 | |
94 private static FlyweightSet cFlyweightSet; | |
95 | |
96 /** | |
97 * Returns a Comparator that uses a Comparable object's natural ordering, | |
98 * except null values are always considered low order. This Comparator | |
99 * allows naturally ordered TreeMaps to support null values. | |
100 */ | |
101 public static Comparator nullLowOrder() { | |
102 return NULL_LOW_ORDER; | |
103 } | |
104 | |
105 /** | |
106 * Returns a Comparator that wraps the given Comparator except null values | |
107 * are always considered low order. This fixes Comparators that don't | |
108 * support comparisons against null and allows them to be used in TreeMaps. | |
109 */ | |
110 public static Comparator nullLowOrder(Comparator c) { | |
111 return new NullLowOrderC(c); | |
112 } | |
113 | |
114 /** | |
115 * Returns a Comparator that uses a Comparable object's natural ordering, | |
116 * except null values are always considered high order. This Comparator | |
117 * allows naturally ordered TreeMaps to support null values. | |
118 */ | |
119 public static Comparator nullHighOrder() { | |
120 return NULL_HIGH_ORDER; | |
121 } | |
122 | |
123 /** | |
124 * Returns a Comparator that wraps the given Comparator except null values | |
125 * are always considered high order. This fixes Comparators that don't | |
126 * support comparisons against null and allows them to be used in TreeMaps. | |
127 */ | |
128 public static Comparator nullHighOrder(Comparator c) { | |
129 return new NullHighOrderC(c); | |
130 } | |
131 | |
132 /** | |
133 * Returns a Comparator that uses a Comparable object's natural ordering, | |
134 * except null values are always considered equal order. This Comparator | |
135 * should not be used in a TreeMap, but can be used in a sorter. | |
136 */ | |
137 public static Comparator nullEqualOrder() { | |
138 return NULL_EQUAL_ORDER; | |
139 } | |
140 | |
141 /** | |
142 * Returns a Comparator that wraps the given Comparator except null values | |
143 * are always considered equal order. This Comparator should not be used in | |
144 * a TreeMap, but can be used in a sorter. | |
145 */ | |
146 public static Comparator nullEqualOrder(Comparator c) { | |
147 return new NullEqualOrderC(c); | |
148 } | |
149 | |
150 /** | |
151 * Returns a Comparator that wraps the given Comparator, but orders in | |
152 * reverse. | |
153 */ | |
154 public static Comparator reverseOrder(Comparator c) { | |
155 return new ReverseOrderC(c); | |
156 } | |
157 | |
158 /** | |
159 * Just like {@link String#intern() String.intern}, except it generates | |
160 * flyweights for any kind of object, and it does not prevent them from | |
161 * being garbage collected. Calling intern on a String does not use the | |
162 * same String pool used by String.intern because those Strings are not | |
163 * always garbage collected. Some virtual machines free up Strings from the | |
164 * interned String pool, others do not. | |
165 * <p> | |
166 * For objects that do not customize the hashCode and equals methods, | |
167 * calling intern is not very useful because the object returned will | |
168 * always be the same as the one passed in. | |
169 * <p> | |
170 * The object type returned from intern is guaranteed to be exactly the | |
171 * same type as the one passed in. Calling intern on null returns null. | |
172 * | |
173 * @param obj Object to intern | |
174 * @return Interned object. | |
175 * @see FlyweightSet | |
176 */ | |
177 public static Object intern(Object obj) { | |
178 FlyweightSet set; | |
179 if ((set = cFlyweightSet) == null) { | |
180 synchronized (Utils.class) { | |
181 if ((set = cFlyweightSet) == null) { | |
182 set = new FlyweightSet(); | |
183 } | |
184 } | |
185 cFlyweightSet = set; | |
186 } | |
187 return set.put(obj); | |
188 } | |
189 | |
190 protected Utils() { | |
191 } | |
192 | |
193 private static class EmptyEnum implements Enumeration, Serializable { | |
194 public boolean hasMoreElements() { | |
195 return false; | |
196 } | |
197 | |
198 public Object nextElement() throws NoSuchElementException { | |
199 throw new NoSuchElementException(); | |
200 } | |
201 | |
202 // Serializable singleton classes should always do this. | |
203 private Object readResolve() throws ObjectStreamException { | |
204 return EMPTY_ENUMERATION; | |
205 } | |
206 }; | |
207 | |
208 private static class EmptyMap implements Map, Serializable { | |
209 public int size() { | |
210 return 0; | |
211 } | |
212 | |
213 public boolean isEmpty() { | |
214 return true; | |
215 } | |
216 | |
217 public boolean containsKey(Object key) { | |
218 return false; | |
219 } | |
220 | |
221 public boolean containsValue(Object value) { | |
222 return false; | |
223 } | |
224 | |
225 public Object get(Object key) { | |
226 return null; | |
227 } | |
228 | |
229 public Object put(Object key, Object value) { | |
230 throw new UnsupportedOperationException | |
231 ("Cannot put into immutable empty map"); | |
232 } | |
233 | |
234 public Object remove(Object key) { | |
235 return null; | |
236 } | |
237 | |
238 public void putAll(Map map) { | |
239 throw new UnsupportedOperationException | |
240 ("Cannot put into immutable empty map"); | |
241 } | |
242 | |
243 public void clear() { | |
244 } | |
245 | |
246 public Set keySet() { | |
247 return Collections.EMPTY_SET; | |
248 } | |
249 | |
250 public Collection values() { | |
251 return Collections.EMPTY_LIST; | |
252 } | |
253 | |
254 public Set entrySet() { | |
255 return Collections.EMPTY_SET; | |
256 } | |
257 | |
258 // Serializable singleton classes should always do this. | |
259 private Object readResolve() throws ObjectStreamException { | |
260 return EMPTY_MAP; | |
261 } | |
262 } | |
263 | |
264 private static class VoidMap extends EmptyMap { | |
265 public Object put(Object key, Object value) { | |
266 return null; | |
267 } | |
268 | |
269 public void putAll(Map map) { | |
270 } | |
271 | |
272 // Serializable singleton classes should always do this. | |
273 private Object readResolve() throws ObjectStreamException { | |
274 return VOID_MAP; | |
275 } | |
276 } | |
277 | |
278 private static class NullLowOrder implements Comparator, Serializable { | |
279 public int compare(Object obj1, Object obj2) { | |
280 if (obj1 != null) { | |
281 return (obj2 != null) ? ((Comparable)obj1).compareTo(obj2) : 1; | |
282 } | |
283 else { | |
284 return (obj2 != null) ? -1 : 0; | |
285 } | |
286 } | |
287 | |
288 // Serializable singleton classes should always do this. | |
289 private Object readResolve() throws ObjectStreamException { | |
290 return NULL_LOW_ORDER; | |
291 } | |
292 } | |
293 | |
294 private static class NullLowOrderC implements Comparator, Serializable { | |
295 private Comparator c; | |
296 | |
297 public NullLowOrderC(Comparator c) { | |
298 this.c = c; | |
299 } | |
300 | |
301 public int compare(Object obj1, Object obj2) { | |
302 if (obj1 != null) { | |
303 return (obj2 != null) ? c.compare(obj1, obj2) : 1; | |
304 } | |
305 else { | |
306 return (obj2 != null) ? -1 : 0; | |
307 } | |
308 } | |
309 } | |
310 | |
311 private static class NullHighOrder implements Comparator, Serializable { | |
312 public int compare(Object obj1, Object obj2) { | |
313 if (obj1 != null) { | |
314 return (obj2 != null) ? ((Comparable)obj1).compareTo(obj2): -1; | |
315 } | |
316 else { | |
317 return (obj2 != null) ? 1 : 0; | |
318 } | |
319 } | |
320 | |
321 // Serializable singleton classes should always do this. | |
322 private Object readResolve() throws ObjectStreamException { | |
323 return NULL_HIGH_ORDER; | |
324 } | |
325 } | |
326 | |
327 private static class NullHighOrderC implements Comparator, Serializable { | |
328 private Comparator c; | |
329 | |
330 public NullHighOrderC(Comparator c) { | |
331 this.c = c; | |
332 } | |
333 | |
334 public int compare(Object obj1, Object obj2) { | |
335 if (obj1 != null) { | |
336 return (obj2 != null) ? c.compare(obj1, obj2) : -1; | |
337 } | |
338 else { | |
339 return (obj2 != null) ? 1 : 0; | |
340 } | |
341 } | |
342 } | |
343 | |
344 private static class NullEqualOrder implements Comparator, Serializable { | |
345 public int compare(Object obj1, Object obj2) { | |
346 return (obj1 != null && obj2 != null) ? | |
347 ((Comparable)obj1).compareTo(obj2) : 0; | |
348 } | |
349 | |
350 // Serializable singleton classes should always do this. | |
351 private Object readResolve() throws ObjectStreamException { | |
352 return NULL_EQUAL_ORDER; | |
353 } | |
354 } | |
355 | |
356 private static class NullEqualOrderC implements Comparator, Serializable { | |
357 private Comparator c; | |
358 | |
359 public NullEqualOrderC(Comparator c) { | |
360 this.c = c; | |
361 } | |
362 | |
363 public int compare(Object obj1, Object obj2) { | |
364 return (obj1 != null && obj2 != null) ? c.compare(obj1, obj2) : 0; | |
365 } | |
366 } | |
367 | |
368 private static class ReverseOrderC implements Comparator, Serializable { | |
369 private Comparator c; | |
370 | |
371 public ReverseOrderC(Comparator c) { | |
372 this.c = c; | |
373 } | |
374 | |
375 public int compare(Object obj1, Object obj2) { | |
376 return c.compare(obj2, obj1); | |
377 } | |
378 } | |
379 } |