Feature.java
01 /*
02  * Copyright 2008-2014 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 griffon.util.TypeUtils.castToBoolean;
23 import static java.util.Objects.requireNonNull;
24 
25 /**
26  @author Andres Almiray
27  */
28 public class Feature {
29     private static final String ERROR_FEATURE_NAME_BLANK = "Argument 'featureName' cannot be blank";
30 
31     public static boolean isFeatureEnabled(@Nonnull String featureName) {
32         requireNonBlank(featureName, ERROR_FEATURE_NAME_BLANK);
33         if (!isBlank(System.getProperty(featureName))) {
34             return Boolean.getBoolean(featureName);
35         }
36         return !isBlank(Metadata.getCurrent().getProperty(featureName)) &&
37             castToBoolean(Metadata.getCurrent().getProperty(featureName));
38     }
39 
40     public static void setFeatureEnabled(@Nonnull String featureName, boolean enabled) {
41         requireNonBlank(featureName, ERROR_FEATURE_NAME_BLANK);
42         System.setProperty(featureName, String.valueOf(enabled));
43     }
44 
45     public static void withFeature(@Nonnull String featureName, @Nonnull Runnable runnable) {
46         requireNonNull(runnable, "Argument 'runnable' cannot be null");
47         if (isFeatureEnabled(featureName)) {
48             runnable.run();
49         }
50     }
51 }