IllegalArgumentException
15 | * if the test result is false
.
16 | * Assert.isTrue(i > 0, "The value must be greater than zero");17 | * @param expression a boolean expression 18 | * @param message the exception message to use if the assertion fails 19 | * @throws IllegalArgumentException if expression is
false
20 | */
21 | public static void isTrue(boolean expression, String message) {
22 | if (!expression) {
23 | throw new IllegalArgumentException(message);
24 | }
25 | }
26 |
27 | /**
28 | * Assert a boolean expression, throwing IllegalArgumentException
29 | * if the test result is false
.
30 | * Assert.isTrue(i > 0);31 | * @param expression a boolean expression 32 | * @throws IllegalArgumentException if expression is
false
33 | */
34 | public static void isTrue(boolean expression) {
35 | isTrue(expression, "[Assertion failed] - this expression must be true");
36 | }
37 |
38 | /**
39 | * Assert that an object is null
.
40 | * Assert.isNull(value, "The value must be null");41 | * @param object the object to check 42 | * @param message the exception message to use if the assertion fails 43 | * @throws IllegalArgumentException if the object is not
null
44 | */
45 | public static void isNull(Object object, String message) {
46 | if (object != null) {
47 | throw new IllegalArgumentException(message);
48 | }
49 | }
50 |
51 | /**
52 | * Assert that an object is null
.
53 | * Assert.isNull(value);54 | * @param object the object to check 55 | * @throws IllegalArgumentException if the object is not
null
56 | */
57 | public static void isNull(Object object) {
58 | isNull(object, "[Assertion failed] - the object argument must be null");
59 | }
60 |
61 | /**
62 | * Assert that an object is not null
.
63 | * Assert.notNull(clazz, "The class must not be null");64 | * @param object the object to check 65 | * @param message the exception message to use if the assertion fails 66 | * @throws IllegalArgumentException if the object is
null
67 | */
68 | public static null
.
77 | * Assert.notNull(clazz);78 | * @param object the object to check 79 | * @throws IllegalArgumentException if the object is
null
80 | */
81 | public static void notNull(Object object) {
82 | notNull(object, "[Assertion failed] - this argument is required; it must not be null");
83 | }
84 |
85 | /**
86 | * Assert that the given String is not empty; that is,
87 | * it must not be null
and not the empty String.
88 | * Assert.hasLength(name, "Name must not be empty");89 | * @param text the String to check 90 | * @param message the exception message to use if the assertion fails 91 | * @see Strings#hasLength 92 | */ 93 | public static void hasLength(String text, String message) { 94 | if (!Strings.hasLength(text)) { 95 | throw new IllegalArgumentException(message); 96 | } 97 | } 98 | 99 | /** 100 | * Assert that the given String is not empty; that is, 101 | * it must not be
null
and not the empty String.
102 | * Assert.hasLength(name);103 | * @param text the String to check 104 | * @see Strings#hasLength 105 | */ 106 | public static void hasLength(String text) { 107 | hasLength(text, 108 | "[Assertion failed] - this String argument must have length; it must not be null or empty"); 109 | } 110 | 111 | /** 112 | * Assert that the given String has valid text content; that is, it must not 113 | * be
null
and must contain at least one non-whitespace character.
114 | * Assert.hasText(name, "'name' must not be empty");115 | * @param text the String to check 116 | * @param message the exception message to use if the assertion fails 117 | * @see Strings#hasText 118 | */ 119 | public static String hasText(String text, String message) { 120 | if (!Strings.hasText(text)) { 121 | throw new IllegalArgumentException(message); 122 | } 123 | return text; 124 | } 125 | 126 | /** 127 | * Assert that the given String has valid text content; that is, it must not 128 | * be
null
and must contain at least one non-whitespace character.
129 | * Assert.hasText(name, "'name' must not be empty");130 | * @param text the String to check 131 | * @see Strings#hasText 132 | */ 133 | public static void hasText(String text) { 134 | hasText(text, 135 | "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank"); 136 | } 137 | 138 | /** 139 | * Assert that the given text does not contain the given substring. 140 | *
Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");141 | * @param textToSearch the text to search 142 | * @param substring the substring to find within the text 143 | * @param message the exception message to use if the assertion fails 144 | */ 145 | public static void doesNotContain(String textToSearch, String substring, String message) { 146 | if (Strings.hasLength(textToSearch) && Strings.hasLength(substring) && 147 | textToSearch.indexOf(substring) != -1) { 148 | throw new IllegalArgumentException(message); 149 | } 150 | } 151 | 152 | /** 153 | * Assert that the given text does not contain the given substring. 154 | *
Assert.doesNotContain(name, "rod");155 | * @param textToSearch the text to search 156 | * @param substring the substring to find within the text 157 | */ 158 | public static void doesNotContain(String textToSearch, String substring) { 159 | doesNotContain(textToSearch, substring, 160 | "[Assertion failed] - this String argument must not contain the substring [" + substring + "]"); 161 | } 162 | 163 | 164 | /** 165 | * Assert that an array has elements; that is, it must not be 166 | *
null
and must have at least one element.
167 | * Assert.notEmpty(array, "The array must have elements");168 | * @param array the array to check 169 | * @param message the exception message to use if the assertion fails 170 | * @throws IllegalArgumentException if the object array is
null
or has no elements
171 | */
172 | public static void notEmpty(Object[] array, String message) {
173 | if (Objects.isEmpty(array)) {
174 | throw new IllegalArgumentException(message);
175 | }
176 | }
177 |
178 | /**
179 | * Assert that an array has elements; that is, it must not be
180 | * null
and must have at least one element.
181 | * Assert.notEmpty(array);182 | * @param array the array to check 183 | * @throws IllegalArgumentException if the object array is
null
or has no elements
184 | */
185 | public static void notEmpty(Object[] array) {
186 | notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
187 | }
188 |
189 | public static void notEmpty(byte[] array, String msg) {
190 | if (Objects.isEmpty(array)) {
191 | throw new IllegalArgumentException(msg);
192 | }
193 | }
194 |
195 | /**
196 | * Assert that an array has no null elements.
197 | * Note: Does not complain if the array is empty!
198 | * Assert.noNullElements(array, "The array must have non-null elements");199 | * @param array the array to check 200 | * @param message the exception message to use if the assertion fails 201 | * @throws IllegalArgumentException if the object array contains a
null
element
202 | */
203 | public static void noNullElements(Object[] array, String message) {
204 | if (array != null) {
205 | for (int i = 0; i < array.length; i++) {
206 | if (array[i] == null) {
207 | throw new IllegalArgumentException(message);
208 | }
209 | }
210 | }
211 | }
212 |
213 | /**
214 | * Assert that an array has no null elements.
215 | * Note: Does not complain if the array is empty!
216 | * Assert.noNullElements(array);217 | * @param array the array to check 218 | * @throws IllegalArgumentException if the object array contains a
null
element
219 | */
220 | public static void noNullElements(Object[] array) {
221 | noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
222 | }
223 |
224 | /**
225 | * Assert that a collection has elements; that is, it must not be
226 | * null
and must have at least one element.
227 | * Assert.notEmpty(collection, "Collection must have elements");228 | * @param collection the collection to check 229 | * @param message the exception message to use if the assertion fails 230 | * @throws IllegalArgumentException if the collection is
null
or has no elements
231 | */
232 | public static null
and must have at least one element.
242 | * Assert.notEmpty(collection, "Collection must have elements");243 | * @param collection the collection to check 244 | * @throws IllegalArgumentException if the collection is
null
or has no elements
245 | */
246 | public static void notEmpty(Collection collection) {
247 | notEmpty(collection,
248 | "[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
249 | }
250 |
251 | /**
252 | * Assert that a Map has entries; that is, it must not be null
253 | * and must have at least one entry.
254 | * Assert.notEmpty(map, "Map must have entries");255 | * @param map the map to check 256 | * @param message the exception message to use if the assertion fails 257 | * @throws IllegalArgumentException if the map is
null
or has no entries
258 | */
259 | public static null
268 | * and must have at least one entry.
269 | * Assert.notEmpty(map);270 | * @param map the map to check 271 | * @throws IllegalArgumentException if the map is
null
or has no entries
272 | */
273 | public static void notEmpty(Map map) {
274 | notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
275 | }
276 |
277 |
278 | /**
279 | * Assert that the provided object is an instance of the provided class.
280 | * Assert.instanceOf(Foo.class, foo);281 | * @param clazz the required class 282 | * @param obj the object to check 283 | * @throws IllegalArgumentException if the object is not an instance of clazz 284 | * @see Class#isInstance 285 | */ 286 | public static void isInstanceOf(Class clazz, Object obj) { 287 | isInstanceOf(clazz, obj, ""); 288 | } 289 | 290 | /** 291 | * Assert that the provided object is an instance of the provided class. 292 | *
Assert.instanceOf(Foo.class, foo);293 | * @param type the type to check against 294 | * @param obj the object to check 295 | * @param message a message which will be prepended to the message produced by 296 | * the function itself, and which may be used to provide context. It should 297 | * normally end in a ": " or ". " so that the function generate message looks 298 | * ok when prepended to it. 299 | * @throws IllegalArgumentException if the object is not an instance of clazz 300 | * @see Class#isInstance 301 | */ 302 | public static void isInstanceOf(Class type, Object obj, String message) { 303 | notNull(type, "Type to check against must not be null"); 304 | if (!type.isInstance(obj)) { 305 | throw new IllegalArgumentException(message + 306 | "Object of class [" + (obj != null ? obj.getClass().getName() : "null") + 307 | "] must be an instance of " + type); 308 | } 309 | } 310 | 311 | /** 312 | * Assert that
superType.isAssignableFrom(subType)
is true
.
313 | * Assert.isAssignable(Number.class, myClass);314 | * @param superType the super type to check 315 | * @param subType the sub type to check 316 | * @throws IllegalArgumentException if the classes are not assignable 317 | */ 318 | public static void isAssignable(Class superType, Class subType) { 319 | isAssignable(superType, subType, ""); 320 | } 321 | 322 | /** 323 | * Assert that
superType.isAssignableFrom(subType)
is true
.
324 | * Assert.isAssignable(Number.class, myClass);325 | * @param superType the super type to check against 326 | * @param subType the sub type to check 327 | * @param message a message which will be prepended to the message produced by 328 | * the function itself, and which may be used to provide context. It should 329 | * normally end in a ": " or ". " so that the function generate message looks 330 | * ok when prepended to it. 331 | * @throws IllegalArgumentException if the classes are not assignable 332 | */ 333 | public static void isAssignable(Class superType, Class subType, String message) { 334 | notNull(superType, "Type to check against must not be null"); 335 | if (subType == null || !superType.isAssignableFrom(subType)) { 336 | throw new IllegalArgumentException(message + subType + " is not assignable to " + superType); 337 | } 338 | } 339 | 340 | 341 | /** 342 | * Assert a boolean expression, throwing
IllegalStateException
343 | * if the test result is false
. Call isTrue if you wish to
344 | * throw IllegalArgumentException on an assertion failure.
345 | * Assert.state(id == null, "The id property must not already be initialized");346 | * @param expression a boolean expression 347 | * @param message the exception message to use if the assertion fails 348 | * @throws IllegalStateException if expression is
false
349 | */
350 | public static void state(boolean expression, String message) {
351 | if (!expression) {
352 | throw new IllegalStateException(message);
353 | }
354 | }
355 |
356 | /**
357 | * Assert a boolean expression, throwing {@link IllegalStateException}
358 | * if the test result is false
.
359 | * Call {@link #isTrue(boolean)} if you wish to 360 | * throw {@link IllegalArgumentException} on an assertion failure. 361 | *
Assert.state(id == null);362 | * @param expression a boolean expression 363 | * @throws IllegalStateException if the supplied expression is
false
364 | */
365 | public static void state(boolean expression) {
366 | state(expression, "[Assertion failed] - this state invariant must be true");
367 | }
368 |
369 | }
370 |
--------------------------------------------------------------------------------
/samza-zookeeper/src/main/java/com/stormpath/samza/lang/Classes.java:
--------------------------------------------------------------------------------
1 | package com.stormpath.samza.lang;
2 |
3 | import java.io.InputStream;
4 | import java.lang.reflect.Constructor;
5 |
6 | /**
7 | * @since 0.1
8 | */
9 | public final class Classes {
10 |
11 | private static final Classes INSTANCE = new Classes();
12 |
13 | private Classes() {}
14 |
15 | /**
16 | * @since 0.1
17 | */
18 | private static final ClassLoaderAccessor THREAD_CL_ACCESSOR = new ExceptionIgnoringAccessor() {
19 | @Override
20 | protected ClassLoader doGetClassLoader() throws Throwable {
21 | return Thread.currentThread().getContextClassLoader();
22 | }
23 | };
24 |
25 | /**
26 | * @since 0.1
27 | */
28 | private static final ClassLoaderAccessor CLASS_CL_ACCESSOR = new ExceptionIgnoringAccessor() {
29 | @Override
30 | protected ClassLoader doGetClassLoader() throws Throwable {
31 | return Classes.class.getClassLoader();
32 | }
33 | };
34 |
35 | /**
36 | * @since 0.1
37 | */
38 | private static final ClassLoaderAccessor SYSTEM_CL_ACCESSOR = new ExceptionIgnoringAccessor() {
39 | @Override
40 | protected ClassLoader doGetClassLoader() throws Throwable {
41 | return ClassLoader.getSystemClassLoader();
42 | }
43 | };
44 |
45 | /**
46 | * Attempts to load the specified class name from the current thread's
47 | * {@link Thread#getContextClassLoader() context class loader}, then the
48 | * current ClassLoader (Classes.class.getClassLoader()
), then the system/application
49 | * ClassLoader (ClassLoader.getSystemClassLoader()
, in that order. If any of them cannot locate
50 | * the specified class, an UnknownClassException
is thrown (our RuntimeException equivalent of
51 | * the JRE's ClassNotFoundException
.
52 | *
53 | * @param fqcn the fully qualified class name to load
54 | * @return the located class
55 | * @throws UnknownClassException if the class cannot be found.
56 | */
57 | public static Class forName(String fqcn) throws UnknownClassException {
58 |
59 | Class clazz = THREAD_CL_ACCESSOR.loadClass(fqcn);
60 |
61 | if (clazz == null) {
62 | clazz = CLASS_CL_ACCESSOR.loadClass(fqcn);
63 | }
64 |
65 | if (clazz == null) {
66 | clazz = SYSTEM_CL_ACCESSOR.loadClass(fqcn);
67 | }
68 |
69 | if (clazz == null) {
70 | String msg = "Unable to load class named [" + fqcn + "] from the thread context, current, or " +
71 | "system/application ClassLoaders. All heuristics have been exhausted. Class could not be found.";
72 |
73 | if (fqcn != null && fqcn.startsWith("com.stormpath.sdk.impl")) {
74 | msg += " Have you remembered to include the stormpath-sdk-impl .jar in your runtime classpath?";
75 | }
76 |
77 | throw new UnknownClassException(msg);
78 | }
79 |
80 | return clazz;
81 | }
82 |
83 | /**
84 | * Returns the specified resource by checking the current thread's
85 | * {@link Thread#getContextClassLoader() context class loader}, then the
86 | * current ClassLoader (Classes.class.getClassLoader()
), then the system/application
87 | * ClassLoader (ClassLoader.getSystemClassLoader()
, in that order, using
88 | * {@link ClassLoader#getResourceAsStream(String) getResourceAsStream(name)}.
89 | *
90 | * @param name the name of the resource to acquire from the classloader(s).
91 | * @return the InputStream of the resource found, or null
if the resource cannot be found from any
92 | * of the three mentioned ClassLoaders.
93 | * @since 0.8
94 | */
95 | public static InputStream getResourceAsStream(String name) {
96 |
97 | InputStream is = THREAD_CL_ACCESSOR.getResourceStream(name);
98 |
99 | if (is == null) {
100 | is = CLASS_CL_ACCESSOR.getResourceStream(name);
101 | }
102 |
103 | if (is == null) {
104 | is = SYSTEM_CL_ACCESSOR.getResourceStream(name);
105 | }
106 |
107 | return is;
108 | }
109 |
110 | public static boolean isAvailable(String fullyQualifiedClassName) {
111 | try {
112 | forName(fullyQualifiedClassName);
113 | return true;
114 | } catch (UnknownClassException e) {
115 | return false;
116 | }
117 | }
118 |
119 | @SuppressWarnings("unchecked")
120 | public static Object newInstance(String fqcn) {
121 | return newInstance(forName(fqcn));
122 | }
123 |
124 | @SuppressWarnings("unchecked")
125 | public static Object newInstance(String fqcn, Object... args) {
126 | return newInstance(forName(fqcn), args);
127 | }
128 |
129 | public static true
if the supplied Collection is null
37 | * or empty. Otherwise, return false
.
38 | * @param collection the Collection to check
39 | * @return whether the given Collection is empty
40 | */
41 | public static boolean isEmpty(Collection collection) {
42 | return (collection == null || collection.isEmpty());
43 | }
44 |
45 | /**
46 | * Returns the collection's size or {@code 0} if the collection is {@code null}.
47 | *
48 | * @param collection the collection to check.
49 | * @return the collection's size or {@code 0} if the collection is {@code null}.
50 | * @since 0.9.2
51 | */
52 | public static int size(Collection collection) {
53 | return collection == null ? 0 : collection.size();
54 | }
55 |
56 | /**
57 | * Returns the map's size or {@code 0} if the map is {@code null}.
58 | *
59 | * @param map the map to check
60 | * @return the map's size or {@code 0} if the map is {@code null}.
61 | * @since 0.9.2
62 | */
63 | public static int size(Map map) {
64 | return map == null ? 0 : map.size();
65 | }
66 |
67 | /**
68 | * Return true
if the supplied Map is null
69 | * or empty. Otherwise, return false
.
70 | * @param map the Map to check
71 | * @return whether the given Map is empty
72 | */
73 | public static boolean isEmpty(Map map) {
74 | return (map == null || map.isEmpty());
75 | }
76 |
77 | /**
78 | * Convert the supplied array into a List. A primitive array gets
79 | * converted into a List of the appropriate wrapper type.
80 | * A null
source value will be converted to an
81 | * empty List.
82 | * @param source the (potentially primitive) array
83 | * @return the converted List result
84 | * @see Objects#toObjectArray(Object)
85 | */
86 | public static List arrayToList(Object source) {
87 | return Arrays.asList(Objects.toObjectArray(source));
88 | }
89 |
90 | /**
91 | * Merge the given array into the given Collection.
92 | * @param array the array to merge (may be null
)
93 | * @param collection the target Collection to merge the array into
94 | */
95 | @SuppressWarnings("unchecked")
96 | public static void mergeArrayIntoCollection(Object array, Collection collection) {
97 | if (collection == null) {
98 | throw new IllegalArgumentException("Collection must not be null");
99 | }
100 | Object[] arr = Objects.toObjectArray(array);
101 | for (Object elem : arr) {
102 | collection.add(elem);
103 | }
104 | }
105 |
106 | /**
107 | * Merge the given Properties instance into the given Map,
108 | * copying all properties (key-value pairs) over.
109 | *
Uses Properties.propertyNames()
to even catch
110 | * default properties linked into the original Properties instance.
111 | * @param props the Properties instance to merge (may be null
)
112 | * @param map the target Map to merge the properties into
113 | */
114 | @SuppressWarnings("unchecked")
115 | public static void mergePropertiesIntoMap(Properties props, Map map) {
116 | if (map == null) {
117 | throw new IllegalArgumentException("Map must not be null");
118 | }
119 | if (props != null) {
120 | for (Enumeration en = props.propertyNames(); en.hasMoreElements();) {
121 | String key = (String) en.nextElement();
122 | Object value = props.getProperty(key);
123 | if (value == null) {
124 | // Potentially a non-String value...
125 | value = props.get(key);
126 | }
127 | map.put(key, value);
128 | }
129 | }
130 | }
131 |
132 |
133 | /**
134 | * Check whether the given Iterator contains the given element.
135 | * @param iterator the Iterator to check
136 | * @param element the element to look for
137 | * @return true
if found, false
else
138 | */
139 | public static boolean contains(Iterator iterator, Object element) {
140 | if (iterator != null) {
141 | while (iterator.hasNext()) {
142 | Object candidate = iterator.next();
143 | if (Objects.nullSafeEquals(candidate, element)) {
144 | return true;
145 | }
146 | }
147 | }
148 | return false;
149 | }
150 |
151 | /**
152 | * Check whether the given Enumeration contains the given element.
153 | * @param enumeration the Enumeration to check
154 | * @param element the element to look for
155 | * @return true
if found, false
else
156 | */
157 | public static boolean contains(Enumeration enumeration, Object element) {
158 | if (enumeration != null) {
159 | while (enumeration.hasMoreElements()) {
160 | Object candidate = enumeration.nextElement();
161 | if (Objects.nullSafeEquals(candidate, element)) {
162 | return true;
163 | }
164 | }
165 | }
166 | return false;
167 | }
168 |
169 | /**
170 | * Check whether the given Collection contains the given element instance.
171 | *
Enforces the given instance to be present, rather than returning
172 | * true
for an equal element as well.
173 | * @param collection the Collection to check
174 | * @param element the element to look for
175 | * @return true
if found, false
else
176 | */
177 | public static boolean containsInstance(Collection collection, Object element) {
178 | if (collection != null) {
179 | for (Object candidate : collection) {
180 | if (candidate == element) {
181 | return true;
182 | }
183 | }
184 | }
185 | return false;
186 | }
187 |
188 | /**
189 | * Return true
if any element in 'candidates
' is
190 | * contained in 'source
'; otherwise returns false
.
191 | * @param source the source Collection
192 | * @param candidates the candidates to search for
193 | * @return whether any of the candidates has been found
194 | */
195 | public static boolean containsAny(Collection source, Collection candidates) {
196 | if (isEmpty(source) || isEmpty(candidates)) {
197 | return false;
198 | }
199 | for (Object candidate : candidates) {
200 | if (source.contains(candidate)) {
201 | return true;
202 | }
203 | }
204 | return false;
205 | }
206 |
207 | /**
208 | * Return the first element in 'candidates
' that is contained in
209 | * 'source
'. If no element in 'candidates
' is present in
210 | * 'source
' returns null
. Iteration order is
211 | * {@link Collection} implementation specific.
212 | * @param source the source Collection
213 | * @param candidates the candidates to search for
214 | * @return the first present object, or null
if not found
215 | */
216 | public static Object findFirstMatch(Collection source, Collection candidates) {
217 | if (isEmpty(source) || isEmpty(candidates)) {
218 | return null;
219 | }
220 | for (Object candidate : candidates) {
221 | if (source.contains(candidate)) {
222 | return candidate;
223 | }
224 | }
225 | return null;
226 | }
227 |
228 | /**
229 | * Find a single value of the given type in the given Collection.
230 | * @param collection the Collection to search
231 | * @param type the type to look for
232 | * @return a value of the given type found if there is a clear match,
233 | * or null
if none or more than one such value found
234 | */
235 | @SuppressWarnings("unchecked")
236 | public static null
if none or more than one such value found
261 | */
262 | public static Object findValueOfType(Collection> collection, Class>[] types) {
263 | if (isEmpty(collection) || Objects.isEmpty(types)) {
264 | return null;
265 | }
266 | for (Class> type : types) {
267 | Object value = findValueOfType(collection, type);
268 | if (value != null) {
269 | return value;
270 | }
271 | }
272 | return null;
273 | }
274 |
275 | /**
276 | * Determine whether the given Collection only contains a single unique object.
277 | * @param collection the Collection to check
278 | * @return true
if the collection contains a single reference or
279 | * multiple references to the same instance, false
else
280 | */
281 | public static boolean hasUniqueObject(Collection collection) {
282 | if (isEmpty(collection)) {
283 | return false;
284 | }
285 | boolean hasCandidate = false;
286 | Object candidate = null;
287 | for (Object elem : collection) {
288 | if (!hasCandidate) {
289 | hasCandidate = true;
290 | candidate = elem;
291 | }
292 | else if (candidate != elem) {
293 | return false;
294 | }
295 | }
296 | return true;
297 | }
298 |
299 | /**
300 | * Find the common element type of the given Collection, if any.
301 | * @param collection the Collection to check
302 | * @return the common element type, or null
if no clear
303 | * common type has been found (or the collection was empty)
304 | */
305 | public static Class> findCommonElementType(Collection collection) {
306 | if (isEmpty(collection)) {
307 | return null;
308 | }
309 | Class> candidate = null;
310 | for (Object val : collection) {
311 | if (val != null) {
312 | if (candidate == null) {
313 | candidate = val.getClass();
314 | }
315 | else if (candidate != val.getClass()) {
316 | return null;
317 | }
318 | }
319 | }
320 | return candidate;
321 | }
322 |
323 | /**
324 | * Marshal the elements from the given enumeration into an array of the given type.
325 | * Enumeration elements must be assignable to the type of the given array. The array
326 | * returned will be a different instance than the array given.
327 | */
328 | public static A[] toArray(EnumerationRuntimeException
equivalent of the JDK's
5 | * ClassNotFoundException
, to maintain a RuntimeException paradigm.
6 | *
7 | * @since 0.1
8 | */
9 | public class UnknownClassException extends RuntimeException {
10 |
11 | /*
12 | /**
13 | * Creates a new UnknownClassException.
14 | *
15 | public UnknownClassException() {
16 | super();
17 | }*/
18 |
19 | /**
20 | * Constructs a new UnknownClassException.
21 | *
22 | * @param message the reason for the exception
23 | */
24 | public UnknownClassException(String message) {
25 | super(message);
26 | }
27 |
28 | /*
29 | * Constructs a new UnknownClassException.
30 | *
31 | * @param cause the underlying Throwable that caused this exception to be thrown.
32 | *
33 | public UnknownClassException(Throwable cause) {
34 | super(cause);
35 | }
36 |
37 | /**
38 | * Constructs a new UnknownClassException.
39 | *
40 | * @param message the reason for the exception
41 | * @param cause the underlying Throwable that caused this exception to be thrown.
42 | *
43 | public UnknownClassException(String message, Throwable cause) {
44 | super(message, cause);
45 | }
46 | */
47 |
48 | }
--------------------------------------------------------------------------------