1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tools.layoutlib.java; 18 19 import java.util.Arrays; 20 import java.util.Comparator; 21 22 /** 23 * Defines the same class as the java.util.Objects which is added in Java 7. 24 * This hack makes it possible to run the Android code which uses Java 7 features 25 * (API 18 and beyond) to run on Java 6. 26 * <p/> 27 * Extracted from API level 19, file: 28 * platform/libcore/luni/src/main/java/java/util/Objects.java 29 */ 30 public final class Objects { Objects()31 private Objects() {} 32 33 /** 34 * Returns 0 if {@code a == b}, or {@code c.compare(a, b)} otherwise. 35 * That is, this makes {@code c} null-safe. 36 */ compare(T a, T b, Comparator<? super T> c)37 public static <T> int compare(T a, T b, Comparator<? super T> c) { 38 if (a == b) { 39 return 0; 40 } 41 return c.compare(a, b); 42 } 43 44 /** 45 * Returns true if both arguments are null, 46 * the result of {@link Arrays#equals} if both arguments are primitive arrays, 47 * the result of {@link Arrays#deepEquals} if both arguments are arrays of reference types, 48 * and the result of {@link #equals} otherwise. 49 */ deepEquals(Object a, Object b)50 public static boolean deepEquals(Object a, Object b) { 51 if (a == null || b == null) { 52 return a == b; 53 } else if (a instanceof Object[] && b instanceof Object[]) { 54 return Arrays.deepEquals((Object[]) a, (Object[]) b); 55 } else if (a instanceof boolean[] && b instanceof boolean[]) { 56 return Arrays.equals((boolean[]) a, (boolean[]) b); 57 } else if (a instanceof byte[] && b instanceof byte[]) { 58 return Arrays.equals((byte[]) a, (byte[]) b); 59 } else if (a instanceof char[] && b instanceof char[]) { 60 return Arrays.equals((char[]) a, (char[]) b); 61 } else if (a instanceof double[] && b instanceof double[]) { 62 return Arrays.equals((double[]) a, (double[]) b); 63 } else if (a instanceof float[] && b instanceof float[]) { 64 return Arrays.equals((float[]) a, (float[]) b); 65 } else if (a instanceof int[] && b instanceof int[]) { 66 return Arrays.equals((int[]) a, (int[]) b); 67 } else if (a instanceof long[] && b instanceof long[]) { 68 return Arrays.equals((long[]) a, (long[]) b); 69 } else if (a instanceof short[] && b instanceof short[]) { 70 return Arrays.equals((short[]) a, (short[]) b); 71 } 72 return a.equals(b); 73 } 74 75 /** 76 * Null-safe equivalent of {@code a.equals(b)}. 77 */ equals(Object a, Object b)78 public static boolean equals(Object a, Object b) { 79 return (a == null) ? (b == null) : a.equals(b); 80 } 81 82 /** 83 * Convenience wrapper for {@link Arrays#hashCode}, adding varargs. 84 * This can be used to compute a hash code for an object's fields as follows: 85 * {@code Objects.hash(a, b, c)}. 86 */ hash(Object... values)87 public static int hash(Object... values) { 88 return Arrays.hashCode(values); 89 } 90 91 /** 92 * Returns 0 for null or {@code o.hashCode()}. 93 */ hashCode(Object o)94 public static int hashCode(Object o) { 95 return (o == null) ? 0 : o.hashCode(); 96 } 97 98 /** 99 * Returns {@code o} if non-null, or throws {@code NullPointerException}. 100 */ requireNonNull(T o)101 public static <T> T requireNonNull(T o) { 102 if (o == null) { 103 throw new NullPointerException(); 104 } 105 return o; 106 } 107 108 /** 109 * Returns {@code o} if non-null, or throws {@code NullPointerException} 110 * with the given detail message. 111 */ requireNonNull(T o, String message)112 public static <T> T requireNonNull(T o, String message) { 113 if (o == null) { 114 throw new NullPointerException(message); 115 } 116 return o; 117 } 118 119 /** 120 * Returns "null" for null or {@code o.toString()}. 121 */ toString(Object o)122 public static String toString(Object o) { 123 return (o == null) ? "null" : o.toString(); 124 } 125 126 /** 127 * Returns {@code nullString} for null or {@code o.toString()}. 128 */ toString(Object o, String nullString)129 public static String toString(Object o, String nullString) { 130 return (o == null) ? nullString : o.toString(); 131 } 132 }