Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ObjectHelper |
|
| 0.0;0 |
1 | /* |
|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
|
3 | * contributor license agreements. See the NOTICE file distributed with |
|
4 | * this work for additional information regarding copyright ownership. |
|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
|
6 | * (the "License"); you may not use this file except in compliance with |
|
7 | * the License. You may obtain a copy of the License at |
|
8 | * |
|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
10 | * |
|
11 | * Unless required by applicable law or agreed to in writing, software |
|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 | * See the License for the specific language governing permissions and |
|
15 | * limitations under the License. |
|
16 | */ |
|
17 | package org.apache.camel.util; |
|
18 | ||
19 | import org.apache.camel.RuntimeCamelException; |
|
20 | import org.apache.camel.converter.ObjectConverter; |
|
21 | import org.apache.commons.logging.Log; |
|
22 | import org.apache.commons.logging.LogFactory; |
|
23 | ||
24 | import java.lang.annotation.Annotation; |
|
25 | import java.lang.reflect.InvocationTargetException; |
|
26 | import java.lang.reflect.Method; |
|
27 | import java.util.ArrayList; |
|
28 | import java.util.Collection; |
|
29 | import java.util.Iterator; |
|
30 | import java.util.List; |
|
31 | ||
32 | /** |
|
33 | * @version $Revision: 534560 $ |
|
34 | */ |
|
35 | 0 | public class ObjectHelper { |
36 | 1 | private static final transient Log log = LogFactory.getLog(ObjectHelper.class); |
37 | ||
38 | /** |
|
39 | * A helper method for comparing objects for equality while handling nulls |
|
40 | */ |
|
41 | public static boolean equals(Object a, Object b) { |
|
42 | 146 | if (a == b) { |
43 | 127 | return true; |
44 | } |
|
45 | 19 | return a != null && b != null && a.equals(b); |
46 | } |
|
47 | ||
48 | /** |
|
49 | * A helper method for performing an ordered comparsion on the objects |
|
50 | * handling nulls and objects which do not |
|
51 | * handle sorting gracefully |
|
52 | */ |
|
53 | public static int compare(Object a, Object b) { |
|
54 | 7 | if (a == b) { |
55 | 0 | return 0; |
56 | } |
|
57 | 7 | if (a == null) { |
58 | 0 | return -1; |
59 | } |
|
60 | 7 | if (b == null) { |
61 | 0 | return 1; |
62 | } |
|
63 | 7 | if (a instanceof Comparable) { |
64 | 7 | Comparable comparable = (Comparable) a; |
65 | 7 | return comparable.compareTo(b); |
66 | } |
|
67 | else { |
|
68 | 0 | int answer = a.getClass().getName().compareTo(b.getClass().getName()); |
69 | 0 | if (answer == 0) { |
70 | 0 | answer = a.hashCode() - b.hashCode(); |
71 | } |
|
72 | 0 | return answer; |
73 | } |
|
74 | } |
|
75 | ||
76 | public static void notNull(Object value, String name) { |
|
77 | 250 | if (value == null) { |
78 | 0 | throw new IllegalArgumentException("No " + name + " specified"); |
79 | } |
|
80 | 250 | } |
81 | ||
82 | public static String[] splitOnCharacter(String value, String needle, int count) { |
|
83 | 103 | String rc[] = new String[count]; |
84 | 103 | rc[0] = value; |
85 | 206 | for (int i = 1; i < count; i++) { |
86 | 103 | String v = rc[i - 1]; |
87 | 103 | int p = v.indexOf(needle); |
88 | 103 | if (p < 0) { |
89 | 0 | return rc; |
90 | } |
|
91 | 103 | rc[i - 1] = v.substring(0, p); |
92 | 103 | rc[i] = v.substring(p + 1); |
93 | } |
|
94 | 103 | return rc; |
95 | } |
|
96 | ||
97 | /** |
|
98 | * Removes any starting characters on the given text which match the given character |
|
99 | * |
|
100 | * @param text the string |
|
101 | * @param ch the initial characters to remove |
|
102 | * @return either the original string or the new substring |
|
103 | */ |
|
104 | public static String removeStartingCharacters(String text, char ch) { |
|
105 | 3 | int idx = 0; |
106 | 6 | while (text.charAt(idx) == ch) { |
107 | 3 | idx++; |
108 | 3 | } |
109 | 3 | if (idx > 0) { |
110 | 2 | return text.substring(idx); |
111 | } |
|
112 | 1 | return text; |
113 | } |
|
114 | ||
115 | /** |
|
116 | * Returns true if the collection contains the specified value |
|
117 | */ |
|
118 | public static boolean contains(Object collectionOrArray, Object value) { |
|
119 | 3 | if (collectionOrArray instanceof Collection) { |
120 | 3 | Collection collection = (Collection) collectionOrArray; |
121 | 3 | return collection.contains(value); |
122 | } |
|
123 | else { |
|
124 | 0 | Iterator iter = ObjectConverter.iterator(value); |
125 | 0 | while (iter.hasNext()) { |
126 | 0 | if (equals(value, iter.next())) { |
127 | 0 | return true; |
128 | } |
|
129 | } |
|
130 | 0 | return false; |
131 | } |
|
132 | } |
|
133 | ||
134 | /** |
|
135 | * Returns the predicate matching boolean on a {@link List} result set |
|
136 | * where if the first element is a boolean its value is used |
|
137 | * otherwise this method returns true if the collection is not empty |
|
138 | * |
|
139 | * @returns true if the first element is a boolean and its value is true or if the list is non empty |
|
140 | */ |
|
141 | public static boolean matches(List list) { |
|
142 | 0 | if (!list.isEmpty()) { |
143 | 0 | Object value = list.get(0); |
144 | 0 | if (value instanceof Boolean) { |
145 | 0 | Boolean flag = (Boolean) value; |
146 | 0 | return flag.booleanValue(); |
147 | } |
|
148 | else { |
|
149 | // lets assume non-empty results are true |
|
150 | 0 | return true; |
151 | } |
|
152 | } |
|
153 | 0 | return false; |
154 | } |
|
155 | ||
156 | public static boolean isNotNullOrBlank(String text) { |
|
157 | 0 | return text != null && text.trim().length() > 0; |
158 | } |
|
159 | ||
160 | /** |
|
161 | * A helper method to access a system property, catching any security exceptions |
|
162 | * |
|
163 | * @param name the name of the system property required |
|
164 | * @param defaultValue the default value to use if the property is not available or a security exception prevents access |
|
165 | * @return the system property value or the default value if the property is not available or security does not allow its access |
|
166 | */ |
|
167 | public static String getSystemProperty(String name, String defaultValue) { |
|
168 | try { |
|
169 | 1 | return System.getProperty(name, defaultValue); |
170 | } |
|
171 | 0 | catch (Exception e) { |
172 | 0 | if (log.isDebugEnabled()) { |
173 | 0 | log.debug("Caught security exception accessing system property: " + name + ". Reason: " + e, e); |
174 | } |
|
175 | 0 | return defaultValue; |
176 | } |
|
177 | } |
|
178 | ||
179 | /** |
|
180 | * Returns the type name of the given type or null if the type variable is null |
|
181 | */ |
|
182 | public static String name(Class type) { |
|
183 | 0 | return type != null ? type.getName() : null; |
184 | } |
|
185 | ||
186 | /** |
|
187 | * Returns the type name of the given value |
|
188 | */ |
|
189 | public static String className(Object value) { |
|
190 | 0 | return name(value != null ? value.getClass() : null); |
191 | } |
|
192 | ||
193 | /** |
|
194 | * Attempts to load the given class name using the thread context class loader |
|
195 | * or the class loader used to load this class |
|
196 | * |
|
197 | * @param name the name of the class to load |
|
198 | * @return the class or null if it could not be loaded |
|
199 | */ |
|
200 | public static Class<?> loadClass(String name) { |
|
201 | 0 | return loadClass(name, ObjectHelper.class.getClassLoader()); |
202 | } |
|
203 | ||
204 | /** |
|
205 | * Attempts to load the given class name using the thread context class loader or the given class loader |
|
206 | * |
|
207 | * @param name the name of the class to load |
|
208 | * @param loader the class loader to use after the thread context class loader |
|
209 | * @return the class or null if it could not be loaded |
|
210 | */ |
|
211 | public static Class<?> loadClass(String name, ClassLoader loader) { |
|
212 | 0 | ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); |
213 | 0 | if (contextClassLoader != null) { |
214 | try { |
|
215 | 0 | return contextClassLoader.loadClass(name); |
216 | } |
|
217 | 0 | catch (ClassNotFoundException e) { |
218 | try { |
|
219 | 0 | return loader.loadClass(name); |
220 | } |
|
221 | 0 | catch (ClassNotFoundException e1) { |
222 | 0 | log.debug("Could not find class: " + name + ". Reason: " + e); |
223 | } |
|
224 | } |
|
225 | } |
|
226 | 0 | return null; |
227 | } |
|
228 | ||
229 | /** |
|
230 | * A helper method to invoke a method via reflection and wrap any exceptions |
|
231 | * as {@link RuntimeCamelException} instances |
|
232 | * |
|
233 | * @param method the method to invoke |
|
234 | * @param instance the object instance (or null for static methods) |
|
235 | * @param parameters the parameters to the method |
|
236 | * @return the result of the method invocation |
|
237 | */ |
|
238 | public static Object invokeMethod(Method method, Object instance, Object... parameters) { |
|
239 | try { |
|
240 | 37 | return method.invoke(instance, parameters); |
241 | } |
|
242 | 0 | catch (IllegalAccessException e) { |
243 | 0 | throw new RuntimeCamelException(e); |
244 | } |
|
245 | 0 | catch (InvocationTargetException e) { |
246 | 0 | throw new RuntimeCamelException(e.getCause()); |
247 | } |
|
248 | } |
|
249 | ||
250 | /** |
|
251 | * Returns a list of methods which are annotated with the given annotation |
|
252 | * |
|
253 | * @param type the type to reflect on |
|
254 | * @param annotationType the annotation type |
|
255 | * @return a list of the methods found |
|
256 | */ |
|
257 | public static List<Method> findMethodsWithAnnotation(Class<?> type, Class<? extends Annotation> annotationType) { |
|
258 | 0 | List<Method> answer = new ArrayList<Method>(); |
259 | do { |
|
260 | 0 | Method[] methods = type.getDeclaredMethods(); |
261 | 0 | for (Method method : methods) { |
262 | 0 | if (method.getAnnotation(annotationType) != null) { |
263 | 0 | answer.add(method); |
264 | } |
|
265 | } |
|
266 | 0 | type = type.getSuperclass(); |
267 | } |
|
268 | 0 | while (type != null); |
269 | 0 | return answer; |
270 | } |
|
271 | ||
272 | /** |
|
273 | * Turns the given object arrays into a meaningful string |
|
274 | * |
|
275 | * @param objects an array of objects or null |
|
276 | * @return a meaningful string |
|
277 | */ |
|
278 | public static String asString(Object[] objects) { |
|
279 | 0 | if (objects == null) { |
280 | 0 | return "null"; |
281 | } |
|
282 | else { |
|
283 | 0 | StringBuffer buffer = new StringBuffer("{"); |
284 | 0 | int counter = 0; |
285 | 0 | for (Object object : objects) { |
286 | 0 | if (counter++ > 0) { |
287 | 0 | buffer.append(", "); |
288 | } |
|
289 | 0 | String text = (object == null) ? "null" : object.toString(); |
290 | 0 | buffer.append(text); |
291 | } |
|
292 | 0 | buffer.append("}"); |
293 | 0 | return buffer.toString(); |
294 | } |
|
295 | } |
|
296 | } |