This project has retired. For details please refer to its Attic page.
BooleanConfOption xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.giraph.conf;
19  
20  import org.apache.hadoop.conf.Configuration;
21  
22  /**
23   * Boolean configuration option
24   */
25  public class BooleanConfOption extends AbstractConfOption {
26    /** Default value */
27    private final boolean defaultValue;
28  
29    /**
30     * Constructor
31     *
32     * @param key configuration key
33     * @param defaultValue default value
34     * @param description configuration description
35     */
36    public BooleanConfOption(String key, boolean defaultValue,
37        String description) {
38      super(key, description);
39      this.defaultValue = defaultValue;
40    }
41  
42    /**
43     * Get the default value of this option
44     *
45     * @return default value
46     */
47    public boolean getDefaultValue() {
48      return defaultValue;
49    }
50  
51    @Override public boolean isDefaultValue(Configuration conf) {
52      return get(conf) == defaultValue;
53    }
54  
55    @Override public String getDefaultValueStr() {
56      return Boolean.toString(defaultValue);
57    }
58  
59    @Override public ConfOptionType getType() {
60      return ConfOptionType.BOOLEAN;
61    }
62  
63    /**
64     * Lookup value in Configuration
65     *
66     * @param conf Configuration
67     * @return value for key in conf, or defaultValue if not present
68     */
69    public boolean get(Configuration conf) {
70      return conf.getBoolean(getKey(), defaultValue);
71    }
72  
73    /**
74     * Check if value is true
75     *
76     * @param conf Configuration
77     * @return true if value is set and true, false otherwise
78     */
79    public boolean isFalse(Configuration conf) {
80      return !get(conf);
81    }
82  
83    /**
84     * Check if value is false
85     *
86     * @param conf Configuration
87     * @return true if value is set and true, false otherwise
88     */
89    public boolean isTrue(Configuration conf) {
90      return get(conf);
91    }
92  
93    /**
94     * Set value in configuration for this key
95     *
96     * @param conf Configuration
97     * @param value to set
98     */
99    public void set(Configuration conf, boolean value) {
100     conf.setBoolean(getKey(), value);
101   }
102 
103   /**
104    * Set value in configuration if it hasn't been set already
105    *
106    * @param conf Configuration
107    * @param value to set
108    */
109   public void setIfUnset(Configuration conf, boolean value) {
110     conf.setBooleanIfUnset(getKey(), value);
111   }
112 }