michael@0: /*
michael@0: * ====================================================================
michael@0: * Licensed to the Apache Software Foundation (ASF) under one
michael@0: * or more contributor license agreements. See the NOTICE file
michael@0: * distributed with this work for additional information
michael@0: * regarding copyright ownership. The ASF licenses this file
michael@0: * to you under the Apache License, Version 2.0 (the
michael@0: * "License"); you may not use this file except in compliance
michael@0: * with the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing,
michael@0: * software distributed under the License is distributed on an
michael@0: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
michael@0: * KIND, either express or implied. See the License for the
michael@0: * specific language governing permissions and limitations
michael@0: * under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: * .
michael@0: *
michael@0: */
michael@0:
michael@0: package ch.boye.httpclientandroidlib.util;
michael@0:
michael@0: /**
michael@0: * A set of utility methods to help produce consistent
michael@0: * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods.
michael@0: *
michael@0: *
michael@0: * @since 4.0
michael@0: */
michael@0: public final class LangUtils {
michael@0:
michael@0: public static final int HASH_SEED = 17;
michael@0: public static final int HASH_OFFSET = 37;
michael@0:
michael@0: /** Disabled default constructor. */
michael@0: private LangUtils() {
michael@0: }
michael@0:
michael@0: public static int hashCode(final int seed, final int hashcode) {
michael@0: return seed * HASH_OFFSET + hashcode;
michael@0: }
michael@0:
michael@0: public static int hashCode(final int seed, final boolean b) {
michael@0: return hashCode(seed, b ? 1 : 0);
michael@0: }
michael@0:
michael@0: public static int hashCode(final int seed, final Object obj) {
michael@0: return hashCode(seed, obj != null ? obj.hashCode() : 0);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Check if two objects are equal.
michael@0: *
michael@0: * @param obj1 first object to compare, may be {@code null}
michael@0: * @param obj2 second object to compare, may be {@code null}
michael@0: * @return {@code true} if the objects are equal or both null
michael@0: */
michael@0: public static boolean equals(final Object obj1, final Object obj2) {
michael@0: return obj1 == null ? obj2 == null : obj1.equals(obj2);
michael@0: }
michael@0:
michael@0: /**
michael@0: * Check if two object arrays are equal.
michael@0: *
michael@0: *
michael@0: * - If both parameters are null, return {@code true}
michael@0: * - If one parameter is null, return {@code false}
michael@0: * - If the array lengths are different, return {@code false}
michael@0: * - Compare array elements using .equals(); return {@code false} if any comparisons fail.
michael@0: * - Return {@code true}
michael@0: *
michael@0: *
michael@0: * @param a1 first array to compare, may be {@code null}
michael@0: * @param a2 second array to compare, may be {@code null}
michael@0: * @return {@code true} if the arrays are equal or both null
michael@0: */
michael@0: public static boolean equals(final Object[] a1, final Object[] a2) {
michael@0: if (a1 == null) {
michael@0: if (a2 == null) {
michael@0: return true;
michael@0: } else {
michael@0: return false;
michael@0: }
michael@0: } else {
michael@0: if (a2 != null && a1.length == a2.length) {
michael@0: for (int i = 0; i < a1.length; i++) {
michael@0: if (!equals(a1[i], a2[i])) {
michael@0: return false;
michael@0: }
michael@0: }
michael@0: return true;
michael@0: } else {
michael@0: return false;
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: }