1 /*
2  * Copyright (C) 2018 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.packageinstaller.role.model;
18 
19 import android.os.Bundle;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 
24 import java.util.Objects;
25 
26 /**
27  * Specifies the value of a meta data for an application to qualify for a {@link Role}.
28  */
29 public class RequiredMetaData {
30 
31     /**
32      * The name of this meta data.
33      */
34     @NonNull
35     private final String mName;
36 
37     /**
38      * The value of this meta data.
39      */
40     @Nullable
41     private final Object mValue;
42 
43     /**
44      * Whether this meta data is optional.
45      *
46      * @see #isQualified(Bundle)
47      */
48     private final boolean mOptional;
49 
RequiredMetaData(@onNull String name, @Nullable Object value, boolean optional)50     public RequiredMetaData(@NonNull String name, @Nullable Object value, boolean optional) {
51         mName = name;
52         mValue = value;
53         mOptional = optional;
54     }
55 
56     @NonNull
getName()57     public String getName() {
58         return mName;
59     }
60 
61     @Nullable
getValue()62     public Object getValue() {
63         return mValue;
64     }
65 
isOptional()66     public boolean isOptional() {
67         return mOptional;
68     }
69 
70     /**
71      * Check whether the meta data of a component is qualified.
72      *
73      * @param metaData the meta data of the component
74      *
75      * @return whether the meta data of the component is qualified
76      */
isQualified(@onNull Bundle metaData)77     public boolean isQualified(@NonNull Bundle metaData) {
78         if (metaData.containsKey(mName)) {
79             Object metaDataValue = metaData.get(mName);
80             return Objects.equals(metaDataValue, mValue);
81         } else {
82             return mOptional;
83         }
84     }
85 
86     @Override
toString()87     public String toString() {
88         return "RequiredMetaData{"
89                 + "mName='" + mName + '\''
90                 + ", mValue=" + mValue
91                 + ", mOptional=" + mOptional
92                 + '}';
93     }
94 
95     @Override
equals(Object object)96     public boolean equals(Object object) {
97         if (this == object) {
98             return true;
99         }
100         if (object == null || getClass() != object.getClass()) {
101             return false;
102         }
103         RequiredMetaData that = (RequiredMetaData) object;
104         return mOptional == that.mOptional
105                 && Objects.equals(mName, that.mName)
106                 && Objects.equals(mValue, that.mValue);
107     }
108 
109     @Override
hashCode()110     public int hashCode() {
111         return Objects.hash(mName, mValue, mOptional);
112     }
113 }
114