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

gu.dtalk.ItemBuilder Maven / Gradle / Ivy

There is a newer version: 0.8.2
Show newest version
package gu.dtalk;

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

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

import com.google.common.base.Throwables;

/**
 * 菜单条目构造器
 * @author guyadong
 *
 * @param  菜单条目子类类型
 */
public class ItemBuilder {

	protected final T item;
	public ItemBuilder(T item) {
		super();
		this.item = item;
	}
	public ItemBuilder name(String name) {
		item.setName(name);
		return this;
	}

	public ItemBuilder disable(boolean disable) {
		item.setDisable(disable);
		return this;
	}
	public ItemBuilder disable() {
		return disable(true);
	}
	public ItemBuilder hide(boolean hide) {
		item.setHide(hide);
		return this;
	}
	public ItemBuilder hide(){
		return hide(true);
	}
	public ItemBuilder needReset(boolean needReset) {
		if(item instanceof CmdItem){
			((CmdItem)item).setNeedReset(needReset);
		}
		return this;
	}
	public ItemBuilder needReset() {
		return needReset(true);
	}
	public ItemBuilder description(String description) {
		item.setDescription(description);
		return this;
	}

	public ItemBuilder uiName(String uiName) {
		item.setUiName(uiName);
		return this;
	}

	public ItemBuilder childs(List childs) {
		checkState(item.isContainer(),"%s isn't container",item.getClass().getSimpleName());
		item.setChilds(childs);
		return this;
	}

	public ItemBuilder addChilds(BaseItem... childs) {
		checkState(item.isContainer(),"%s isn't container",item.getClass().getSimpleName());
		item.addChilds(childs);
		return this;
	}

	public ItemBuilder addChilds(Collection childs) {
		checkState(item.isContainer(),"%s isn't container",item.getClass().getSimpleName());
		item.addChilds(childs);
		return this;
	}

	public T instance(){
		return item;
	}
	public static ItemBuilder builder(T instance) {
		return new ItemBuilder(checkNotNull(instance,"instance is null"));
	}
	public static ItemBuilder builder(Class type) {
		// 不允许为抽象类
		checkArgument(!Modifier.isAbstract(type.getModifiers()),"%s is a abstract class",type.getName());
		try {
			return new ItemBuilder(type.newInstance());
		} catch (Throwable e) {
			Throwables.throwIfUnchecked(e);
			throw new RuntimeException(e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy