001 /******************************************************************************* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 *******************************************************************************/ 019 package org.apache.commons.convert; 020 021 /** Utility methods. */ 022 public class Util { 023 024 /** Convenience method to cast parameterized types. 025 * 026 * @param <V> The type to cast to 027 * @param object The object to cast 028 * @return <code>obj</code> cast to type <code>V</code> 029 */ 030 @SuppressWarnings("unchecked") 031 public static <V> V cast(Object object) { 032 return (V) object; 033 } 034 035 /** 036 * Tests if a class is the same class as, or sub-class of, or implements <code>typeClass</code>. 037 * @param objectClass Class to test 038 * @param typeClass Class to test against 039 * @return <code>true</code> if <code>objectClass</code> is the same class as, or sub-class of, or implements <code>typeClass</code> 040 */ 041 public static boolean instanceOf(Class<?> objectClass, Class<?> typeClass) { 042 if (objectClass == typeClass) { 043 return true; 044 } 045 if (objectClass.isInterface()) { 046 if (typeClass.isInterface()) { 047 // objectClass == interface, typeClass == interface 048 Class<?>[] ifaces = objectClass.getInterfaces(); 049 for (Class<?> iface: ifaces) { 050 if (iface == typeClass) { 051 return true; 052 } 053 } 054 } else { 055 // objectClass == interface, typeClass != interface 056 Class<?>[] ifaces = typeClass.getInterfaces(); 057 for (Class<?> iface: ifaces) { 058 if (iface == objectClass) { 059 return true; 060 } 061 } 062 } 063 } else { 064 if (typeClass.isInterface()) { 065 // objectClass != interface, typeClass == interface 066 while (objectClass != null) { 067 Class<?>[] ifaces = objectClass.getInterfaces(); 068 for (Class<?> iface: ifaces) { 069 if (iface == typeClass) { 070 return true; 071 } 072 } 073 objectClass = objectClass.getSuperclass(); 074 } 075 } else { 076 // objectClass != interface, typeClass != interface 077 while (objectClass != null) { 078 if (objectClass == typeClass) { 079 return true; 080 } 081 objectClass = objectClass.getSuperclass(); 082 } 083 } 084 } 085 return false; 086 } 087 088 /** Returns <code>true</code> if <code>str</code> is <code>null</code> 089 * or empty. 090 * 091 * @param str The <code>String</code> to test 092 * @return <code>true</code> if <code>str</code> is <code>null</code> 093 * or empty 094 */ 095 public static boolean isEmpty(String str) { 096 return str == null || str.trim().length() == 0; 097 } 098 099 private Util() {} 100 }