All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gu.dtalk.OptionBuilder Maven / Gradle / Ivy

The newest version!
package gu.dtalk;

import static com.google.common.base.Preconditions.*;

import java.lang.reflect.Modifier;
import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.base.Throwables;

import gu.dtalk.event.ValueListener;

/**
 * 选项实例构造器
 * @author guyadong
 *
 * @param  选项的数据类型
 * @param 选项的实现类型
 */
public class OptionBuilder> {

	private final O option;
	public OptionBuilder(O item) {
		super();
		this.option = item;
	}
	public OptionBuilder name(String name) {
		option.setName(name);
		return this;
	}

	public OptionBuilder disable(boolean disable) {
		option.setDisable(disable);
		return this;
	}
	public OptionBuilder disable() {
		return disable(true);
	}
	public OptionBuilder hide(boolean hide) {
		option.setHide(hide);
		return this;
	}
	public OptionBuilder hide() {
		return hide(true);
	}
	public OptionBuilder readonly(boolean readOnly) {
		option.setReadOnly(readOnly);
		return this;
	}
	public OptionBuilder readonly() {
		return readonly(true);
	}
	public OptionBuilder needReset(boolean needReset) {
		option.setNeedReset(needReset);
		return this;
	}
	public OptionBuilder needReset() {
		return needReset(true);
	}
	public OptionBuilder required(boolean required) {
		option.setRequired(required);
		return this;
	}
	public OptionBuilder regex(String regex) {
		option.setRegex(regex);
		return this;
	}
	public OptionBuilder required() {
		return required(true);
	}
	public OptionBuilder description(String description) {
		option.setDescription(description);
		return this;
	}

	public OptionBuilder uiName(String uiName) {
		option.setUiName(uiName);
		return this;
	}

	public OptionBuilder value(T value) {
		option.setValue(value);
		return this;
	}
	public OptionBuilder defaultValue(T value) {
		option.setDefaultValue(value);
		return this;
	}
	public OptionBuilder asValue(String value) {
		option.asValue(value);
		return this;
	}
	public OptionBuilder asDefaultValue(String value) {
		option.asDefaultValue(value);
		return this;
	}
	public OptionBuilder validator(Predicate validator) {
		option.setValidator(validator);
		return this;
	}
	public final OptionBuilder available(List available) {
		option.setAvailable(available);
		return this;
	}
	@SuppressWarnings("unchecked")
	public final OptionBuilder available(T... values) {
		option.clearAvailable();
		option.addAvailable(values);
		return this;
	}
	/**
	 * 添加事件侦听器
	 * @param listeners  侦听器列表
	 * @return 当前对象
	 */
	@SafeVarargs
	public final OptionBuilder addListener(ValueListener ...listeners) {
		option.addListener(listeners);
		return this;
	}
	/**
	 * @return 返回option对象
	 * @throws IllegalArgumentException 返回前检查value,defaultValue的有效性,无效则抛出异常
	 */
	public O instance(){
		option.compile();
		return option;
	}
	public static >OptionBuilder builder(O instance) {
		return new OptionBuilder(checkNotNull(instance,"instance is null"));
	}
	public static >OptionBuilder builder(Class type) {
		// 不允许为抽象类
		checkArgument(!Modifier.isAbstract(type.getModifiers()),"%s is a abstract class",type.getName());
		try {
			return new OptionBuilder(type.newInstance());
		} catch (Throwable e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
	@SuppressWarnings("unchecked")
	public static >OptionBuilder builder(OptionType optionType) {
		Class type = (Class) checkNotNull(optionType,"optionType is null").optClass;
		try {
			return new OptionBuilder((O)((O) type.newInstance()).setType(optionType));
		} catch (Throwable e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy