View Javadoc
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 org.apache.hc.core5.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       * <ul>
72       * <li>If both parameters are null, return {@code true}</li>
73       * <li>If one parameter is null, return {@code false}</li>
74       * <li>If the array lengths are different, return {@code false}</li>
75       * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li>
76       * <li>Return {@code true}</li>
77       * </ul>
78       *
79       * @param a1 first array to compare, may be {@code null}
80       * @param a2 second array to compare, may be {@code null}
81       * @return {@code true} if the arrays are equal or both null
82       */
83      public static boolean equals(final Object[] a1, final Object[] a2) {
84          if (a1 == null) {
85              return a2 == null;
86          }
87          if (a2 != null && a1.length == a2.length) {
88              for (int i = 0; i < a1.length; i++) {
89                  if (!equals(a1[i], a2[i])) {
90                      return false;
91                  }
92              }
93              return true;
94          }
95          return false;
96      }
97  
98  }