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 the original author or authors.
014     */
015    package org.fest.util;
016    
017    import static org.fest.util.Arrays.isEmpty;
018    
019    /**
020     * Understands utility methods related to objects.
021     *
022     * @author Alex Ruiz
023     */
024    public final class Objects {
025    
026      /** Prime number used to calculate the hash code of objects. */
027      public static final int HASH_CODE_PRIME = 31;
028    
029      /**
030       * Returns <code>true</code> if the given objects are equal or if both objects are <code>null</code>.
031       * @param o1 one of the objects to compare.
032       * @param o2 one of the objects to compare.
033       * @return <code>true</code> if the given objects are equal or if both objects are <code>null</code>.
034       */
035      public static boolean areEqual(Object o1, Object o2) {
036        if (o1 == null) return o2 == null;
037        return o1.equals(o2);
038      }
039    
040      /**
041       * Returns an array containing the names of the given types.
042       * @param types the given types.
043       * @return the names of the given types stored in an array.
044       */
045      public static String[] namesOf(Class<?>... types) {
046        if (isEmpty(types)) return new String[0];
047        String[] names = new String[types.length];
048        for (int i = 0; i < types.length; i++) names[i] = types[i].getName();
049        return names;
050      }
051    
052      /**
053       * Returns the hash code for the given object. If the object is <code>null</code>, this method returns zero. Otherwise
054       * calls the method <code>hashCode</code> of the given object.
055       * @param o the given object.
056       * @return the hash code for the given object
057       */
058      public static int hashCodeFor(Object o) {
059        return o != null ? o.hashCode() : 0;
060      }
061    
062      /**
063       * 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
064       * type, this method returns <code>null</code>.
065       * @param <T> the generic type to cast the given object to.
066       * @param o the object to cast.
067       * @param type the given type.
068       * @return the casted object, or <code>null</code> if the given object is not to the given type.
069       */
070      public static <T> T castIfBelongsToType(Object o, Class<T> type) {
071        if (o != null && type.isAssignableFrom(o.getClass())) return type.cast(o);
072        return null;
073      }
074    
075      private Objects() {}
076    }