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 
21 import java.util.Arrays;
22 
23 /**
24  * Static utilities for features.
25  */
26 public class FeatureUtils {
27 
28     /**
29      * Returns a feature that is enabled if any of {@code features} is enabled.
30      *
31      * @param features the features to or
32      */
OR(final Feature... features)33     public static Feature OR(final Feature... features) {
34         return new Feature() {
35             @Override
36             public boolean isEnabled(Context context) {
37                 for (Feature f : features) {
38                     if (f.isEnabled(context)) {
39                         return true;
40                     }
41                 }
42                 return false;
43             }
44 
45             @Override
46             public String toString() {
47                 return "or(" + Arrays.asList(features) + ")";
48             }
49         };
50 
51     }
52 
53     /**
54      * Returns a feature that is enabled if all of {@code features} is enabled.
55      *
56      * @param features the features to and
57      */
58     public static Feature AND(final Feature... features) {
59         return new Feature() {
60             @Override
61             public boolean isEnabled(Context context) {
62                 for (Feature f : features) {
63                     if (!f.isEnabled(context)) {
64                         return false;
65                     }
66                 }
67                 return true;
68             }
69 
70             @Override
71             public String toString() {
72                 return "and(" + Arrays.asList(features) + ")";
73             }
74         };
75     }
76 
77     /**
78      * A feature that is always enabled.
79      */
80     public static final Feature ON = new Feature() {
81         @Override
82         public boolean isEnabled(Context context) {
83             return true;
84         }
85 
86         @Override
87         public String toString() {
88             return "on";
89         }
90     };
91 
92     /**
93      * A feature that is always disabled.
94      */
95     public static final Feature OFF = new Feature() {
96         @Override
97         public boolean isEnabled(Context context) {
98             return false;
99         }
100 
101         @Override
102         public String toString() {
103             return "off";
104         }
105     };
106 
107     private FeatureUtils() {
108     }
109 }
110