• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.common.feature;
18 
19 import android.content.Context;
20 import com.android.tv.common.util.CommonUtils;
21 import java.util.Arrays;
22 
23 /** Static utilities for features. */
24 public class FeatureUtils {
25 
26     /**
27      * Returns a feature that is enabled if any of {@code features} is enabled.
28      *
29      * @param features the features to or
30      */
or(final Feature... features)31     public static Feature or(final Feature... features) {
32         return new Feature() {
33             @Override
34             public boolean isEnabled(Context context) {
35                 for (Feature f : features) {
36                     if (f.isEnabled(context)) {
37                         return true;
38                     }
39                 }
40                 return false;
41             }
42 
43             @Override
44             public String toString() {
45                 return "or(" + Arrays.asList(features) + ")";
46             }
47         };
48     }
49 
50     /**
51      * Returns a feature that is enabled if all of {@code features} is enabled.
52      *
53      * @param features the features to and
54      */
55     public static Feature and(final Feature... features) {
56         return new Feature() {
57             @Override
58             public boolean isEnabled(Context context) {
59                 for (Feature f : features) {
60                     if (!f.isEnabled(context)) {
61                         return false;
62                     }
63                 }
64                 return true;
65             }
66 
67             @Override
68             public String toString() {
69                 return "and(" + Arrays.asList(features) + ")";
70             }
71         };
72     }
73 
74     /**
75      * Returns a feature that is opposite of the given {@code feature}.
76      *
77      * @param feature the feature to invert
78      */
79     public static Feature not(final Feature feature) {
80         return new Feature() {
81             @Override
82             public boolean isEnabled(Context context) {
83                 return !feature.isEnabled(context);
84             }
85 
86             @Override
87             public String toString() {
88                 return "not(" + feature + ")";
89             }
90         };
91     }
92 
93     /** A feature that is always enabled. */
94     public static final Feature ON =
95             new Feature() {
96                 @Override
97                 public boolean isEnabled(Context context) {
98                     return true;
99                 }
100 
101                 @Override
102                 public String toString() {
103                     return "on";
104                 }
105             };
106 
107     /** A feature that is always disabled. */
108     public static final Feature OFF =
109             new Feature() {
110                 @Override
111                 public boolean isEnabled(Context context) {
112                     return false;
113                 }
114 
115                 @Override
116                 public String toString() {
117                     return "off";
118                 }
119             };
120 
121     /** True if running in robolectric. */
122     public static final Feature ROBOLECTRIC =
123             new Feature() {
124                 @Override
125                 public boolean isEnabled(Context context) {
126                     return CommonUtils.isRoboTest();
127                 }
128 
129                 @Override
130                 public String toString() {
131                     return "isRobolecteric";
132                 }
133             };
134 
135     private FeatureUtils() {}
136 }
137