Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/util/Util.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 52184e4b815c |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.jface.util.Util; | |
15 | |
16 import java.util.Collections; | |
17 import java.util.List; | |
18 import java.util.MissingResourceException; | |
19 import java.util.ResourceBundle; | |
20 import java.util.SortedSet; | |
21 import java.util.TreeSet; | |
22 | |
23 import java.lang.all; | |
24 import java.util.Set; | |
25 import tango.core.Exception; | |
26 private extern(C) int _d_isbaseof(ClassInfo *b, ClassInfo *c); | |
27 | |
28 /** | |
29 * <p> | |
30 * A static class providing utility methods to all of JFace. | |
31 * </p> | |
32 * | |
33 * @since 3.1 | |
34 */ | |
35 public final class Util { | |
36 | |
37 // /** | |
38 // * An unmodifiable, empty, sorted set. This value is guaranteed to never | |
39 // * change and never be <code>null</code>. | |
40 // */ | |
41 // public static final SortedSet EMPTY_SORTED_SET = Collections | |
42 // .unmodifiableSortedSet(new TreeSet()); | |
43 | |
44 /** | |
45 * A common zero-length string. It avoids needing write <code>NON-NLS</code> | |
46 * next to code fragments. It's also a bit clearer to read. | |
47 */ | |
48 public static final String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$ | |
49 | |
50 /** | |
51 * Verifies that the given object is an instance of the given class. | |
52 * | |
53 * @param object | |
54 * The object to check; may be <code>null</code>. | |
55 * @param c | |
56 * The class which the object should be; must not be | |
57 * <code>null</code>. | |
58 */ | |
59 public static final void assertInstance(Object object, ClassInfo c) { | |
60 assertInstance(object, c, false); | |
61 } | |
62 | |
63 /** | |
64 * Verifies the given object is an instance of the given class. It is | |
65 * possible to specify whether the object is permitted to be | |
66 * <code>null</code>. | |
67 * | |
68 * @param object | |
69 * The object to check; may be <code>null</code>. | |
70 * @param c | |
71 * The class which the object should be; must not be | |
72 * <code>null</code>. | |
73 * @param allowNull | |
74 * Whether the object is allowed to be <code>null</code>. | |
75 */ | |
76 private static final void assertInstance(Object object, | |
77 ClassInfo c, bool allowNull) { | |
78 if (object is null && allowNull) { | |
79 return; | |
80 } | |
81 | |
82 if (object is null || c is null) { | |
83 throw new NullPointerException(); | |
84 } else if (!_d_isbaseof( &object.classinfo, &c ) ) { | |
85 throw new IllegalArgumentException(null); | |
86 } | |
87 } | |
88 | |
89 /** | |
90 * Compares two bool values. <code>false</code> is considered to be | |
91 * "less than" <code>true</code>. | |
92 * | |
93 * @param left | |
94 * The left value to compare | |
95 * @param right | |
96 * The right value to compare | |
97 * @return <code>-1</code> if the left is <code>false</code> and the | |
98 * right is <code>true</code>. <code>1</code> if the opposite | |
99 * is true. If they are equal, then it returns <code>0</code>. | |
100 */ | |
101 public static final int compare(bool left, bool right) { | |
102 return left is false ? (right is true ? -1 : 0) : 1; | |
103 } | |
104 | |
105 /** | |
106 * Compares two integer values. | |
107 * | |
108 * @param left | |
109 * The left value to compare | |
110 * @param right | |
111 * The right value to compare | |
112 * @return <code>left - right</code> | |
113 */ | |
114 public static final int compare(int left, int right) { | |
115 return left - right; | |
116 } | |
117 | |
118 /** | |
119 * Compares to comparable objects -- defending against <code>null</code>. | |
120 * | |
121 * @param left | |
122 * The left object to compare; may be <code>null</code>. | |
123 * @param right | |
124 * The right object to compare; may be <code>null</code>. | |
125 * @return The result of the comparison. <code>null</code> is considered | |
126 * to be the least possible value. | |
127 */ | |
128 public static final int compare(Comparable left, | |
129 Comparable right) { | |
130 if (left is null && right is null) { | |
131 return 0; | |
132 } else if (left is null) { | |
133 return -1; | |
134 } else if (right is null) { | |
135 return 1; | |
136 } else { | |
137 return left.compareTo(cast(Object)right); | |
138 } | |
139 } | |
140 | |
141 /** | |
142 * Compares two arrays of comparable objects -- accounting for | |
143 * <code>null</code>. | |
144 * | |
145 * @param left | |
146 * The left array to be compared; may be <code>null</code>. | |
147 * @param right | |
148 * The right array to be compared; may be <code>null</code>. | |
149 * @return The result of the comparison. <code>null</code> is considered | |
150 * to be the least possible value. A shorter array is considered | |
151 * less than a longer array. | |
152 */ | |
153 public static final int compare(Comparable[] left, | |
154 Comparable[] right) { | |
155 if (left is null && right is null) { | |
156 return 0; | |
157 } else if (left is null) { | |
158 return -1; | |
159 } else if (right is null) { | |
160 return 1; | |
161 } else { | |
162 int l = left.length; | |
163 int r = right.length; | |
164 | |
165 if (l !is r) { | |
166 return l - r; | |
167 } | |
168 | |
169 for (int i = 0; i < l; i++) { | |
170 int compareTo = compare(left[i], right[i]); | |
171 | |
172 if (compareTo !is 0) { | |
173 return compareTo; | |
174 } | |
175 } | |
176 | |
177 return 0; | |
178 } | |
179 } | |
180 public static final int compare(String left, String right) { | |
181 return left < right; | |
182 } | |
183 | |
184 // /** | |
185 // * Compares two lists -- account for <code>null</code>. The lists must | |
186 // * contain comparable objects. | |
187 // * | |
188 // * @param left | |
189 // * The left list to compare; may be <code>null</code>. This | |
190 // * list must only contain instances of <code>Comparable</code>. | |
191 // * @param right | |
192 // * The right list to compare; may be <code>null</code>. This | |
193 // * list must only contain instances of <code>Comparable</code>. | |
194 // * @return The result of the comparison. <code>null</code> is considered | |
195 // * to be the least possible value. A shorter list is considered less | |
196 // * than a longer list. | |
197 // */ | |
198 // public static final int compare(SeqView!(Object) left, SeqView!(Object) right) { | |
199 // if (left is null && right is null) { | |
200 // return 0; | |
201 // } else if (left is null) { | |
202 // return -1; | |
203 // } else if (right is null) { | |
204 // return 1; | |
205 // } else { | |
206 // int l = left.size(); | |
207 // int r = right.size(); | |
208 // | |
209 // if (l !is r) { | |
210 // return l - r; | |
211 // } | |
212 // | |
213 // for (int i = 0; i < l; i++) { | |
214 // int compareTo = compare((Comparable) left.get(i), | |
215 // (Comparable) right.get(i)); | |
216 // | |
217 // if (compareTo !is 0) { | |
218 // return compareTo; | |
219 // } | |
220 // } | |
221 // | |
222 // return 0; | |
223 // } | |
224 // } | |
225 | |
226 /** | |
227 * Tests whether the first array ends with the second array. | |
228 * | |
229 * @param left | |
230 * The array to check (larger); may be <code>null</code>. | |
231 * @param right | |
232 * The array that should be a subsequence (smaller); may be | |
233 * <code>null</code>. | |
234 * @param equals | |
235 * Whether the two array are allowed to be equal. | |
236 * @return <code>true</code> if the second array is a subsequence of the | |
237 * array list, and they share end elements. | |
238 */ | |
239 public static final bool endsWith(Object[] left, | |
240 Object[] right, bool equals) { | |
241 if (left is null || right is null) { | |
242 return false; | |
243 } | |
244 | |
245 int l = left.length; | |
246 int r = right.length; | |
247 | |
248 if (r > l || !equals && r is l) { | |
249 return false; | |
250 } | |
251 | |
252 for (int i = 0; i < r; i++) { | |
253 if (!Util.opEquals(left[l - i - 1], right[r - i - 1])) { | |
254 return false; | |
255 } | |
256 } | |
257 | |
258 return true; | |
259 } | |
260 | |
261 /** | |
262 * Checks whether the two objects are <code>null</code> -- allowing for | |
263 * <code>null</code>. | |
264 * | |
265 * @param left | |
266 * The left object to compare; may be <code>null</code>. | |
267 * @param right | |
268 * The right object to compare; may be <code>null</code>. | |
269 * @return <code>true</code> if the two objects are equivalent; | |
270 * <code>false</code> otherwise. | |
271 */ | |
272 public static final bool opEquals(Object left, Object right) { | |
273 return left is null ? right is null : ((right !is null) && left | |
274 .opEquals(right)); | |
275 } | |
276 public static final bool opEquals(String left, String right) { | |
277 return left == right; | |
278 } | |
279 | |
280 /** | |
281 * Tests whether two arrays of objects are equal to each other. The arrays | |
282 * must not be <code>null</code>, but their elements may be | |
283 * <code>null</code>. | |
284 * | |
285 * @param leftArray | |
286 * The left array to compare; may be <code>null</code>, and | |
287 * may be empty and may contain <code>null</code> elements. | |
288 * @param rightArray | |
289 * The right array to compare; may be <code>null</code>, and | |
290 * may be empty and may contain <code>null</code> elements. | |
291 * @return <code>true</code> if the arrays are equal length and the | |
292 * elements at the same position are equal; <code>false</code> | |
293 * otherwise. | |
294 */ | |
295 public static final bool opEquals(Object[] leftArray, | |
296 Object[] rightArray) { | |
297 if (leftArray is rightArray) { | |
298 return true; | |
299 } | |
300 | |
301 if (leftArray is null) { | |
302 return (rightArray is null); | |
303 } else if (rightArray is null) { | |
304 return false; | |
305 } | |
306 | |
307 if (leftArray.length !is rightArray.length) { | |
308 return false; | |
309 } | |
310 | |
311 for (int i = 0; i < leftArray.length; i++) { | |
312 Object left = leftArray[i]; | |
313 Object right = rightArray[i]; | |
314 bool equal = ((left is null) ? (right is null) : (left.opEquals(right))) !is 0; | |
315 if (!equal) { | |
316 return false; | |
317 } | |
318 } | |
319 | |
320 return true; | |
321 } | |
322 | |
323 public static final bool opEquals(String[] leftArray, String[] rightArray) { | |
324 if (leftArray.length !is rightArray.length) { | |
325 return false; | |
326 } | |
327 | |
328 for (int i = 0; i < leftArray.length; i++) { | |
329 String left = leftArray[i]; | |
330 String right = rightArray[i]; | |
331 if (left != right) { | |
332 return false; | |
333 } | |
334 } | |
335 | |
336 return true; | |
337 } | |
338 | |
339 /** | |
340 * Provides a hash code based on the given integer value. | |
341 * | |
342 * @param i | |
343 * The integer value | |
344 * @return <code>i</code> | |
345 */ | |
346 public static final hash_t toHash(int i) { | |
347 return i; | |
348 } | |
349 | |
350 /** | |
351 * Provides a hash code for the object -- defending against | |
352 * <code>null</code>. | |
353 * | |
354 * @param object | |
355 * The object for which a hash code is required. | |
356 * @return <code>object.hashCode</code> or <code>0</code> if | |
357 * <code>object</code> if <code>null</code>. | |
358 */ | |
359 public static final hash_t toHash( Object object) { | |
360 return object !is null ? object.toHash() : 0; | |
361 } | |
362 | |
363 /** | |
364 * Computes the hash code for an array of objects, but with defense against | |
365 * <code>null</code>. | |
366 * | |
367 * @param objects | |
368 * The array of objects for which a hash code is needed; may be | |
369 * <code>null</code>. | |
370 * @return The hash code for <code>objects</code>; or <code>0</code> if | |
371 * <code>objects</code> is <code>null</code>. | |
372 */ | |
373 public static final hash_t toHash(Object[] objects) { | |
374 if (objects is null) { | |
375 return 0; | |
376 } | |
377 | |
378 int hashCode = 89; | |
379 for (int i = 0; i < objects.length; i++) { | |
380 final Object object = objects[i]; | |
381 if (object !is null) { | |
382 hashCode = hashCode * 31 + object.toHash(); | |
383 } | |
384 } | |
385 | |
386 return hashCode; | |
387 } | |
388 public static final hash_t toHash(String str) { | |
389 return java.lang.all.toHash(str); | |
390 } | |
391 public static final hash_t toHash(String[] objects) { | |
392 int hashCode = 89; | |
393 for (int i = 0; i < objects.length; i++) { | |
394 auto object = objects[i]; | |
395 hashCode = hashCode * 31 + toHash(object); | |
396 } | |
397 | |
398 return hashCode; | |
399 } | |
400 | |
401 /** | |
402 * Checks whether the second array is a subsequence of the first array, and | |
403 * that they share common starting elements. | |
404 * | |
405 * @param left | |
406 * The first array to compare (large); may be <code>null</code>. | |
407 * @param right | |
408 * The second array to compare (small); may be <code>null</code>. | |
409 * @param equals | |
410 * Whether it is allowed for the two arrays to be equivalent. | |
411 * @return <code>true</code> if the first arrays starts with the second | |
412 * list; <code>false</code> otherwise. | |
413 */ | |
414 public static final bool startsWith(Object[] left, | |
415 Object[] right, bool equals) { | |
416 if (left is null || right is null) { | |
417 return false; | |
418 } | |
419 | |
420 int l = left.length; | |
421 int r = right.length; | |
422 | |
423 if (r > l || !equals && r is l) { | |
424 return false; | |
425 } | |
426 | |
427 for (int i = 0; i < r; i++) { | |
428 if (!opEquals(left[i], right[i])) { | |
429 return false; | |
430 } | |
431 } | |
432 | |
433 return true; | |
434 } | |
435 | |
436 /** | |
437 * Converts an array into a string representation that is suitable for | |
438 * debugging. | |
439 * | |
440 * @param array | |
441 * The array to convert; may be <code>null</code>. | |
442 * @return The string representation of the array; never <code>null</code>. | |
443 */ | |
444 public static final String toString(Object[] array) { | |
445 if (array is null) { | |
446 return "null"; //$NON-NLS-1$ | |
447 } | |
448 | |
449 final StringBuffer buffer = new StringBuffer(); | |
450 buffer.append('['); | |
451 | |
452 final int length = array.length; | |
453 for (int i = 0; i < length; i++) { | |
454 if (i !is 0) { | |
455 buffer.append(','); | |
456 } | |
457 Object object = array[i]; | |
458 String element = (object is null ) ? "null" : object.toString; | |
459 buffer.append(element); | |
460 } | |
461 buffer.append(']'); | |
462 | |
463 return buffer.toString(); | |
464 } | |
465 | |
466 /** | |
467 * Provides a translation of a particular key from the resource bundle. | |
468 * | |
469 * @param resourceBundle | |
470 * The key to look up in the resource bundle; should not be | |
471 * <code>null</code>. | |
472 * @param key | |
473 * The key to look up in the resource bundle; should not be | |
474 * <code>null</code>. | |
475 * @param defaultString | |
476 * The value to return if the resource cannot be found; may be | |
477 * <code>null</code>. | |
478 * @return The value of the translated resource at <code>key</code>. If | |
479 * the key cannot be found, then it is simply the | |
480 * <code>defaultString</code>. | |
481 */ | |
482 public static final String translateString( | |
483 ResourceBundle resourceBundle, String key, | |
484 String defaultString) { | |
485 if (resourceBundle !is null && key !is null) { | |
486 try { | |
487 String translatedString = resourceBundle.getString(key); | |
488 | |
489 if (translatedString !is null) { | |
490 return translatedString; | |
491 } | |
492 } catch (MissingResourceException eMissingResource) { | |
493 // Such is life. We'll return the key | |
494 } | |
495 } | |
496 | |
497 return defaultString; | |
498 } | |
499 | |
500 /** | |
501 * Foundation replacement for String.replaceAll(*). | |
502 * | |
503 * @param src the starting string. | |
504 * @param find the string to find. | |
505 * @param replacement the string to replace. | |
506 * @return The new string. | |
507 * @since 3.4 | |
508 */ | |
509 public static final String replaceAll(String src, String find, String replacement) { | |
510 final int len = src.length; | |
511 final int findLen = find.length; | |
512 | |
513 int idx = src.indexOf(find); | |
514 if (idx < 0) { | |
515 return src; | |
516 } | |
517 | |
518 StringBuffer buf = new StringBuffer(); | |
519 int beginIndex = 0; | |
520 while (idx !is -1 && idx < len) { | |
521 buf.append(src.substring(beginIndex, idx)); | |
522 buf.append(replacement); | |
523 | |
524 beginIndex = idx + findLen; | |
525 if (beginIndex < len) { | |
526 idx = src.indexOf(find, beginIndex); | |
527 } else { | |
528 idx = -1; | |
529 } | |
530 } | |
531 if (beginIndex<len) { | |
532 buf.append(src.substring(beginIndex, (idx is -1 ? len : idx))); | |
533 } | |
534 return buf.toString(); | |
535 } | |
536 | |
537 /** | |
538 * This class should never be constructed. | |
539 */ | |
540 private this() { | |
541 // Not allowed. | |
542 } | |
543 } |