Mercurial > projects > dwt2
comparison org.eclipse.core.commands/src/org/eclipse/core/internal/commands/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 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 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.core.internal.commands.util.Util; | |
15 | |
16 import java.lang.all; | |
17 | |
18 // import java.util.Collections; | |
19 // import java.util.HashMap; | |
20 // import java.util.HashSet; | |
21 // import java.util.Iterator; | |
22 // import java.util.Map; | |
23 // import java.util.Set; | |
24 // import java.util.SortedMap; | |
25 // import java.util.SortedSet; | |
26 // import java.util.TreeMap; | |
27 // import java.util.TreeSet; | |
28 | |
29 /** | |
30 * A class providing utility functions for the commands plug-in. | |
31 * | |
32 * @since 3.1 | |
33 */ | |
34 public final class Util { | |
35 /++ | |
36 /** | |
37 * A shared, unmodifiable, empty, sorted map. This value is guaranteed to | |
38 * always be the same. | |
39 */ | |
40 public const static SortedMap EMPTY_SORTED_MAP = Collections | |
41 .unmodifiableSortedMap(new TreeMap()); | |
42 | |
43 /** | |
44 * A shared, unmodifiable, empty, sorted set. This value is guaranteed to | |
45 * always be the same. | |
46 */ | |
47 public final static SortedSet EMPTY_SORTED_SET = Collections | |
48 .unmodifiableSortedSet(new TreeSet()); | |
49 ++/ | |
50 /** | |
51 * A shared, zero-length string -- for avoiding non-externalized string | |
52 * tags. This value is guaranteed to always be the same. | |
53 */ | |
54 public final static String ZERO_LENGTH_STRING = "\0"[ 0 .. 0 ]; //$NON-NLS-1$ | |
55 /++ | |
56 | |
57 /** | |
58 * Asserts the the given object is an instance of the given class -- | |
59 * optionally allowing the object to be <code>null</code>. | |
60 * | |
61 * @param object | |
62 * The object for which the type should be checked. | |
63 * @param c | |
64 * The class that the object must be; fails if the class is | |
65 * <code>null</code>. | |
66 * @param allowNull | |
67 * Whether the object being <code>null</code> will not cause a | |
68 * failure. | |
69 */ | |
70 public static final void assertInstance(final Object object, final Class c, | |
71 final bool allowNull) { | |
72 if (object is null && allowNull) { | |
73 return; | |
74 } | |
75 | |
76 if (object is null || c is null) { | |
77 throw new NullPointerException(); | |
78 } else if (!c.isInstance(object)) { | |
79 throw new IllegalArgumentException(); | |
80 } | |
81 } | |
82 ++/ | |
83 /** | |
84 * Compares two bool values. <code>false</code> is considered to be | |
85 * less than <code>true</code>. | |
86 * | |
87 * @param left | |
88 * The left value to compare. | |
89 * @param right | |
90 * The right value to compare. | |
91 * @return <code>-1</code> if <code>left</code> is <code>false</code> | |
92 * and <code>right</code> is <code>true</code>;<code>0</code> | |
93 * if they are equal; <code>1</code> if <code>left</code> is | |
94 * <code>true</code> and <code>right</code> is | |
95 * <code>false</code> | |
96 */ | |
97 public static final int compare(bool left, bool right) { | |
98 return left is false ? (right is true ? -1 : 0) : (right is true ? 0 | |
99 : 1); | |
100 } | |
101 | |
102 /** | |
103 * Compares two comparable objects, but with protection against | |
104 * <code>null</code>. | |
105 * | |
106 * @param left | |
107 * The left value to compare; may be <code>null</code>. | |
108 * @param right | |
109 * The right value to compare; may be <code>null</code>. | |
110 * @return <code>-1</code> if <code>left</code> is <code>null</code> | |
111 * and <code>right</code> is not <code>null</code>; | |
112 * <code>0</code> if they are both <code>null</code>; | |
113 * <code>1</code> if <code>left</code> is not <code>null</code> | |
114 * and <code>right</code> is <code>null</code>. Otherwise, the | |
115 * result of <code>left.compareTo(right)</code>. | |
116 */ | |
117 public static final int compare(Comparable left, | |
118 Comparable right) { | |
119 if (left is null && right is null) { | |
120 return 0; | |
121 } else if (left is null) { | |
122 return -1; | |
123 } else if (right is null) { | |
124 return 1; | |
125 } else { | |
126 return left.compareTo( cast(Object)right); | |
127 } | |
128 } | |
129 | |
130 /** | |
131 * Compares two integer values. This method fails if the distance between | |
132 * <code>left</code> and <code>right</code> is greater than | |
133 * <code>Integer.MAX_VALUE</code>. | |
134 * | |
135 * @param left | |
136 * The left value to compare. | |
137 * @param right | |
138 * The right value to compare. | |
139 * @return <code>left - right</code> | |
140 */ | |
141 public static final int compare(int left, int right) { | |
142 return left - right; | |
143 } | |
144 | |
145 /** | |
146 * Compares two objects that are not otherwise comparable. If neither object | |
147 * is <code>null</code>, then the string representation of each object is | |
148 * used. | |
149 * | |
150 * @param left | |
151 * The left value to compare. The string representation of this | |
152 * value must not be <code>null</code>. | |
153 * @param right | |
154 * The right value to compare. The string representation of this | |
155 * value must not be <code>null</code>. | |
156 * @return <code>-1</code> if <code>left</code> is <code>null</code> | |
157 * and <code>right</code> is not <code>null</code>; | |
158 * <code>0</code> if they are both <code>null</code>; | |
159 * <code>1</code> if <code>left</code> is not <code>null</code> | |
160 * and <code>right</code> is <code>null</code>. Otherwise, the | |
161 * result of | |
162 * <code>left.toString().compareTo(right.toString())</code>. | |
163 */ | |
164 public static final int compare(Object left, Object right) { | |
165 if (left is null && right is null) { | |
166 return 0; | |
167 } else if (left is null) { | |
168 return -1; | |
169 } else if (right is null) { | |
170 return 1; | |
171 } else { | |
172 return left.toString() < (right.toString()); | |
173 } | |
174 } | |
175 public static final int compare(Object[] left, Object[] right) { | |
176 if (left.length !is right.length ) { | |
177 return left.length < right.length; | |
178 } | |
179 for( int i = 0; i < left.length; i++ ){ | |
180 int res = left[i].opCmp(right[i]); | |
181 if( res !is 0 ){ | |
182 return res; | |
183 } | |
184 } | |
185 return 0; | |
186 } | |
187 public static final int compare(String left, String right) { | |
188 return left < right; | |
189 } | |
190 | |
191 /** | |
192 * Decides whether two booleans are equal. | |
193 * | |
194 * @param left | |
195 * The first bool to compare; may be <code>null</code>. | |
196 * @param right | |
197 * The second bool to compare; may be <code>null</code>. | |
198 * @return <code>true</code> if the booleans are equal; <code>false</code> | |
199 * otherwise. | |
200 */ | |
201 public static bool equals(bool left, bool right) { | |
202 return left is right; | |
203 } | |
204 | |
205 /** | |
206 * Decides whether two objects are equal -- defending against | |
207 * <code>null</code>. | |
208 * | |
209 * @param left | |
210 * The first object to compare; may be <code>null</code>. | |
211 * @param right | |
212 * The second object to compare; may be <code>null</code>. | |
213 * @return <code>true</code> if the objects are equals; <code>false</code> | |
214 * otherwise. | |
215 */ | |
216 public static bool equals(Object left, Object right) { | |
217 return left is null ? right is null : ((right !is null) && (left | |
218 .opEquals(right)) !is 0 ); | |
219 } | |
220 public static bool equals(String left, String right) { | |
221 return left == right; | |
222 } | |
223 public static bool equals(String[] left, String[] right) { | |
224 if( left.length !is right.length ){ | |
225 return false; | |
226 } | |
227 for( int i = 0; i < left.length; i++ ){ | |
228 if( !equals( left[i], right[i] )){ | |
229 return false; | |
230 } | |
231 } | |
232 return true; | |
233 } | |
234 | |
235 /** | |
236 * Tests whether two arrays of objects are equal to each other. The arrays | |
237 * must not be <code>null</code>, but their elements may be | |
238 * <code>null</code>. | |
239 * | |
240 * @param leftArray | |
241 * The left array to compare; may be <code>null</code>, and | |
242 * may be empty and may contain <code>null</code> elements. | |
243 * @param rightArray | |
244 * The right array to compare; may be <code>null</code>, and | |
245 * may be empty and may contain <code>null</code> elements. | |
246 * @return <code>true</code> if the arrays are equal length and the | |
247 * elements at the same position are equal; <code>false</code> | |
248 * otherwise. | |
249 */ | |
250 public static bool equals(Object[] leftArray, | |
251 Object[] rightArray) { | |
252 if (leftArray is null) { | |
253 return (rightArray is null); | |
254 } else if (rightArray is null) { | |
255 return false; | |
256 } | |
257 | |
258 if (leftArray.length !is rightArray.length) { | |
259 return false; | |
260 } | |
261 | |
262 for (int i = 0; i < leftArray.length; i++) { | |
263 Object left = leftArray[i]; | |
264 Object right = rightArray[i]; | |
265 bool equal = (left is null) ? (right is null) : (left | |
266 .opEquals(right) !is 0); | |
267 if (!equal) { | |
268 return false; | |
269 } | |
270 } | |
271 | |
272 return true; | |
273 } | |
274 | |
275 /** | |
276 * Computes the hash code for an integer. | |
277 * | |
278 * @param i | |
279 * The integer for which a hash code should be computed. | |
280 * @return <code>i</code>. | |
281 */ | |
282 public static final hash_t toHash(int i) { | |
283 return i; | |
284 } | |
285 | |
286 /** | |
287 * Computes the hash code for an object, but with defense against | |
288 * <code>null</code>. | |
289 * | |
290 * @param object | |
291 * The object for which a hash code is needed; may be | |
292 * <code>null</code>. | |
293 * @return The hash code for <code>object</code>; or <code>0</code> if | |
294 * <code>object</code> is <code>null</code>. | |
295 */ | |
296 public static final hash_t toHash( Object object) { | |
297 return object !is null ? object.toHash() : 0; | |
298 } | |
299 public static final hash_t toHash( char[] str) { | |
300 return str !is null ? java.lang.all.toHash( str ) : 0; | |
301 } | |
302 /++ | |
303 | |
304 /** | |
305 * Makes a type-safe copy of the given map. This method should be used when | |
306 * a map is crossing an API boundary (i.e., from a hostile plug-in into | |
307 * internal code, or vice versa). | |
308 * | |
309 * @param map | |
310 * The map which should be copied; must not be <code>null</code>. | |
311 * @param keyClass | |
312 * The class that all the keys must be; must not be | |
313 * <code>null</code>. | |
314 * @param valueClass | |
315 * The class that all the values must be; must not be | |
316 * <code>null</code>. | |
317 * @param allowNullKeys | |
318 * Whether <code>null</code> keys should be allowed. | |
319 * @param allowNullValues | |
320 * Whether <code>null</code> values should be allowed. | |
321 * @return A copy of the map; may be empty, but never <code>null</code>. | |
322 */ | |
323 public static final Map safeCopy(final Map map, final Class keyClass, | |
324 final Class valueClass, final bool allowNullKeys, | |
325 final bool allowNullValues) { | |
326 if (map is null || keyClass is null || valueClass is null) { | |
327 throw new NullPointerException(); | |
328 } | |
329 | |
330 final Map copy = Collections.unmodifiableMap(new HashMap(map)); | |
331 final Iterator iterator = copy.entrySet().iterator(); | |
332 | |
333 while (iterator.hasNext()) { | |
334 final Map.Entry entry = (Map.Entry) iterator.next(); | |
335 assertInstance(entry.getKey(), keyClass, allowNullKeys); | |
336 assertInstance(entry.getValue(), valueClass, allowNullValues); | |
337 } | |
338 | |
339 return map; | |
340 } | |
341 | |
342 /** | |
343 * Makes a type-safe copy of the given set. This method should be used when | |
344 * a set is crossing an API boundary (i.e., from a hostile plug-in into | |
345 * internal code, or vice versa). | |
346 * | |
347 * @param set | |
348 * The set which should be copied; must not be <code>null</code>. | |
349 * @param c | |
350 * The class that all the values must be; must not be | |
351 * <code>null</code>. | |
352 * @return A copy of the set; may be empty, but never <code>null</code>. | |
353 * None of its element will be <code>null</code>. | |
354 */ | |
355 public static final Set safeCopy(final Set set, final Class c) { | |
356 return safeCopy(set, c, false); | |
357 } | |
358 | |
359 /** | |
360 * Makes a type-safe copy of the given set. This method should be used when | |
361 * a set is crossing an API boundary (i.e., from a hostile plug-in into | |
362 * internal code, or vice versa). | |
363 * | |
364 * @param set | |
365 * The set which should be copied; must not be <code>null</code>. | |
366 * @param c | |
367 * The class that all the values must be; must not be | |
368 * <code>null</code>. | |
369 * @param allowNullElements | |
370 * Whether null values should be allowed. | |
371 * @return A copy of the set; may be empty, but never <code>null</code>. | |
372 */ | |
373 public static final Set safeCopy(final Set set, final Class c, | |
374 final bool allowNullElements) { | |
375 if (set is null || c is null) { | |
376 throw new NullPointerException(); | |
377 } | |
378 | |
379 final Set copy = Collections.unmodifiableSet(new HashSet(set)); | |
380 final Iterator iterator = copy.iterator(); | |
381 | |
382 while (iterator.hasNext()) { | |
383 assertInstance(iterator.next(), c, allowNullElements); | |
384 } | |
385 | |
386 return set; | |
387 } | |
388 ++/ | |
389 /** | |
390 * The utility class is meant to just provide static members. | |
391 */ | |
392 private this() { | |
393 // Should not be called. | |
394 } | |
395 } |