1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
|
/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.ui.internal.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashPipe;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public final class Util {
public final static SortedMap EMPTY_SORTED_MAP = Collections
.unmodifiableSortedMap(new TreeMap());
public final static SortedSet EMPTY_SORTED_SET = Collections
.unmodifiableSortedSet(new TreeSet());
public final static String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$
/**
* Ensures that a string is not null. Converts null strings into empty
* strings, and leaves any other string unmodified. Use this to help
* wrap calls to methods that return null instead of the empty string.
* Can also help protect against implementation errors in methods that
* are not supposed to return null.
*
* @param input input string (may be null)
* @return input if not null, or the empty string if input is null
*/
public static String safeString(String input) {
if (input != null) {
return input;
}
return ZERO_LENGTH_STRING;
}
public static void assertInstance(Object object, Class c) {
assertInstance(object, c, false);
}
public static void assertInstance(Object object, Class c, boolean allowNull) {
if (object == null && allowNull) return;
if (object == null || c == null)
throw new NullPointerException();
else if (!c.isInstance(object)) throw new IllegalArgumentException();
}
public static int compare(boolean left, boolean right) {
return left == false ? (right == true ? -1 : 0) : 1;
}
public static int compare(Comparable left, Comparable right) {
if (left == null && right == null)
return 0;
else if (left == null)
return -1;
else if (right == null)
return 1;
else
return left.compareTo(right);
}
public static int compare(Comparable[] left, Comparable[] right) {
if (left == null && right == null)
return 0;
else if (left == null)
return -1;
else if (right == null)
return 1;
else {
int l = left.length;
int r = right.length;
if (l != r)
return l - r;
else {
for (int i = 0; i < l; i++) {
int compareTo = compare(left[i], right[i]);
if (compareTo != 0) return compareTo;
}
return 0;
}
}
}
public static int compare(int left, int right) {
return left - right;
}
public static int compare(List left, List right) {
if (left == null && right == null)
return 0;
else if (left == null)
return -1;
else if (right == null)
return 1;
else {
int l = left.size();
int r = right.size();
if (l != r)
return l - r;
else {
for (int i = 0; i < l; i++) {
int compareTo = compare((Comparable) left.get(i),
(Comparable) right.get(i));
if (compareTo != 0) return compareTo;
}
return 0;
}
}
}
public static int compare(Object left, Object right) {
if (left == null && right == null)
return 0;
else if (left == null)
return -1;
else if (right == null)
return 1;
else
return left.toString().compareTo(right.toString());
}
public static void diff(Map left, Map right, Set leftOnly, Set different,
Set rightOnly) {
if (left == null || right == null || leftOnly == null
|| different == null || rightOnly == null)
throw new NullPointerException();
Iterator iterator = left.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
if (!right.containsKey(key))
leftOnly.add(key);
else if (!Util.equals(left.get(key), right.get(key)))
different.add(key);
}
iterator = right.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
if (!left.containsKey(key)) rightOnly.add(key);
}
}
public static void diff(Set left, Set right, Set leftOnly, Set rightOnly) {
if (left == null || right == null || leftOnly == null
|| rightOnly == null) throw new NullPointerException();
Iterator iterator = left.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
if (!right.contains(object)) leftOnly.add(object);
}
iterator = right.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
if (!left.contains(object)) rightOnly.add(object);
}
}
public static boolean endsWith(List left, List right, boolean equals) {
if (left == null || right == null)
return false;
else {
int l = left.size();
int r = right.size();
if (r > l || !equals && r == l) return false;
for (int i = 0; i < r; i++)
if (!equals(left.get(l - i - 1), right.get(r - i - 1)))
return false;
return true;
}
}
public static boolean endsWith(Object[] left, Object[] right, boolean equals) {
if (left == null || right == null)
return false;
else {
int l = left.length;
int r = right.length;
if (r > l || !equals && r == l) return false;
for (int i = 0; i < r; i++)
if (!equals(left[l - i - 1], right[r - i - 1])) return false;
return true;
}
}
public static boolean equals(boolean left, boolean right) {
return left == right;
}
public static boolean equals(int left, int right) {
return left == right;
}
public static boolean equals(Object left, Object right) {
return left == null ? right == null : ((right != null) && left
.equals(right));
}
public static int hashCode(boolean b) {
return b ? Boolean.TRUE.hashCode() : Boolean.FALSE.hashCode();
}
public static int hashCode(int i) {
return i;
}
public static int hashCode(Object object) {
return object != null ? object.hashCode() : 0;
}
public static Collection safeCopy(Collection collection, Class c) {
return safeCopy(collection, c, false);
}
public static Collection safeCopy(Collection collection, Class c,
boolean allowNullElements) {
if (collection == null || c == null) throw new NullPointerException();
collection = Collections.unmodifiableCollection(new ArrayList(collection));
Iterator iterator = collection.iterator();
while (iterator.hasNext())
assertInstance(iterator.next(), c, allowNullElements);
return collection;
}
public static List safeCopy(List list, Class c) {
return safeCopy(list, c, false);
}
public static List safeCopy(List list, Class c, boolean allowNullElements) {
if (list == null || c == null) throw new NullPointerException();
list = Collections.unmodifiableList(new ArrayList(list));
Iterator iterator = list.iterator();
while (iterator.hasNext())
assertInstance(iterator.next(), c, allowNullElements);
return list;
}
public static Map safeCopy(Map map, Class keyClass, Class valueClass) {
return safeCopy(map, keyClass, valueClass, false, false);
}
public static Map safeCopy(Map map, Class keyClass, Class valueClass,
boolean allowNullKeys, boolean allowNullValues) {
if (map == null || keyClass == null || valueClass == null)
throw new NullPointerException();
map = Collections.unmodifiableMap(new HashMap(map));
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
assertInstance(entry.getKey(), keyClass, allowNullKeys);
assertInstance(entry.getValue(), valueClass, allowNullValues);
}
return map;
}
public static Set safeCopy(Set set, Class c) {
return safeCopy(set, c, false);
}
public static Set safeCopy(Set set, Class c, boolean allowNullElements) {
if (set == null || c == null) throw new NullPointerException();
set = Collections.unmodifiableSet(new HashSet(set));
Iterator iterator = set.iterator();
while (iterator.hasNext())
assertInstance(iterator.next(), c, allowNullElements);
return set;
}
public static SortedMap safeCopy(SortedMap sortedMap, Class keyClass,
Class valueClass) {
return safeCopy(sortedMap, keyClass, valueClass, false, false);
}
public static SortedMap safeCopy(SortedMap sortedMap, Class keyClass,
Class valueClass, boolean allowNullKeys, boolean allowNullValues) {
if (sortedMap == null || keyClass == null || valueClass == null)
throw new NullPointerException();
sortedMap = Collections.unmodifiableSortedMap(new TreeMap(sortedMap));
Iterator iterator = sortedMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
assertInstance(entry.getKey(), keyClass, allowNullKeys);
assertInstance(entry.getValue(), valueClass, allowNullValues);
}
return sortedMap;
}
public static SortedSet safeCopy(SortedSet sortedSet, Class c) {
return safeCopy(sortedSet, c, false);
}
public static SortedSet safeCopy(SortedSet sortedSet, Class c,
boolean allowNullElements) {
if (sortedSet == null || c == null) throw new NullPointerException();
sortedSet = Collections.unmodifiableSortedSet(new TreeSet(sortedSet));
Iterator iterator = sortedSet.iterator();
while (iterator.hasNext())
assertInstance(iterator.next(), c, allowNullElements);
return sortedSet;
}
public static boolean startsWith(List left, List right, boolean equals) {
if (left == null || right == null)
return false;
else {
int l = left.size();
int r = right.size();
if (r > l || !equals && r == l) return false;
for (int i = 0; i < r; i++)
if (!equals(left.get(i), right.get(i))) return false;
return true;
}
}
public static boolean startsWith(Object[] left, Object[] right,
boolean equals) {
if (left == null || right == null)
return false;
else {
int l = left.length;
int r = right.length;
if (r > l || !equals && r == l) return false;
for (int i = 0; i < r; i++)
if (!equals(left[i], right[i])) return false;
return true;
}
}
public static String translateString(ResourceBundle resourceBundle,
String key) {
return Util.translateString(resourceBundle, key, key, true, true);
}
public static String translateString(ResourceBundle resourceBundle,
String key, String string, boolean signal, boolean trim) {
if (resourceBundle != null && key != null)
try {
final String translatedString = resourceBundle
.getString(key);
if (translatedString != null)
return trim ? translatedString.trim()
: translatedString;
} catch (MissingResourceException eMissingResource) {
if (signal) System.err.println(eMissingResource);
}
return trim ? string.trim() : string;
}
private Util() {
}
}
|