001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.core.util;
018:
019: import java.lang.reflect.Array;
020:
021: /**
022: * Miscellaneous object utility methods. Mainly for internal use within the
023: * framework; consider Jakarta's Commons Lang for a more comprehensive suite
024: * of object utilities.
025: *
026: * @author kimchy
027: */
028: public abstract class ObjectUtils {
029:
030: /**
031: * Determine if the given objects are equal, returning true if both are null
032: * or false if only one is null.
033: * @param o1 first Object to compare
034: * @param o2 second Object to compare
035: * @return whether the given objects are equal
036: */
037: public static boolean nullSafeEquals(Object o1, Object o2) {
038: return (o1 == o2 || (o1 != null && o1.equals(o2)));
039: }
040:
041: /**
042: * Return a hex string form of an object's identity hash code.
043: * @param o the object
044: * @return the object's identity code in hex
045: */
046: public static String getIdentityHexString(Object o) {
047: return Integer.toHexString(System.identityHashCode(o));
048: }
049:
050: /**
051: * Return whether the given throwable is a checked exception,
052: * i.e. an Exception but not a RuntimeException.
053: * @param ex the throwable to check
054: * @return whether the throwable is a checked exception
055: * @see java.lang.Exception
056: * @see java.lang.RuntimeException
057: */
058: public static boolean isCheckedException(Throwable ex) {
059: return (ex instanceof Exception)
060: && (!(ex instanceof RuntimeException));
061: }
062:
063: /**
064: * Check whether the given exception is compatible with the exceptions
065: * declared in a throws clause.
066: * @param ex the exception to checked
067: * @param declaredExceptions the exceptions declared in the throws clause
068: * @return whether the given exception is compatible
069: */
070: public static boolean isCompatibleWithThrowsClause(Throwable ex,
071: Class[] declaredExceptions) {
072: if (ex instanceof RuntimeException) {
073: return true;
074: }
075: if (declaredExceptions != null) {
076: for (int i = 0; i < declaredExceptions.length; i++) {
077: if (declaredExceptions[i].isAssignableFrom(ex
078: .getClass())) {
079: return true;
080: }
081: }
082: }
083: return false;
084: }
085:
086: /**
087: * Return whether the given array is empty: that is, null or of zero length.
088: * @param array the array to check
089: */
090: public static boolean isEmpty(Object[] array) {
091: return (array == null || array.length == 0);
092: }
093:
094: /**
095: * Convert a primitive array to an object array of primitive wrapper objects.
096: * @param primitiveArray the primitive array
097: * @return the object array
098: * @throws IllegalArgumentException if the parameter is not a primitive array
099: */
100: public static Object[] toObjectArray(Object primitiveArray) {
101: if (primitiveArray == null) {
102: return new Object[0];
103: }
104: Class clazz = primitiveArray.getClass();
105: Assert
106: .isTrue(clazz.isArray(),
107: "The specified parameter is not an array - it must be a primitive array.");
108: Assert.isTrue(clazz.getComponentType().isPrimitive(),
109: "The specified parameter is not a primitive array.");
110: int length = Array.getLength(primitiveArray);
111: if (length == 0) {
112: return new Object[0];
113: }
114: Class wrapperType = Array.get(primitiveArray, 0).getClass();
115: Object[] newArray = (Object[]) Array.newInstance(wrapperType,
116: length);
117: for (int i = 0; i < length; i++) {
118: newArray[i] = Array.get(primitiveArray, i);
119: }
120: return newArray;
121: }
122:
123: }
|