001package gu.dtalk;
002
003import static com.google.common.base.Preconditions.*;
004
005import java.lang.reflect.Modifier;
006
007import com.google.common.base.Predicate;
008import com.google.common.base.Throwables;
009
010import gu.dtalk.event.ValueListener;
011
012/**
013 * 选项实例构造器
014 * @author guyadong
015 *
016 * @param <T> 选项的数据类型
017 * @param <O>选项的实现类型
018 */
019public class OptionBuilder<T,O extends BaseOption<T>> {
020
021        private final O option;
022        public OptionBuilder(O item) {
023                super();
024                this.option = item;
025        }
026        public OptionBuilder<T,O> name(String name) {
027                option.setName(name);
028                return this;
029        }
030
031        public OptionBuilder<T,O> disable(boolean disable) {
032                option.setDisable(disable);
033                return this;
034        }
035        public OptionBuilder<T,O> readonly(boolean readOnly) {
036                option.setReadOnly(readOnly);
037                return this;
038        }
039        public OptionBuilder<T,O> required(boolean required) {
040                option.setRequired(required);
041                return this;
042        }
043        public OptionBuilder<T,O> description(String description) {
044                option.setDescription(description);
045                return this;
046        }
047
048        public OptionBuilder<T,O> uiName(String uiName) {
049                option.setUiName(uiName);
050                return this;
051        }
052
053        public OptionBuilder<T,O> value(T value) {
054                option.setValue(value);
055                return this;
056        }
057        public OptionBuilder<T,O> defaultValue(T value) {
058                option.setDefaultValue(value);
059                return this;
060        }
061        public OptionBuilder<T,O> asValue(String value) {
062                option.asValue(value);
063                return this;
064        }
065        public OptionBuilder<T,O> asDefaultValue(String value) {
066                option.asDefaultValue(value);
067                return this;
068        }
069        public OptionBuilder<T,O> validator(Predicate<T> validator) {
070                option.setValidator(validator);
071                return this;
072        }
073        /**
074         * 添加事件侦听器
075         * @param listeners
076         * @return
077         */
078        @SafeVarargs
079        public final OptionBuilder<T, O> addListener(ValueListener<T> ...listeners) {
080                option.addListener(listeners);
081                return this;
082        }
083        /**
084         * 返回<T>对象
085         * @return
086         * @throws IllegalArgumentException 返回前检查value,defaultValue的有效性,无效则抛出异常
087         */
088        public O instance(){
089                option.compile();
090                return option;
091        }
092        public static <T,O extends BaseOption<T>>OptionBuilder<T,O> builder(O instance) {
093                return new OptionBuilder<T,O>(checkNotNull(instance,"instance is null"));
094        }
095        public static <T,O extends BaseOption<T>>OptionBuilder<T,O> builder(Class<O> type) {
096                // 不允许为抽象类
097                checkArgument(!Modifier.isAbstract(type.getModifiers()),"%s is a abstract class",type.getName());
098                try {
099                        return new OptionBuilder<T,O>(type.newInstance());
100                } catch (Exception e) {
101                        Throwables.throwIfUnchecked(e);
102                        throw new RuntimeException(e);
103                }
104        }
105        @SuppressWarnings("unchecked")
106        public static <T, O extends BaseOption<T>>OptionBuilder<T,O> builder(OptionType optionType) {
107                Class<T> type = (Class<T>) checkNotNull(optionType,"optionType is null").optClass;
108                try {
109                        return new OptionBuilder<T,O>((O)((O) type.newInstance()).setType(optionType));
110                } catch (Exception e) {
111                        Throwables.throwIfUnchecked(e);
112                        throw new RuntimeException(e);
113                }
114        }
115}