Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Util |
|
| 5.75;5.75 |
1 | /******************************************************************************* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | *******************************************************************************/ | |
19 | package org.apache.commons.convert; | |
20 | ||
21 | /** Utility methods. */ | |
22 | public class Util { | |
23 | ||
24 | /** Convenience method to cast parameterized types. | |
25 | * | |
26 | * @param <V> The type to cast to | |
27 | * @param object The object to cast | |
28 | * @return <code>obj</code> cast to type <code>V</code> | |
29 | */ | |
30 | @SuppressWarnings("unchecked") | |
31 | public static <V> V cast(Object object) { | |
32 | 382 | return (V) object; |
33 | } | |
34 | ||
35 | /** | |
36 | * Tests if a class is the same class as, or sub-class of, or implements <code>typeClass</code>. | |
37 | * @param objectClass Class to test | |
38 | * @param typeClass Class to test against | |
39 | * @return <code>true</code> if <code>objectClass</code> is the same class as, or sub-class of, or implements <code>typeClass</code> | |
40 | */ | |
41 | public static boolean instanceOf(Class<?> objectClass, Class<?> typeClass) { | |
42 | 10033 | if (objectClass == typeClass) { |
43 | 375 | return true; |
44 | } | |
45 | 9658 | if (objectClass.isInterface()) { |
46 | 1562 | if (typeClass.isInterface()) { |
47 | // objectClass == interface, typeClass == interface | |
48 | 63 | Class<?>[] ifaces = objectClass.getInterfaces(); |
49 | 113 | for (Class<?> iface: ifaces) { |
50 | 61 | if (iface == typeClass) { |
51 | 11 | return true; |
52 | } | |
53 | } | |
54 | 52 | } else { |
55 | // objectClass == interface, typeClass != interface | |
56 | 1499 | Class<?>[] ifaces = typeClass.getInterfaces(); |
57 | 3663 | for (Class<?> iface: ifaces) { |
58 | 2165 | if (iface == objectClass) { |
59 | 1 | return true; |
60 | } | |
61 | } | |
62 | 1498 | } |
63 | } else { | |
64 | 8096 | if (typeClass.isInterface()) { |
65 | // objectClass != interface, typeClass == interface | |
66 | 1121 | while (objectClass != null) { |
67 | 824 | Class<?>[] ifaces = objectClass.getInterfaces(); |
68 | 1593 | for (Class<?> iface: ifaces) { |
69 | 780 | if (iface == typeClass) { |
70 | 11 | return true; |
71 | } | |
72 | } | |
73 | 813 | objectClass = objectClass.getSuperclass(); |
74 | 813 | } |
75 | } else { | |
76 | // objectClass != interface, typeClass != interface | |
77 | 29026 | while (objectClass != null) { |
78 | 21352 | if (objectClass == typeClass) { |
79 | 114 | return true; |
80 | } | |
81 | 21238 | objectClass = objectClass.getSuperclass(); |
82 | } | |
83 | } | |
84 | } | |
85 | 9521 | return false; |
86 | } | |
87 | ||
88 | /** Returns <code>true</code> if <code>str</code> is <code>null</code> | |
89 | * or empty. | |
90 | * | |
91 | * @param str The <code>String</code> to test | |
92 | * @return <code>true</code> if <code>str</code> is <code>null</code> | |
93 | * or empty | |
94 | */ | |
95 | public static boolean isEmpty(String str) { | |
96 | 11 | return str == null || str.trim().length() == 0; |
97 | } | |
98 | ||
99 | 1 | private Util() {} |
100 | } |