Merge "ASL allow DataType to be easily compared (for lint annotation logic)" into main
diff --git a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/marshallable/DataType.java b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/marshallable/DataType.java
index 97304cb..d2326d1 100644
--- a/tools/app_metadata_bundles/src/lib/java/com/android/asllib/marshallable/DataType.java
+++ b/tools/app_metadata_bundles/src/lib/java/com/android/asllib/marshallable/DataType.java
@@ -21,7 +21,9 @@
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 
+import java.util.HashSet;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 
 /**
@@ -76,6 +78,43 @@
         }
     }
 
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null || obj.getClass() != this.getClass()) {
+            return false;
+        }
+        DataType objAsDataType = (DataType) obj;
+        return Objects.equals(this.mDataTypeName, objAsDataType.mDataTypeName)
+                && Objects.equals(
+                        new HashSet<>(this.mPurposes), new HashSet<>(objAsDataType.mPurposes))
+                && Objects.equals(this.mIsCollectionOptional, objAsDataType.mIsCollectionOptional)
+                && Objects.equals(this.mIsSharingOptional, objAsDataType.mIsSharingOptional)
+                && Objects.equals(this.mEphemeral, objAsDataType.mEphemeral);
+    }
+
+    @Override
+    public int hashCode() {
+        int result = 1;
+        int prime = 31;
+        result =
+                (prime * result) + (this.mDataTypeName != null ? this.mDataTypeName.hashCode() : 0);
+        result =
+                (prime * result)
+                        + (this.mPurposes != null ? new HashSet<>(this.mPurposes).hashCode() : 0);
+        result =
+                (prime * result)
+                        + (this.mIsCollectionOptional != null
+                                ? this.mIsCollectionOptional.hashCode()
+                                : 0);
+        result =
+                (prime * result)
+                        + (this.mIsSharingOptional != null
+                                ? this.mIsSharingOptional.hashCode()
+                                : 0);
+        result = (prime * result) + (this.mEphemeral != null ? this.mEphemeral.hashCode() : 0);
+        return result;
+    }
+
     private final String mDataTypeName;
 
     private final List<Purpose> mPurposes;
diff --git a/tools/app_metadata_bundles/src/test/java/com/android/asllib/AllTests.java b/tools/app_metadata_bundles/src/test/java/com/android/asllib/AllTests.java
index 54c80f6..f156484 100644
--- a/tools/app_metadata_bundles/src/test/java/com/android/asllib/AllTests.java
+++ b/tools/app_metadata_bundles/src/test/java/com/android/asllib/AllTests.java
@@ -20,6 +20,7 @@
 import com.android.asllib.marshallable.AppInfoTest;
 import com.android.asllib.marshallable.DataCategoryTest;
 import com.android.asllib.marshallable.DataLabelsTest;
+import com.android.asllib.marshallable.DataTypeEqualityTest;
 import com.android.asllib.marshallable.DeveloperInfoTest;
 import com.android.asllib.marshallable.SafetyLabelsTest;
 import com.android.asllib.marshallable.SecurityLabelsTest;
@@ -37,6 +38,7 @@
     AppInfoTest.class,
     DataCategoryTest.class,
     DataLabelsTest.class,
+    DataTypeEqualityTest.class,
     DeveloperInfoTest.class,
     SafetyLabelsTest.class,
     SecurityLabelsTest.class,
diff --git a/tools/app_metadata_bundles/src/test/java/com/android/asllib/marshallable/DataTypeEqualityTest.java b/tools/app_metadata_bundles/src/test/java/com/android/asllib/marshallable/DataTypeEqualityTest.java
new file mode 100644
index 0000000..da7f287
--- /dev/null
+++ b/tools/app_metadata_bundles/src/test/java/com/android/asllib/marshallable/DataTypeEqualityTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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.asllib.marshallable;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+@RunWith(JUnit4.class)
+public class DataTypeEqualityTest {
+
+    public static final List<String> OPTIONAL_FIELD_NAMES =
+            List.of("isDataDeletable", "isDataEncrypted");
+    public static final List<String> OPTIONAL_FIELD_NAMES_OD =
+            List.of("is_data_deletable", "is_data_encrypted");
+
+    /** Logic for setting up tests (empty if not yet needed). */
+    public static void main(String[] params) throws Exception {}
+
+    @Before
+    public void setUp() throws Exception {
+        System.out.println("set up.");
+    }
+
+    /** Test for equality different order. */
+    @Test
+    public void testEqualityDifferentOrder() throws Exception {
+        System.out.println("starting testEqualityDifferentOrder.");
+        DataType dataType1 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        DataType dataType2 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.PERSONALIZATION, DataType.Purpose.ADVERTISING),
+                        true,
+                        false,
+                        null);
+        assertEquals(dataType1, dataType2);
+        assertEquals(dataType2, dataType1);
+    }
+
+    /** Test for contains different order. */
+    @Test
+    public void testContainsDifferentOrder() throws Exception {
+        System.out.println("starting testContainsDifferentOrder.");
+        DataType dataType1 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        DataType dataType2 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.PERSONALIZATION, DataType.Purpose.ADVERTISING),
+                        true,
+                        false,
+                        null);
+        Set<DataType> set = new HashSet<>();
+        set.add(dataType1);
+        assertTrue(set.contains(dataType2));
+    }
+
+    /** Test for inequality. */
+    @Test
+    public void testInequality() throws Exception {
+        System.out.println("starting testInequality.");
+        DataType dataType1 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        DataType dataType2 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        assertNotEquals(dataType1, dataType2);
+        assertNotEquals(dataType2, dataType1);
+    }
+
+    /** Test for inequality bool. */
+    @Test
+    public void testInequalityBool() throws Exception {
+        System.out.println("starting testInequalityBool.");
+        DataType dataType1 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        DataType dataType2 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        true);
+        assertNotEquals(dataType1, dataType2);
+        assertNotEquals(dataType2, dataType1);
+    }
+
+    /** Test for does not contain. */
+    @Test
+    public void testDoesNotContain() throws Exception {
+        System.out.println("starting testDoesNotContain.");
+        System.out.println("starting testContainsDifferentOrder.");
+        DataType dataType1 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(
+                                DataType.Purpose.ADVERTISING, DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        DataType dataType2 =
+                new DataType(
+                        "datatype1",
+                        Arrays.asList(DataType.Purpose.PERSONALIZATION),
+                        true,
+                        false,
+                        null);
+        Set<DataType> set = new HashSet<>();
+        set.add(dataType1);
+        assertFalse(set.contains(dataType2));
+    }
+}