001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.mime4j.util;
021
022/**
023 * A set of utility methods to help produce consistent
024 * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods.
025 */
026public final class LangUtils {
027
028    public static final int HASH_SEED = 17;
029    public static final int HASH_OFFSET = 37;
030
031    /** Disabled default constructor. */
032    private LangUtils() {
033    }
034
035    public static int hashCode(final int seed, final int hashcode) {
036        return seed * HASH_OFFSET + hashcode;
037    }
038
039    public static int hashCode(final int seed, final boolean b) {
040        return hashCode(seed, b ? 1 : 0);
041    }
042
043    public static int hashCode(final int seed, final Object obj) {
044        return hashCode(seed, obj != null ? obj.hashCode() : 0);
045    }
046
047    /**
048     * Check if two objects are equal.
049     *
050     * @param obj1 first object to compare, may be {@code null}
051     * @param obj2 second object to compare, may be {@code null}
052     * @return {@code true} if the objects are equal or both null
053     */
054    public static boolean equals(final Object obj1, final Object obj2) {
055        return obj1 == null ? obj2 == null : obj1.equals(obj2);
056    }
057
058    /**
059     * Check if two strings are equal, ignoring case considerations.
060     *
061     * @param s1 first string to compare, may be {@code null}
062     * @param s2 second string to compare, may be {@code null}
063     * @return {@code true} if the objects are equal or both null
064     */
065    public static boolean equalsIgnoreCase(final String s1, final String s2) {
066        return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
067    }
068
069}