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