1 /*
2  * Copyright (C) 2024 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 android.platform.test.flag.junit;
18 
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.function.BiPredicate;
24 import java.util.function.Predicate;
25 
26 /** @hide */
27 public class CustomFeatureFlags implements FeatureFlags {
28 
29     private BiPredicate<String, Predicate<FeatureFlags>> mGetValueImpl;
30 
CustomFeatureFlags(BiPredicate<String, Predicate<FeatureFlags>> getValueImpl)31     public CustomFeatureFlags(BiPredicate<String, Predicate<FeatureFlags>> getValueImpl) {
32         mGetValueImpl = getValueImpl;
33     }
34 
35     @Override
flagName3()36     public boolean flagName3() {
37         return getValue(Flags.FLAG_FLAG_NAME3, FeatureFlags::flagName3);
38     }
39 
40     @Override
flagName4()41     public boolean flagName4() {
42         return getValue(Flags.FLAG_FLAG_NAME4, FeatureFlags::flagName4);
43     }
44 
45     @Override
roEnabled()46     public boolean roEnabled() {
47         return getValue(Flags.FLAG_RO_ENABLED, FeatureFlags::roEnabled);
48     }
49 
50     @Override
roDisabled()51     public boolean roDisabled() {
52         return getValue(Flags.FLAG_RO_DISABLED, FeatureFlags::roDisabled);
53     }
54 
getValue(String flagName, Predicate<FeatureFlags> getter)55     protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) {
56         return mGetValueImpl.test(flagName, getter);
57     }
58 
getFlagNames()59     public List<String> getFlagNames() {
60         return Arrays.asList(
61             Flags.FLAG_FLAG_NAME3,
62             Flags.FLAG_FLAG_NAME4,
63             Flags.FLAG_RO_ENABLED,
64             Flags.FLAG_RO_DISABLED
65         );
66     }
67 
isFlagReadOnlyOptimized(String flagName)68     public boolean isFlagReadOnlyOptimized(String flagName) {
69         return mReadOnlyFlagSet.contains(flagName);
70     }
71 
72     private Set<String> mReadOnlyFlagSet = new HashSet<>(
73         Arrays.asList(
74             Flags.FLAG_RO_ENABLED,
75             Flags.FLAG_RO_DISABLED,
76             "")
77         );
78 }
79