Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/eclipse/ui/internal/util/Util.java


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