| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.os; |
| 18 |
|
| 19 |
import android.annotation.SystemApi; |
| 20 |
|
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.Collection; |
| 23 |
import java.util.Iterator; |
| 24 |
import java.util.List; |
| 25 |
import java.util.Map; |
| 26 |
import java.util.Objects; |
| 27 |
import java.util.stream.IntStream; |
| 28 |
|
| 29 |
|
| 30 |
@SystemApi |
| 31 |
public class HidlSupport { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
@SystemApi |
| 45 |
public static boolean deepEquals(Object lft, Object rgt) { |
| 46 |
if (lft == rgt) { |
| 47 |
return true; |
| 48 |
} |
| 49 |
if (lft == null || rgt == null) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
Class<?> lftClazz = lft.getClass(); |
| 54 |
Class<?> rgtClazz = rgt.getClass(); |
| 55 |
if (lftClazz != rgtClazz) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
if (lftClazz.isArray()) { |
| 60 |
Class<?> lftElementType = lftClazz.getComponentType(); |
| 61 |
if (lftElementType != rgtClazz.getComponentType()) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if (lftElementType.isPrimitive()) { |
| 66 |
return Objects.deepEquals(lft, rgt); |
| 67 |
} |
| 68 |
|
| 69 |
Object[] lftArray = (Object[])lft; |
| 70 |
Object[] rgtArray = (Object[])rgt; |
| 71 |
return (lftArray.length == rgtArray.length) && |
| 72 |
IntStream.range(0, lftArray.length).allMatch( |
| 73 |
i -> deepEquals(lftArray[i], rgtArray[i])); |
| 74 |
} |
| 75 |
|
| 76 |
if (lft instanceof List<?>) { |
| 77 |
List<Object> lftList = (List<Object>)lft; |
| 78 |
List<Object> rgtList = (List<Object>)rgt; |
| 79 |
if (lftList.size() != rgtList.size()) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
Iterator<Object> lftIter = lftList.iterator(); |
| 84 |
return rgtList.stream() |
| 85 |
.allMatch(rgtElement -> deepEquals(lftIter.next(), rgtElement)); |
| 86 |
} |
| 87 |
|
| 88 |
throwErrorIfUnsupportedType(lft); |
| 89 |
|
| 90 |
return lft.equals(rgt); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static final class Mutable<E> { |
| 102 |
public E value; |
| 103 |
|
| 104 |
public Mutable() { |
| 105 |
value = null; |
| 106 |
} |
| 107 |
|
| 108 |
public Mutable(E value) { |
| 109 |
this.value = value; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@SystemApi |
| 118 |
public static int deepHashCode(Object o) { |
| 119 |
if (o == null) { |
| 120 |
return 0; |
| 121 |
} |
| 122 |
Class<?> clazz = o.getClass(); |
| 123 |
if (clazz.isArray()) { |
| 124 |
Class<?> elementType = clazz.getComponentType(); |
| 125 |
if (elementType.isPrimitive()) { |
| 126 |
return primitiveArrayHashCode(o); |
| 127 |
} |
| 128 |
return Arrays.hashCode(Arrays.stream((Object[])o) |
| 129 |
.mapToInt(element -> deepHashCode(element)) |
| 130 |
.toArray()); |
| 131 |
} |
| 132 |
|
| 133 |
if (o instanceof List<?>) { |
| 134 |
return Arrays.hashCode(((List<Object>)o).stream() |
| 135 |
.mapToInt(element -> deepHashCode(element)) |
| 136 |
.toArray()); |
| 137 |
} |
| 138 |
|
| 139 |
throwErrorIfUnsupportedType(o); |
| 140 |
|
| 141 |
return o.hashCode(); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
private static void throwErrorIfUnsupportedType(Object o) { |
| 146 |
if (o instanceof Collection<?> && !(o instanceof List<?>)) { |
| 147 |
throw new UnsupportedOperationException( |
| 148 |
"Cannot check equality on collections other than lists: " + |
| 149 |
o.getClass().getName()); |
| 150 |
} |
| 151 |
|
| 152 |
if (o instanceof Map<?, ?>) { |
| 153 |
throw new UnsupportedOperationException( |
| 154 |
"Cannot check equality on maps"); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
private static int primitiveArrayHashCode(Object o) { |
| 160 |
Class<?> elementType = o.getClass().getComponentType(); |
| 161 |
if (elementType == boolean.class) { |
| 162 |
return Arrays.hashCode(((boolean[])o)); |
| 163 |
} |
| 164 |
if (elementType == byte.class) { |
| 165 |
return Arrays.hashCode(((byte[])o)); |
| 166 |
} |
| 167 |
if (elementType == char.class) { |
| 168 |
return Arrays.hashCode(((char[])o)); |
| 169 |
} |
| 170 |
if (elementType == double.class) { |
| 171 |
return Arrays.hashCode(((double[])o)); |
| 172 |
} |
| 173 |
if (elementType == float.class) { |
| 174 |
return Arrays.hashCode(((float[])o)); |
| 175 |
} |
| 176 |
if (elementType == int.class) { |
| 177 |
return Arrays.hashCode(((int[])o)); |
| 178 |
} |
| 179 |
if (elementType == long.class) { |
| 180 |
return Arrays.hashCode(((long[])o)); |
| 181 |
} |
| 182 |
if (elementType == short.class) { |
| 183 |
return Arrays.hashCode(((short[])o)); |
| 184 |
} |
| 185 |
|
| 186 |
throw new UnsupportedOperationException(); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
@SystemApi |
| 201 |
public static boolean interfacesEqual(IHwInterface lft, Object rgt) { |
| 202 |
if (lft == rgt) { |
| 203 |
return true; |
| 204 |
} |
| 205 |
if (lft == null || rgt == null) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
if (!(rgt instanceof IHwInterface)) { |
| 209 |
return false; |
| 210 |
} |
| 211 |
return Objects.equals(lft.asBinder(), ((IHwInterface) rgt).asBinder()); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
@SystemApi |
| 219 |
public static native int getPidIfSharable(); |
| 220 |
|
| 221 |
|
| 222 |
public HidlSupport() {} |
| 223 |
} |