Yifan Hong | 8ef5fead | 2017-03-27 13:02:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 android.os; |
| 18 | |
| 19 | import java.util.Arrays; |
| 20 | import java.util.Collection; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.List; |
| 23 | import java.util.Map; |
| 24 | import java.util.Objects; |
| 25 | import java.util.stream.IntStream; |
| 26 | |
| 27 | /** @hide */ |
| 28 | public class HidlSupport { |
| 29 | /** |
| 30 | * Similar to Objects.deepEquals, but also take care of lists. |
| 31 | * Two objects of HIDL types are considered equal if: |
| 32 | * 1. Both null |
| 33 | * 2. Both non-null, and of the same class, and: |
| 34 | * 2.1 Both are primitive arrays / enum arrays, elements are equal using == check |
| 35 | * 2.2 Both are object arrays, elements are checked recursively |
| 36 | * 2.3 Both are Lists, elements are checked recursively |
| 37 | * 2.4 (If both are collections other than lists or maps, throw an error) |
| 38 | * 2.5 lft.equals(rgt) returns true |
| 39 | */ |
| 40 | public static boolean deepEquals(Object lft, Object rgt) { |
| 41 | if (lft == rgt) { |
| 42 | return true; |
| 43 | } |
| 44 | if (lft == null || rgt == null) { |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | Class<?> lftClazz = lft.getClass(); |
| 49 | Class<?> rgtClazz = rgt.getClass(); |
| 50 | if (lftClazz != rgtClazz) { |
| 51 | return false; |
| 52 | } |
| 53 | |
| 54 | if (lftClazz.isArray()) { |
| 55 | Class<?> lftElementType = lftClazz.getComponentType(); |
| 56 | if (lftElementType != rgtClazz.getComponentType()) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (lftElementType.isPrimitive()) { |
| 61 | return Objects.deepEquals(lft, rgt); |
| 62 | } |
| 63 | |
| 64 | Object[] lftArray = (Object[])lft; |
| 65 | Object[] rgtArray = (Object[])rgt; |
| 66 | return (lftArray.length == rgtArray.length) && |
| 67 | IntStream.range(0, lftArray.length).allMatch( |
| 68 | i -> deepEquals(lftArray[i], rgtArray[i])); |
| 69 | } |
| 70 | |
| 71 | if (lft instanceof List<?>) { |
| 72 | List<Object> lftList = (List<Object>)lft; |
| 73 | List<Object> rgtList = (List<Object>)rgt; |
| 74 | if (lftList.size() != rgtList.size()) { |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | Iterator<Object> lftIter = lftList.iterator(); |
| 79 | return rgtList.stream() |
| 80 | .allMatch(rgtElement -> deepEquals(lftIter.next(), rgtElement)); |
| 81 | } |
| 82 | |
| 83 | throwErrorIfUnsupportedType(lft); |
| 84 | |
| 85 | return lft.equals(rgt); |
| 86 | } |
| 87 | |
| 88 | /** |
Steven Moreland | 4bfa2eb | 2018-01-05 09:41:10 -0800 | [diff] [blame] | 89 | * Class which can be used to fetch an object out of a lambda. Fetching an object |
| 90 | * out of a local scope with HIDL is a common operation (although usually it can |
| 91 | * and should be avoided). |
| 92 | * |
| 93 | * @param <E> Inner object type. |
| 94 | */ |
| 95 | public static final class Mutable<E> { |
| 96 | public E value; |
| 97 | |
| 98 | public Mutable() { |
| 99 | value = null; |
| 100 | } |
| 101 | |
| 102 | public Mutable(E value) { |
| 103 | this.value = value; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
Yifan Hong | 8ef5fead | 2017-03-27 13:02:34 -0700 | [diff] [blame] | 108 | * Similar to Arrays.deepHashCode, but also take care of lists. |
| 109 | */ |
| 110 | public static int deepHashCode(Object o) { |
| 111 | if (o == null) { |
| 112 | return 0; |
| 113 | } |
| 114 | Class<?> clazz = o.getClass(); |
| 115 | if (clazz.isArray()) { |
| 116 | Class<?> elementType = clazz.getComponentType(); |
| 117 | if (elementType.isPrimitive()) { |
| 118 | return primitiveArrayHashCode(o); |
| 119 | } |
| 120 | return Arrays.hashCode(Arrays.stream((Object[])o) |
| 121 | .mapToInt(element -> deepHashCode(element)) |
| 122 | .toArray()); |
| 123 | } |
| 124 | |
| 125 | if (o instanceof List<?>) { |
| 126 | return Arrays.hashCode(((List<Object>)o).stream() |
| 127 | .mapToInt(element -> deepHashCode(element)) |
| 128 | .toArray()); |
| 129 | } |
| 130 | |
| 131 | throwErrorIfUnsupportedType(o); |
| 132 | |
| 133 | return o.hashCode(); |
| 134 | } |
| 135 | |
| 136 | private static void throwErrorIfUnsupportedType(Object o) { |
| 137 | if (o instanceof Collection<?> && !(o instanceof List<?>)) { |
| 138 | throw new UnsupportedOperationException( |
| 139 | "Cannot check equality on collections other than lists: " + |
| 140 | o.getClass().getName()); |
| 141 | } |
| 142 | |
| 143 | if (o instanceof Map<?, ?>) { |
| 144 | throw new UnsupportedOperationException( |
| 145 | "Cannot check equality on maps"); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private static int primitiveArrayHashCode(Object o) { |
| 150 | Class<?> elementType = o.getClass().getComponentType(); |
| 151 | if (elementType == boolean.class) { |
| 152 | return Arrays.hashCode(((boolean[])o)); |
| 153 | } |
| 154 | if (elementType == byte.class) { |
| 155 | return Arrays.hashCode(((byte[])o)); |
| 156 | } |
| 157 | if (elementType == char.class) { |
| 158 | return Arrays.hashCode(((char[])o)); |
| 159 | } |
| 160 | if (elementType == double.class) { |
| 161 | return Arrays.hashCode(((double[])o)); |
| 162 | } |
| 163 | if (elementType == float.class) { |
| 164 | return Arrays.hashCode(((float[])o)); |
| 165 | } |
| 166 | if (elementType == int.class) { |
| 167 | return Arrays.hashCode(((int[])o)); |
| 168 | } |
| 169 | if (elementType == long.class) { |
| 170 | return Arrays.hashCode(((long[])o)); |
| 171 | } |
| 172 | if (elementType == short.class) { |
| 173 | return Arrays.hashCode(((short[])o)); |
| 174 | } |
| 175 | // Should not reach here. |
| 176 | throw new UnsupportedOperationException(); |
| 177 | } |
Yifan Hong | 73b6c27 | 2017-10-31 17:32:15 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
| 180 | * Test that two interfaces are equal. This is the Java equivalent to C++ |
| 181 | * interfacesEqual function. |
| 182 | * This essentially calls .equals on the internal binder objects (via Binder()). |
| 183 | * - If both interfaces are proxies, asBinder() returns a {@link HwRemoteBinder} |
| 184 | * object, and they are compared in {@link HwRemoteBinder#equals}. |
| 185 | * - If both interfaces are stubs, asBinder() returns the object itself. By default, |
| 186 | * auto-generated IFoo.Stub does not override equals(), but an implementation can |
| 187 | * optionally override it, and {@code interfacesEqual} will use it here. |
| 188 | */ |
| 189 | public static boolean interfacesEqual(IHwInterface lft, Object rgt) { |
| 190 | if (lft == rgt) { |
| 191 | return true; |
| 192 | } |
| 193 | if (lft == null || rgt == null) { |
| 194 | return false; |
| 195 | } |
| 196 | if (!(rgt instanceof IHwInterface)) { |
| 197 | return false; |
| 198 | } |
| 199 | return Objects.equals(lft.asBinder(), ((IHwInterface) rgt).asBinder()); |
| 200 | } |
Yifan Hong | bb0bd00 | 2017-11-14 16:14:04 -0800 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * Return PID of process if sharable to clients. |
| 204 | */ |
| 205 | public static native int getPidIfSharable(); |
Yifan Hong | 8ef5fead | 2017-03-27 13:02:34 -0700 | [diff] [blame] | 206 | } |