1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.util; |
---|
21 | |
---|
22 | /** |
---|
23 | * A set of utility methods to help produce consistent |
---|
24 | * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods. |
---|
25 | */ |
---|
26 | public final class LangUtils { |
---|
27 | |
---|
28 | public static final int HASH_SEED = 17; |
---|
29 | public static final int HASH_OFFSET = 37; |
---|
30 | |
---|
31 | /** Disabled default constructor. */ |
---|
32 | private LangUtils() { |
---|
33 | } |
---|
34 | |
---|
35 | public static int hashCode(final int seed, final int hashcode) { |
---|
36 | return seed * HASH_OFFSET + hashcode; |
---|
37 | } |
---|
38 | |
---|
39 | public static int hashCode(final int seed, final boolean b) { |
---|
40 | return hashCode(seed, b ? 1 : 0); |
---|
41 | } |
---|
42 | |
---|
43 | public static int hashCode(final int seed, final Object obj) { |
---|
44 | return hashCode(seed, obj != null ? obj.hashCode() : 0); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Check if two objects are equal. |
---|
49 | * |
---|
50 | * @param obj1 first object to compare, may be {@code null} |
---|
51 | * @param obj2 second object to compare, may be {@code null} |
---|
52 | * @return {@code true} if the objects are equal or both null |
---|
53 | */ |
---|
54 | public static boolean equals(final Object obj1, final Object obj2) { |
---|
55 | return obj1 == null ? obj2 == null : obj1.equals(obj2); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Check if two strings are equal, ignoring case considerations. |
---|
60 | * |
---|
61 | * @param s1 first string to compare, may be {@code null} |
---|
62 | * @param s2 second string to compare, may be {@code null} |
---|
63 | * @return {@code true} if the objects are equal or both null |
---|
64 | */ |
---|
65 | public static boolean equalsIgnoreCase(final String s1, final String s2) { |
---|
66 | return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2); |
---|
67 | } |
---|
68 | |
---|
69 | } |
---|