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 import com.google.common.collect.Lists;
24
25 import java.util.List;
26
27 /**
28 * String Configuration option
29 */
30 public class StrConfOption extends AbstractConfOption {
31 /** Default value */
32 private final String defaultValue;
33
34 /**
35 * Constructor
36 *
37 * @param key key
38 * @param defaultValue default value
39 * @param description configuration description
40 */
41 public StrConfOption(String key, String defaultValue, String description) {
42 super(key, description);
43 this.defaultValue = defaultValue;
44 }
45
46 public String getDefaultValue() {
47 return defaultValue;
48 }
49
50 @Override public boolean isDefaultValue(Configuration conf) {
51 return Objects.equal(get(conf), defaultValue);
52 }
53
54 @Override public String getDefaultValueStr() {
55 return defaultValue;
56 }
57
58 @Override public ConfOptionType getType() {
59 return ConfOptionType.STRING;
60 }
61
62 /**
63 * Lookup value
64 *
65 * @param conf Configuration
66 * @return value for key, or defaultValue
67 */
68 public String get(Configuration conf) {
69 return conf.get(getKey(), defaultValue);
70 }
71
72 /**
73 * Lookup value with user defined defaultValue
74 *
75 * @param conf Configuration
76 * @param defaultVal default value to use
77 * @return value for key, or defaultVal passed in
78 */
79 public String getWithDefault(Configuration conf, String defaultVal) {
80 return conf.get(getKey(), defaultVal);
81 }
82
83 /**
84 * Get array of values for key
85 *
86 * @param conf Configuration
87 * @return array of values for key
88 */
89 public String[] getArray(Configuration conf) {
90 return conf.getStrings(getKey(), defaultValue);
91 }
92
93 /**
94 * Get list of values for key
95 *
96 * @param conf Configuration
97 * @return list of values for key
98 */
99 public List<String> getList(Configuration conf) {
100 return Lists.newArrayList(getArray(conf));
101 }
102
103 /**
104 * Set value for key
105 *
106 * @param conf Configuration
107 * @param value to set
108 */
109 public void set(Configuration conf, String value) {
110 conf.set(getKey(), value);
111 }
112
113 /**
114 * Set value if not already present
115 *
116 * @param conf Configuration
117 * @param value to set
118 */
119 public void setIfUnset(Configuration conf, String value) {
120 conf.setIfUnset(getKey(), value);
121 }
122 }