001    /*
002     * Created on Jun 2, 2006
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005     * the License. You may obtain a copy of the License at
006     *
007     * http://www.apache.org/licenses/LICENSE-2.0
008     *
009     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010     * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011     * specific language governing permissions and limitations under the License.
012     *
013     * Copyright @2006-2011 the original author or authors.
014     */
015    package org.fest.util;
016    
017    import static org.fest.util.Arrays.*;
018    
019    import java.lang.reflect.Array;
020    
021    /**
022     * Utility methods related to objects.
023     *
024     * @author Alex Ruiz
025     * @author Joel Costigliola
026     */
027    public final class Objects {
028    
029      /** Prime number used to calculate the hash code of objects. */
030      public static final int HASH_CODE_PRIME = 31;
031    
032      /**
033       * Returns {@code true} if the given objects are equal or if both objects are {@code null}.
034       * @param o1 one of the objects to compare.
035       * @param o2 one of the objects to compare.
036       * @return {@code true} if the given objects are equal or if both objects are {@code null}.
037       */
038      public static boolean areEqual(Object o1, Object o2) {
039        if (o1 == null) return o2 == null;
040        if (o1.equals(o2)) return true;
041        return areEqualArrays(o1, o2);
042      }
043    
044      private static boolean areEqualArrays(Object o1, Object o2) {
045        if (!isArray(o1) || !isArray(o2)) return false;
046        if (o1 == o2) return true;
047        int size = Array.getLength(o1);
048        if (Array.getLength(o2) != size) return false;
049        for (int i = 0; i < size; i++) {
050          Object e1 = Array.get(o1, i);
051          Object e2 = Array.get(o2, i);
052          if (!areEqual(e1, e2)) return false;
053        }
054        return true;
055      }
056    
057      /**
058       * Returns an array containing the names of the given types.
059       * @param types the given types.
060       * @return the names of the given types stored in an array.
061       */
062      public static String[] namesOf(Class<?>... types) {
063        if (isEmpty(types)) return new String[0];
064        String[] names = new String[types.length];
065        for (int i = 0; i < types.length; i++)
066          names[i] = types[i].getName();
067        return names;
068      }
069    
070      /**
071       * Returns the hash code for the given object. If the object is {@code null}, this method returns zero. Otherwise
072       * calls the method {@code hashCode} of the given object.
073       * @param o the given object.
074       * @return the hash code for the given object
075       */
076      public static int hashCodeFor(Object o) {
077        return o != null ? o.hashCode() : 0;
078      }
079    
080      /**
081       * Casts the given object to the given type only if the object is of the given type. If the object is not of the given
082       * type, this method returns {@code null}.
083       * @param <T> the generic type to cast the given object to.
084       * @param o the object to cast.
085       * @param type the given type.
086       * @return the casted object, or {@code null} if the given object is not to the given type.
087       */
088      public static <T> T castIfBelongsToType(Object o, Class<T> type) {
089        if (o != null && type.isAssignableFrom(o.getClass())) return type.cast(o);
090        return null;
091      }
092    
093      private Objects() {}
094    }