1 11 12 package org.eclipse.core.internal.commands.util; 13 14 import java.util.Collections ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.SortedMap ; 21 import java.util.SortedSet ; 22 import java.util.TreeMap ; 23 import java.util.TreeSet ; 24 25 30 public final class Util { 31 32 36 public final static SortedMap EMPTY_SORTED_MAP = Collections 37 .unmodifiableSortedMap(new TreeMap ()); 38 39 43 public final static SortedSet EMPTY_SORTED_SET = Collections 44 .unmodifiableSortedSet(new TreeSet ()); 45 46 50 public final static String ZERO_LENGTH_STRING = ""; 52 65 public static final void assertInstance(final Object object, final Class c, 66 final boolean allowNull) { 67 if (object == null && allowNull) { 68 return; 69 } 70 71 if (object == null || c == null) { 72 throw new NullPointerException (); 73 } else if (!c.isInstance(object)) { 74 throw new IllegalArgumentException (); 75 } 76 } 77 78 92 public static final int compare(final boolean left, final boolean right) { 93 return left == false ? (right == true ? -1 : 0) : (right == true ? 0 94 : 1); 95 } 96 97 112 public static final int compare(final Comparable left, 113 final Comparable right) { 114 if (left == null && right == null) { 115 return 0; 116 } else if (left == null) { 117 return -1; 118 } else if (right == null) { 119 return 1; 120 } else { 121 return left.compareTo(right); 122 } 123 } 124 125 136 public static final int compare(final int left, final int right) { 137 return left - right; 138 } 139 140 159 public static final int compare(final Object left, final Object right) { 160 if (left == null && right == null) { 161 return 0; 162 } else if (left == null) { 163 return -1; 164 } else if (right == null) { 165 return 1; 166 } else { 167 return left.toString().compareTo(right.toString()); 168 } 169 } 170 171 181 public static final boolean equals(final boolean left, final boolean right) { 182 return left == right; 183 } 184 185 196 public static final boolean equals(final Object left, final Object right) { 197 return left == null ? right == null : ((right != null) && left 198 .equals(right)); 199 } 200 201 216 public static final boolean equals(final Object [] leftArray, 217 final Object [] rightArray) { 218 if (leftArray == null) { 219 return (rightArray == null); 220 } else if (rightArray == null) { 221 return false; 222 } 223 224 if (leftArray.length != rightArray.length) { 225 return false; 226 } 227 228 for (int i = 0; i < leftArray.length; i++) { 229 final Object left = leftArray[i]; 230 final Object right = rightArray[i]; 231 final boolean equal = (left == null) ? (right == null) : (left 232 .equals(right)); 233 if (!equal) { 234 return false; 235 } 236 } 237 238 return true; 239 } 240 241 248 public static final int hashCode(final int i) { 249 return i; 250 } 251 252 262 public static final int hashCode(final Object object) { 263 return object != null ? object.hashCode() : 0; 264 } 265 266 285 public static final Map safeCopy(final Map map, final Class keyClass, 286 final Class valueClass, final boolean allowNullKeys, 287 final boolean allowNullValues) { 288 if (map == null || keyClass == null || valueClass == null) { 289 throw new NullPointerException (); 290 } 291 292 final Map copy = Collections.unmodifiableMap(new HashMap (map)); 293 final Iterator iterator = copy.entrySet().iterator(); 294 295 while (iterator.hasNext()) { 296 final Map.Entry entry = (Map.Entry ) iterator.next(); 297 assertInstance(entry.getKey(), keyClass, allowNullKeys); 298 assertInstance(entry.getValue(), valueClass, allowNullValues); 299 } 300 301 return map; 302 } 303 304 317 public static final Set safeCopy(final Set set, final Class c) { 318 return safeCopy(set, c, false); 319 } 320 321 335 public static final Set safeCopy(final Set set, final Class c, 336 final boolean allowNullElements) { 337 if (set == null || c == null) { 338 throw new NullPointerException (); 339 } 340 341 final Set copy = Collections.unmodifiableSet(new HashSet (set)); 342 final Iterator iterator = copy.iterator(); 343 344 while (iterator.hasNext()) { 345 assertInstance(iterator.next(), c, allowNullElements); 346 } 347 348 return set; 349 } 350 351 354 private Util() { 355 } 357 } 358 | Popular Tags |