This project has retired. For details please refer to its
Attic page.
ClassConfOption xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.conf;
19
20 import org.apache.giraph.utils.ReflectionUtils;
21 import org.apache.hadoop.conf.Configuration;
22 import org.apache.log4j.Logger;
23
24 import com.google.common.base.Objects;
25
26
27
28
29
30
31 public class ClassConfOption<C> extends AbstractConfOption {
32
33 private static final Logger LOG = Logger.getLogger(ClassConfOption.class);
34
35
36 private final Class<C> interfaceClass;
37
38 private final Class<? extends C> defaultClass;
39
40
41
42
43
44
45
46
47
48 private ClassConfOption(String key, Class<? extends C> defaultClass,
49 Class<C> interfaceClass, String description) {
50 super(key, description);
51 this.defaultClass = defaultClass;
52 this.interfaceClass = interfaceClass;
53 }
54
55
56
57
58
59
60
61
62
63
64
65 public static <T> ClassConfOption<T> create(String key,
66 Class<? extends T> defaultClass, Class<T> interfaceClass,
67 String description) {
68 return new ClassConfOption<T>(key, defaultClass, interfaceClass,
69 description);
70 }
71
72 public Class<? extends C> getDefaultClass() {
73 return defaultClass;
74 }
75
76 public Class<C> getInterfaceClass() {
77 return interfaceClass;
78 }
79
80 @Override public boolean isDefaultValue(Configuration conf) {
81 return Objects.equal(get(conf), defaultClass);
82 }
83
84 @Override public String getDefaultValueStr() {
85 return defaultClass == null ? "null" : defaultClass.getSimpleName();
86 }
87
88 @Override public ConfOptionType getType() {
89 return ConfOptionType.CLASS;
90 }
91
92 @Override public String toString() {
93 StringBuilder sb = new StringBuilder(30);
94 sb.append(" ");
95 sb.append(getKey()).append(" => ").append(getDefaultValueStr());
96 sb.append(" [").append(interfaceClass.getSimpleName()).append("] ");
97 sb.append(" (").append(getType().toString().toLowerCase()).append(")\n");
98 return sb.toString();
99 }
100
101
102
103
104
105
106
107 public C newInstance(Configuration conf) {
108 Class<? extends C> klass = get(conf);
109 if (klass == null) {
110 return null;
111 }
112 if (conf instanceof ImmutableClassesGiraphConfiguration) {
113 return ReflectionUtils.newInstance(klass,
114 (ImmutableClassesGiraphConfiguration) conf);
115 } else {
116 return org.apache.hadoop.util.ReflectionUtils.newInstance(klass, conf);
117 }
118 }
119
120
121
122
123
124
125
126 public Class<? extends C> get(Configuration conf) {
127 return conf.getClass(getKey(), defaultClass, interfaceClass);
128 }
129
130
131
132
133
134
135
136 public Class<? extends C>[] getArray(Configuration conf) {
137 return getClassesOfType(conf, getKey(), interfaceClass);
138 }
139
140
141
142
143
144
145
146
147
148
149
150 public static <T> Class<? extends T>[] getClassesOfType(Configuration conf,
151 String name, Class<T> xface, Class<? extends T> ... defaultValue) {
152 Class<?>[] klasses = conf.getClasses(name, defaultValue);
153 for (Class<?> klass : klasses) {
154 if (!xface.isAssignableFrom(klass)) {
155 throw new RuntimeException(klass + " is not assignable from " +
156 xface.getName());
157 }
158 }
159 return (Class<? extends T>[]) klasses;
160 }
161
162
163
164
165
166
167
168
169 public Class<? extends C> getWithDefault(Configuration conf,
170 Class<? extends C> defaultValue) {
171 return conf.getClass(getKey(), defaultValue, interfaceClass);
172 }
173
174
175
176
177
178
179
180 public void set(Configuration conf, Class<? extends C> klass) {
181 conf.setClass(getKey(), klass, interfaceClass);
182 }
183
184
185
186
187
188
189
190 public void setIfUnset(Configuration conf, Class<? extends C> klass) {
191 if (!contains(conf)) {
192 set(conf, klass);
193 }
194 }
195
196
197
198
199
200
201
202 public void setMany(Configuration conf, Class<? extends C> ... klasses) {
203 String[] klassNames = new String[klasses.length];
204 for (int i = 0; i < klasses.length; ++i) {
205 Class<?> klass = klasses[i];
206 if (!interfaceClass.isAssignableFrom(klass)) {
207 throw new RuntimeException(klass + " does not implement " +
208 interfaceClass.getName());
209 }
210 klassNames[i] = klasses[i].getName();
211 }
212 conf.setStrings(getKey(), klassNames);
213 }
214
215
216
217
218
219
220
221 public void add(Configuration conf, Class<? extends C> klass) {
222 addToClasses(conf, getKey(), klass, interfaceClass);
223 }
224
225
226
227
228
229
230
231
232
233
234
235 public static <T> void addToClasses(Configuration conf, String name,
236 Class<? extends T> klass, Class<T> xface) {
237 if (!xface.isAssignableFrom(klass)) {
238 throw new RuntimeException(klass + " does not implement " +
239 xface.getName());
240 }
241 String value;
242 String klasses = conf.get(name);
243 if (klasses == null) {
244 value = klass.getName();
245 } else {
246 value = klasses + "," + klass.getName();
247 }
248 conf.set(name, value);
249 }
250 }