1 /*
2  * Copyright (C) 2023 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.devicelockcontroller.activities;
18 
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
22 
23 /**
24  * A group of {@link DevicePolicy}.
25  */
26 final class DevicePolicyGroup {
27 
28     private final int mGroupTitleTextId;
29 
30     private final List<DevicePolicy> mDevicePolicyList;
31 
DevicePolicyGroup(int groupTitleTextId, List<DevicePolicy> devicePolicyList)32     DevicePolicyGroup(int groupTitleTextId, List<DevicePolicy> devicePolicyList) {
33         mGroupTitleTextId = groupTitleTextId;
34         mDevicePolicyList = devicePolicyList;
35     }
36 
getGroupTitleTextId()37     int getGroupTitleTextId() {
38         return mGroupTitleTextId;
39     }
40 
getDevicePolicyList()41     List<DevicePolicy> getDevicePolicyList() {
42         return mDevicePolicyList;
43     }
44 
45     @Override
equals(Object obj)46     public boolean equals(Object obj) {
47         if (this == obj) {
48             return true;
49         }
50         if (!(obj instanceof DevicePolicyGroup)) {
51             return false;
52         }
53         DevicePolicyGroup that = (DevicePolicyGroup) obj;
54         if (this.mGroupTitleTextId != that.mGroupTitleTextId) {
55             return false;
56         }
57         if (this.mDevicePolicyList == that.mDevicePolicyList) {
58             return true;
59         }
60         if (this.mDevicePolicyList == null || that.mDevicePolicyList == null
61                 || this.mDevicePolicyList.size() != that.mDevicePolicyList.size()) {
62             return false;
63         }
64         for (int i = 0; i < this.mDevicePolicyList.size(); i++) {
65             if (!(this.mDevicePolicyList.get(i).equals(that.mDevicePolicyList.get(i)))) {
66                 return false;
67             }
68         }
69         return true;
70     }
71 
72     @Override
hashCode()73     public int hashCode() {
74         return Objects.hash(mGroupTitleTextId, mDevicePolicyList);
75     }
76 
77     /**
78      * Builder class for the {@link DevicePolicyGroup}.
79      */
80     static class Builder {
81 
82         private int mGroupTitleTextId;
83 
84         private final List<DevicePolicy> mDevicePolicyList = new ArrayList<>();
85 
setTitleTextId(int groupTitleTextId)86         Builder setTitleTextId(int groupTitleTextId) {
87             mGroupTitleTextId = groupTitleTextId;
88             return this;
89         }
90 
addDevicePolicy(DevicePolicy devicePolicy)91         private Builder addDevicePolicy(DevicePolicy devicePolicy) {
92             mDevicePolicyList.add(devicePolicy);
93             return this;
94         }
95 
addDevicePolicy(int drawableId, int textId)96         Builder addDevicePolicy(int drawableId, int textId) {
97             addDevicePolicy(new DevicePolicy(drawableId, textId));
98             return this;
99         }
100 
build()101         DevicePolicyGroup build() {
102             return new DevicePolicyGroup(mGroupTitleTextId, mDevicePolicyList);
103         }
104     }
105 }
106