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  import java.util.Objects;
31  
32  /**
33   * A set of utility methods to help produce consistent
34   * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods.
35   *
36   *
37   * @since 4.0
38   */
39  public final class LangUtils {
40  
41      public static final int HASH_SEED = 17;
42      public static final int HASH_OFFSET = 37;
43  
44      /** Disabled default constructor. */
45      private LangUtils() {
46      }
47  
48      public static int hashCode(final int seed, final int hashcode) {
49          return seed * HASH_OFFSET + hashcode;
50      }
51  
52      public static int hashCode(final int seed, final boolean b) {
53          return hashCode(seed, b ? 1 : 0);
54      }
55  
56      public static int hashCode(final int seed, final Object obj) {
57          return hashCode(seed, obj != null ? obj.hashCode() : 0);
58      }
59  
60      /**
61       * Check if two objects are equal.
62       *
63       * @param obj1 first object to compare, may be {@code null}
64       * @param obj2 second object to compare, may be {@code null}
65       * @return {@code true} if the objects are equal or both null
66       */
67      public static boolean equals(final Object obj1, final Object obj2) {
68          return Objects.equals(obj1, obj2);
69      }
70  
71      /**
72       * Check if two object arrays are equal.
73       * <ul>
74       * <li>If both parameters are null, return {@code true}</li>
75       * <li>If one parameter is null, return {@code false}</li>
76       * <li>If the array lengths are different, return {@code false}</li>
77       * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li>
78       * <li>Return {@code true}</li>
79       * </ul>
80       *
81       * @param a1 first array to compare, may be {@code null}
82       * @param a2 second array to compare, may be {@code null}
83       * @return {@code true} if the arrays are equal or both null
84       */
85      public static boolean equals(final Object[] a1, final Object[] a2) {
86          if (a1 == null) {
87              return a2 == null;
88          }
89          if (a2 != null && a1.length == a2.length) {
90              for (int i = 0; i < a1.length; i++) {
91                  if (!equals(a1[i], a2[i])) {
92                      return false;
93                  }
94              }
95              return true;
96          }
97          return false;
98      }
99  
100 }