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.HashMap; 21 import java.util.HashSet; 22 import java.util.List; 23 import java.util.Map; 24 import java.util.Set; 25 import java.util.function.BiPredicate; 26 import java.util.function.Predicate; 27 28 /** @hide */ 29 public class CustomFeatureFlags implements FeatureFlags { 30 31 private BiPredicate<String, Predicate<FeatureFlags>> mGetValueImpl; 32 CustomFeatureFlags(BiPredicate<String, Predicate<FeatureFlags>> getValueImpl)33 public CustomFeatureFlags(BiPredicate<String, Predicate<FeatureFlags>> getValueImpl) { 34 mGetValueImpl = getValueImpl; 35 } 36 37 @Override flagName3()38 public boolean flagName3() { 39 return getValue(Flags.FLAG_FLAG_NAME3, FeatureFlags::flagName3); 40 } 41 42 @Override flagName4()43 public boolean flagName4() { 44 return getValue(Flags.FLAG_FLAG_NAME4, FeatureFlags::flagName4); 45 } 46 47 @Override roEnabled()48 public boolean roEnabled() { 49 return getValue(Flags.FLAG_RO_ENABLED, FeatureFlags::roEnabled); 50 } 51 52 @Override roDisabled()53 public boolean roDisabled() { 54 return getValue(Flags.FLAG_RO_DISABLED, FeatureFlags::roDisabled); 55 } 56 getValue(String flagName, Predicate<FeatureFlags> getter)57 protected boolean getValue(String flagName, Predicate<FeatureFlags> getter) { 58 return mGetValueImpl.test(flagName, getter); 59 } 60 getFlagNames()61 public List<String> getFlagNames() { 62 return Arrays.asList( 63 Flags.FLAG_FLAG_NAME3, 64 Flags.FLAG_FLAG_NAME4, 65 Flags.FLAG_RO_ENABLED, 66 Flags.FLAG_RO_DISABLED, 67 Flags.FLAG_FLAG_FINALIZED); 68 } 69 isFlagReadOnlyOptimized(String flagName)70 public boolean isFlagReadOnlyOptimized(String flagName) { 71 return mReadOnlyFlagSet.contains(flagName); 72 } 73 74 private Set<String> mReadOnlyFlagSet = 75 new HashSet<>(Arrays.asList(Flags.FLAG_RO_ENABLED, Flags.FLAG_RO_DISABLED, "")); 76 77 private Map<String, Integer> mFinalizedFlags = 78 new HashMap<>( 79 Map.ofEntries( 80 Map.entry(Flags.FLAG_FLAG_FINALIZED, 36), 81 Map.entry("", Integer.MAX_VALUE))); 82 isFlagFinalized(String flagName)83 public boolean isFlagFinalized(String flagName) { 84 if (!mFinalizedFlags.containsKey(flagName)) { 85 return false; 86 } 87 return 99 >= mFinalizedFlags.get(flagName); 88 } 89 } 90