OSDN Git Service

Add BitUtils (from "Support multiple filters per association request")
authorEugene Susla <eugenesusla@google.com>
Fri, 24 Feb 2017 02:24:39 +0000 (18:24 -0800)
committerHugo Benichi <hugobenichi@google.com>
Thu, 20 Apr 2017 03:16:48 +0000 (12:16 +0900)
This patch is a cherry-pick of the BitUtils class from commit
36e866b8e0ec08e45b5e7fbc65aeeb3a9bb7b11e.

(cherry picked from commit 36e866b8e0ec08e45b5e7fbc65aeeb3a9bb7b11e)

Test: none
Change-Id: Iaf33929f6841db273a92d650e84287bf2964fa3d
Merged-In: I0a978787551a1ee5750ec5544b241d3bbfed5a7c

core/java/com/android/internal/util/BitUtils.java [new file with mode: 0644]

diff --git a/core/java/com/android/internal/util/BitUtils.java b/core/java/com/android/internal/util/BitUtils.java
new file mode 100644 (file)
index 0000000..a208ccb
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+
+package com.android.internal.util;
+
+import android.annotation.Nullable;
+
+import libcore.util.Objects;
+
+import java.util.Arrays;
+import java.util.UUID;
+
+public class BitUtils {
+    private BitUtils() {}
+
+    public static boolean maskedEquals(long a, long b, long mask) {
+        return (a & mask) == (b & mask);
+    }
+
+    public static boolean maskedEquals(byte a, byte b, byte mask) {
+        return (a & mask) == (b & mask);
+    }
+
+    public static boolean maskedEquals(byte[] a, byte[] b, @Nullable byte[] mask) {
+        if (a == null || b == null) return a == b;
+        Preconditions.checkArgument(a.length == b.length, "Inputs must be of same size");
+        if (mask == null) return Arrays.equals(a, b);
+        Preconditions.checkArgument(a.length == mask.length, "Mask must be of same size as inputs");
+        for (int i = 0; i < mask.length; i++) {
+            if (!maskedEquals(a[i], b[i], mask[i])) return false;
+        }
+        return true;
+    }
+
+    public static boolean maskedEquals(UUID a, UUID b, @Nullable UUID mask) {
+        if (mask == null) {
+            return Objects.equal(a, b);
+        }
+        return maskedEquals(a.getLeastSignificantBits(), b.getLeastSignificantBits(),
+                    mask.getLeastSignificantBits())
+                && maskedEquals(a.getMostSignificantBits(), b.getMostSignificantBits(),
+                    mask.getMostSignificantBits());
+    }
+}