1 /* 2 * Copyright (C) 2016 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.tv.experiments; 18 19 import android.support.annotation.VisibleForTesting; 20 21 /** 22 * Experiments return values based on user, device and other criteria. 23 */ 24 public final class ExperimentFlag<T> { 25 26 private static boolean sAllowOverrides = false; 27 28 @VisibleForTesting initForTest()29 public static void initForTest() { 30 sAllowOverrides = true; 31 } 32 33 /** Returns a boolean experiment */ createFlag( boolean defaultValue)34 public static ExperimentFlag<Boolean> createFlag( 35 boolean defaultValue) { 36 return new ExperimentFlag<>( 37 defaultValue); 38 } 39 40 private final T mDefaultValue; 41 42 private T mOverrideValue = null; 43 private boolean mOverridden = false; 44 ExperimentFlag( T defaultValue)45 private ExperimentFlag( 46 T defaultValue) { 47 mDefaultValue = defaultValue; 48 } 49 50 /** Returns value for this experiment */ get()51 public T get() { 52 return sAllowOverrides && mOverridden ? mOverrideValue : mDefaultValue; 53 } 54 55 @VisibleForTesting override(T t)56 public void override(T t) { 57 if (sAllowOverrides) { 58 mOverridden = true; 59 mOverrideValue = t; 60 } 61 } 62 63 @VisibleForTesting resetOverride()64 public void resetOverride() { 65 mOverridden = false; 66 } 67 68 69 70 } 71