1 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 71 public class Utils { 72 77 public static final Enumeration EMPTY_ENUMERATION = new EmptyEnum(); 78 79 80 public static final Map EMPTY_MAP = new EmptyMap(); 81 82 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 101 public static Comparator nullLowOrder() { 102 return NULL_LOW_ORDER; 103 } 104 105 110 public static Comparator nullLowOrder(Comparator c) { 111 return new NullLowOrderC(c); 112 } 113 114 119 public static Comparator nullHighOrder() { 120 return NULL_HIGH_ORDER; 121 } 122 123 128 public static Comparator nullHighOrder(Comparator c) { 129 return new NullHighOrderC(c); 130 } 131 132 137 public static Comparator nullEqualOrder() { 138 return NULL_EQUAL_ORDER; 139 } 140 141 146 public static Comparator nullEqualOrder(Comparator c) { 147 return new NullEqualOrderC(c); 148 } 149 150 154 public static Comparator reverseOrder(Comparator c) { 155 return new ReverseOrderC(c); 156 } 157 158 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 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 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 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 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 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 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 } 380 | Popular Tags |