1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.conf;
1920import org.apache.hadoop.conf.Configuration;
2122import com.google.common.base.Objects;
2324/**25 * Enum Configuration option26 *27 * @param <T> Enum class28 */29publicclass EnumConfOption<T extends Enum<T>> extendsAbstractConfOption {
30/** Enum class */31privatefinal Class<T> klass;
32/** Default value */33privatefinal T defaultValue;
3435/**36 * Constructor37 *38 * @param key Configuration key39 * @param klass Enum class40 * @param defaultValue default value41 * @param description description of the option42 */43publicEnumConfOption(String key, Class<T> klass, T defaultValue,
44 String description) {
45super(key, description);
46this.klass = klass;
47this.defaultValue = defaultValue;
48 }
4950/**51 * Create new EnumConfOption52 *53 * @param key String configuration key54 * @param klass enum class55 * @param defaultValue default enum value56 * @param description description of the option57 * @param <X> enum type58 * @return EnumConfOption59 */60publicstatic <X extends Enum<X>> EnumConfOption<X>
61 create(String key, Class<X> klass, X defaultValue, String description) {
62returnnew EnumConfOption<X>(key, klass, defaultValue, description);
63 }
6465 @Override publicboolean isDefaultValue(Configuration conf) {
66return Objects.equal(get(conf), defaultValue);
67 }
6869 @Override public String getDefaultValueStr() {
70return defaultValue.name();
71 }
7273 @Override publicConfOptionType getType() {
74return ConfOptionType.ENUM;
75 }
7677/**78 * Lookup value79 *80 * @param conf Configuration81 * @return enum value82 */83public T get(Configuration conf) {
84 String valueStr = conf.get(getKey(), getDefaultValueStr());
85return T.valueOf(klass, valueStr);
86 }
8788/**89 * Set value90 *91 * @param conf Configuration92 * @param value to set93 */94publicvoid set(Configuration conf, Enum<T> value) {
95 conf.set(getKey(), value.name());
96 }
9798/**99 * Set value if it's not already present100 *101 * @param conf Configuration102 * @param value to set103 */104publicvoid setIfUnset(Configuration conf, Enum<T> value) {
105if (!contains(conf)) {
106 set(conf, value);
107 }
108 }
109 }