This project has retired. For details please refer to its Attic page.
EnumConfOption 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  import com.google.common.base.Objects;
23  
24  /**
25   * Enum Configuration option
26   *
27   * @param <T> Enum class
28   */
29  public class EnumConfOption<T extends Enum<T>> extends AbstractConfOption {
30    /** Enum class */
31    private final Class<T> klass;
32    /** Default value */
33    private final T defaultValue;
34  
35    /**
36     * Constructor
37     *
38     * @param key Configuration key
39     * @param klass Enum class
40     * @param defaultValue default value
41     * @param description description of the option
42     */
43    public EnumConfOption(String key, Class<T> klass, T defaultValue,
44        String description) {
45      super(key, description);
46      this.klass = klass;
47      this.defaultValue = defaultValue;
48    }
49  
50    /**
51     * Create new EnumConfOption
52     *
53     * @param key String configuration key
54     * @param klass enum class
55     * @param defaultValue default enum value
56     * @param description description of the option
57     * @param <X> enum type
58     * @return EnumConfOption
59     */
60    public static <X extends Enum<X>> EnumConfOption<X>
61    create(String key, Class<X> klass, X defaultValue, String description) {
62      return new EnumConfOption<X>(key, klass, defaultValue, description);
63    }
64  
65    @Override public boolean isDefaultValue(Configuration conf) {
66      return Objects.equal(get(conf), defaultValue);
67    }
68  
69    @Override public String getDefaultValueStr() {
70      return defaultValue.name();
71    }
72  
73    @Override public ConfOptionType getType() {
74      return ConfOptionType.ENUM;
75    }
76  
77    /**
78     * Lookup value
79     *
80     * @param conf Configuration
81     * @return enum value
82     */
83    public T get(Configuration conf) {
84      String valueStr = conf.get(getKey(), getDefaultValueStr());
85      return T.valueOf(klass, valueStr);
86    }
87  
88    /**
89     * Set value
90     *
91     * @param conf Configuration
92     * @param value to set
93     */
94    public void set(Configuration conf, Enum<T> value) {
95      conf.set(getKey(), value.name());
96    }
97  
98    /**
99     * Set value if it's not already present
100    *
101    * @param conf Configuration
102    * @param value to set
103    */
104   public void setIfUnset(Configuration conf, Enum<T> value) {
105     if (!contains(conf)) {
106       set(conf, value);
107     }
108   }
109 }