1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/util/LangUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.util; 1.32 + 1.33 +/** 1.34 + * A set of utility methods to help produce consistent 1.35 + * {@link Object#equals equals} and {@link Object#hashCode hashCode} methods. 1.36 + * 1.37 + * 1.38 + * @since 4.0 1.39 + */ 1.40 +public final class LangUtils { 1.41 + 1.42 + public static final int HASH_SEED = 17; 1.43 + public static final int HASH_OFFSET = 37; 1.44 + 1.45 + /** Disabled default constructor. */ 1.46 + private LangUtils() { 1.47 + } 1.48 + 1.49 + public static int hashCode(final int seed, final int hashcode) { 1.50 + return seed * HASH_OFFSET + hashcode; 1.51 + } 1.52 + 1.53 + public static int hashCode(final int seed, final boolean b) { 1.54 + return hashCode(seed, b ? 1 : 0); 1.55 + } 1.56 + 1.57 + public static int hashCode(final int seed, final Object obj) { 1.58 + return hashCode(seed, obj != null ? obj.hashCode() : 0); 1.59 + } 1.60 + 1.61 + /** 1.62 + * Check if two objects are equal. 1.63 + * 1.64 + * @param obj1 first object to compare, may be {@code null} 1.65 + * @param obj2 second object to compare, may be {@code null} 1.66 + * @return {@code true} if the objects are equal or both null 1.67 + */ 1.68 + public static boolean equals(final Object obj1, final Object obj2) { 1.69 + return obj1 == null ? obj2 == null : obj1.equals(obj2); 1.70 + } 1.71 + 1.72 + /** 1.73 + * Check if two object arrays are equal. 1.74 + * <p> 1.75 + * <ul> 1.76 + * <li>If both parameters are null, return {@code true}</li> 1.77 + * <li>If one parameter is null, return {@code false}</li> 1.78 + * <li>If the array lengths are different, return {@code false}</li> 1.79 + * <li>Compare array elements using .equals(); return {@code false} if any comparisons fail.</li> 1.80 + * <li>Return {@code true}</li> 1.81 + * </ul> 1.82 + * 1.83 + * @param a1 first array to compare, may be {@code null} 1.84 + * @param a2 second array to compare, may be {@code null} 1.85 + * @return {@code true} if the arrays are equal or both null 1.86 + */ 1.87 + public static boolean equals(final Object[] a1, final Object[] a2) { 1.88 + if (a1 == null) { 1.89 + if (a2 == null) { 1.90 + return true; 1.91 + } else { 1.92 + return false; 1.93 + } 1.94 + } else { 1.95 + if (a2 != null && a1.length == a2.length) { 1.96 + for (int i = 0; i < a1.length; i++) { 1.97 + if (!equals(a1[i], a2[i])) { 1.98 + return false; 1.99 + } 1.100 + } 1.101 + return true; 1.102 + } else { 1.103 + return false; 1.104 + } 1.105 + } 1.106 + } 1.107 + 1.108 +}