View Javadoc

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          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          if (objectClass == typeClass) {
43              return true;
44          }
45          if (objectClass.isInterface()) {
46              if (typeClass.isInterface()) {
47                  // objectClass == interface, typeClass == interface
48                  Class<?>[] ifaces = objectClass.getInterfaces();
49                  for (Class<?> iface: ifaces) {
50                      if (iface == typeClass) {
51                          return true;
52                      }
53                  }
54              } else {
55                  // objectClass == interface, typeClass != interface
56                  Class<?>[] ifaces = typeClass.getInterfaces();
57                  for (Class<?> iface: ifaces) {
58                      if (iface == objectClass) {
59                          return true;
60                      }
61                  }
62              }
63          } else {
64              if (typeClass.isInterface()) {
65                  // objectClass != interface, typeClass == interface
66                  while (objectClass != null) {
67                      Class<?>[] ifaces = objectClass.getInterfaces();
68                      for (Class<?> iface: ifaces) {
69                          if (iface == typeClass) {
70                              return true;
71                          }
72                      }
73                      objectClass = objectClass.getSuperclass();
74                  }
75              } else {
76                  // objectClass != interface, typeClass != interface
77                  while (objectClass != null) {
78                      if (objectClass == typeClass) {
79                          return true;
80                      }
81                      objectClass = objectClass.getSuperclass();
82                  }
83              }
84          }
85          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          return str == null || str.trim().length() == 0;
97      }
98  
99      private Util() {}
100 }