2017-06-27 10:22:10 +08:00
|
|
|
package com.baidu.hugegraph.config;
|
|
|
|
|
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
2017-06-28 11:49:28 +08:00
|
|
|
import com.baidu.hugegraph.util.E;
|
2017-06-27 10:22:10 +08:00
|
|
|
import com.google.common.base.Joiner;
|
|
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
|
import com.google.common.base.Predicate;
|
|
|
|
|
import com.google.common.collect.ImmutableSet;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Created by liningrui on 2017/3/23.
|
|
|
|
|
*/
|
|
|
|
|
public class ConfigOption<T> {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ConfigOption.class);
|
|
|
|
|
|
|
|
|
|
private static final Set<Class<?>> ACCEPTED_DATATYPES;
|
|
|
|
|
private static final String ACCEPTED_DATATYPES_STRING;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
ACCEPTED_DATATYPES = ImmutableSet.of(
|
|
|
|
|
Boolean.class,
|
|
|
|
|
Short.class,
|
|
|
|
|
Integer.class,
|
|
|
|
|
Byte.class,
|
|
|
|
|
Long.class,
|
|
|
|
|
Float.class,
|
|
|
|
|
Double.class,
|
|
|
|
|
String.class,
|
|
|
|
|
String[].class
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
ACCEPTED_DATATYPES_STRING = Joiner.on(", ").join(ACCEPTED_DATATYPES);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final String name;
|
|
|
|
|
private final String desc;
|
|
|
|
|
private final Boolean rewritable;
|
|
|
|
|
private final Class<T> dataType;
|
|
|
|
|
private T value;
|
2017-06-28 11:49:28 +08:00
|
|
|
private final Predicate<T> checkFunc;
|
2017-06-27 10:22:10 +08:00
|
|
|
|
2017-06-28 12:21:21 +08:00
|
|
|
public ConfigOption(String name, T value, Boolean rewritable, String desc,
|
|
|
|
|
Predicate<T> verifyFunc) {
|
|
|
|
|
this(name, (Class<T>) value.getClass(), value, rewritable, desc, verifyFunc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConfigOption(String name, Class<T> dataType, T value, Boolean
|
2017-06-28 11:49:28 +08:00
|
|
|
rewritable, String desc, Predicate<T> checkFunc) {
|
2017-06-27 10:22:10 +08:00
|
|
|
Preconditions.checkNotNull(name);
|
|
|
|
|
Preconditions.checkNotNull(dataType);
|
|
|
|
|
Preconditions.checkNotNull(rewritable);
|
|
|
|
|
|
|
|
|
|
if (!ACCEPTED_DATATYPES.contains(dataType)) {
|
2017-06-28 11:49:28 +08:00
|
|
|
String msg = String.format("Input datatype: '%s' doesn't belong "
|
|
|
|
|
+ "to acceptable type set: [%s]",
|
|
|
|
|
dataType, ACCEPTED_DATATYPES_STRING);
|
2017-06-27 10:22:10 +08:00
|
|
|
logger.error(msg);
|
|
|
|
|
throw new IllegalArgumentException(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.dataType = dataType;
|
|
|
|
|
this.value = value;
|
|
|
|
|
this.rewritable = rewritable;
|
|
|
|
|
this.desc = desc;
|
2017-06-28 11:49:28 +08:00
|
|
|
this.checkFunc = checkFunc;
|
2017-06-27 10:22:10 +08:00
|
|
|
|
2017-06-28 11:49:28 +08:00
|
|
|
if (this.checkFunc != null) {
|
|
|
|
|
check(this.value);
|
2017-06-27 10:22:10 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String name() {
|
2017-06-28 11:49:28 +08:00
|
|
|
return this.name;
|
2017-06-27 10:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Class<T> dataType() {
|
2017-06-28 11:49:28 +08:00
|
|
|
return this.dataType;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String desc() {
|
|
|
|
|
return this.desc;
|
2017-06-27 10:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T value() {
|
2017-06-28 11:49:28 +08:00
|
|
|
return this.value;
|
2017-06-27 10:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void value(T value) {
|
2017-06-28 11:49:28 +08:00
|
|
|
check(value);
|
|
|
|
|
E.checkArgument(this.rewritable, "Not allowed to modify option: '%s' "
|
|
|
|
|
+ "which is unrewritable", this.name);
|
2017-06-27 10:22:10 +08:00
|
|
|
this.value = value;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 12:21:21 +08:00
|
|
|
public void check(Object value) {
|
2017-06-28 11:49:28 +08:00
|
|
|
E.checkNotNull(value, "value", this.name);
|
|
|
|
|
E.checkArgument(this.dataType.isInstance(value),
|
|
|
|
|
"Invalid class for option '%s'. Expected '%s' but given '%s'",
|
|
|
|
|
this.name, this.dataType, value.getClass());
|
|
|
|
|
T result = (T) value;
|
|
|
|
|
E.checkArgument(this.checkFunc.apply(result),
|
|
|
|
|
"Invalid option value for [%s]: %s", this.name, value);
|
2017-06-27 10:22:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|