001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.util;
029
030/**
031 * A set of utility methods to help produce consistent
032 * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods.
033 *
034 *
035 * @since 4.0
036 */
037public final class LangUtils {
038
039    public static final int HASH_SEED = 17;
040    public static final int HASH_OFFSET = 37;
041
042    /** Disabled default constructor. */
043    private LangUtils() {
044    }
045
046    public static int hashCode(final int seed, final int hashcode) {
047        return seed * HASH_OFFSET + hashcode;
048    }
049
050    public static int hashCode(final int seed, final boolean b) {
051        return hashCode(seed, b ? 1 : 0);
052    }
053
054    public static int hashCode(final int seed, final Object obj) {
055        return hashCode(seed, obj != null ? obj.hashCode() : 0);
056    }
057
058    /**
059     * Check if two objects are equal.
060     *
061     * @param obj1 first object to compare, may be {@code null}
062     * @param obj2 second object to compare, may be {@code null}
063     * @return {@code true} if the objects are equal or both null
064     */
065    public static boolean equals(final Object obj1, final Object obj2) {
066        return obj1 == null ? obj2 == null : obj1.equals(obj2);
067    }
068
069    /**
070     * Check if two object arrays are equal.
071     * <ul>
072     * <li>If both parameters are null, return {@code true}</li>
073     * <li>If one parameter is null, return {@code false}</li>
074     * <li>If the array lengths are different, return {@code false}</li>
075     * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li>
076     * <li>Return {@code true}</li>
077     * </ul>
078     *
079     * @param a1 first array to compare, may be {@code null}
080     * @param a2 second array to compare, may be {@code null}
081     * @return {@code true} if the arrays are equal or both null
082     */
083    public static boolean equals(final Object[] a1, final Object[] a2) {
084        if (a1 == null) {
085            return a2 == null;
086        } else {
087            if (a2 != null && a1.length == a2.length) {
088                for (int i = 0; i < a1.length; i++) {
089                    if (!equals(a1[i], a2[i])) {
090                        return false;
091                    }
092                }
093                return true;
094            } else {
095                return false;
096            }
097        }
098    }
099
100}