Feature.java
01 /*
02  * Copyright 2008-2016 the original author or authors.
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *     http://www.apache.org/licenses/LICENSE-2.0
09  *
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 package griffon.core.env;
17 
18 import javax.annotation.Nonnull;
19 
20 import static griffon.util.GriffonNameUtils.isBlank;
21 import static griffon.util.GriffonNameUtils.requireNonBlank;
22 import static java.util.Objects.requireNonNull;
23 
24 /**
25  @author Andres Almiray
26  */
27 public class Feature {
28     private static final String ERROR_FEATURE_NAME_BLANK = "Argument 'featureName' cannot be blank";
29 
30     public static boolean isFeatureEnabled(@Nonnull String featureName) {
31         requireNonBlank(featureName, ERROR_FEATURE_NAME_BLANK);
32         return !isBlank(System.getProperty(featureName)) && Boolean.getBoolean(featureName);
33     }
34 
35     public static void setFeatureEnabled(@Nonnull String featureName, boolean enabled) {
36         requireNonBlank(featureName, ERROR_FEATURE_NAME_BLANK);
37         System.setProperty(featureName, String.valueOf(enabled));
38     }
39 
40     public static void withFeature(@Nonnull String featureName, @Nonnull Runnable runnable) {
41         requireNonNull(runnable, "Argument 'runnable' cannot be null");
42         if (isFeatureEnabled(featureName)) {
43             runnable.run();
44         }
45     }
46 }