|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.util; |
|
29 |
|
30 /** |
|
31 * A set of utility methods to help produce consistent |
|
32 * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods. |
|
33 * |
|
34 * |
|
35 * @since 4.0 |
|
36 */ |
|
37 public final class LangUtils { |
|
38 |
|
39 public static final int HASH_SEED = 17; |
|
40 public static final int HASH_OFFSET = 37; |
|
41 |
|
42 /** Disabled default constructor. */ |
|
43 private LangUtils() { |
|
44 } |
|
45 |
|
46 public static int hashCode(final int seed, final int hashcode) { |
|
47 return seed * HASH_OFFSET + hashcode; |
|
48 } |
|
49 |
|
50 public static int hashCode(final int seed, final boolean b) { |
|
51 return hashCode(seed, b ? 1 : 0); |
|
52 } |
|
53 |
|
54 public static int hashCode(final int seed, final Object obj) { |
|
55 return hashCode(seed, obj != null ? obj.hashCode() : 0); |
|
56 } |
|
57 |
|
58 /** |
|
59 * Check if two objects are equal. |
|
60 * |
|
61 * @param obj1 first object to compare, may be {@code null} |
|
62 * @param obj2 second object to compare, may be {@code null} |
|
63 * @return {@code true} if the objects are equal or both null |
|
64 */ |
|
65 public static boolean equals(final Object obj1, final Object obj2) { |
|
66 return obj1 == null ? obj2 == null : obj1.equals(obj2); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Check if two object arrays are equal. |
|
71 * <p> |
|
72 * <ul> |
|
73 * <li>If both parameters are null, return {@code true}</li> |
|
74 * <li>If one parameter is null, return {@code false}</li> |
|
75 * <li>If the array lengths are different, return {@code false}</li> |
|
76 * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li> |
|
77 * <li>Return {@code true}</li> |
|
78 * </ul> |
|
79 * |
|
80 * @param a1 first array to compare, may be {@code null} |
|
81 * @param a2 second array to compare, may be {@code null} |
|
82 * @return {@code true} if the arrays are equal or both null |
|
83 */ |
|
84 public static boolean equals(final Object[] a1, final Object[] a2) { |
|
85 if (a1 == null) { |
|
86 if (a2 == null) { |
|
87 return true; |
|
88 } else { |
|
89 return false; |
|
90 } |
|
91 } else { |
|
92 if (a2 != null && a1.length == a2.length) { |
|
93 for (int i = 0; i < a1.length; i++) { |
|
94 if (!equals(a1[i], a2[i])) { |
|
95 return false; |
|
96 } |
|
97 } |
|
98 return true; |
|
99 } else { |
|
100 return false; |
|
101 } |
|
102 } |
|
103 } |
|
104 |
|
105 } |