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

gu.dtalk.SwitchOption Maven / Gradle / Ivy

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

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.base.MoreObjects;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import static com.google.common.base.Preconditions.*;

/**
 * 单选列表选项
 * @author guyadong
 *
 * @param  OPTION数据类型
 */
public class SwitchOption extends CheckOption {
	@JSONField(serialize = false,deserialize = false)
	private final Predicate> switchValidator = new Predicate>() {

		@Override
		public boolean apply(Set input) {			
			return input.size() == 1;
		}
	};
	public SwitchOption() {
		super();
		super.setValidator(switchValidator);
	}

	@Override
	public final OptionType getType() {
		return OptionType.SWITCH;
	}

	@Override
	public BaseOption> setValidator(Predicate> validator) {
		return super.setValidator(Predicates.and(switchValidator, validator));
	}

	@JSONField(serialize = false,deserialize = false)
	public E getSwitch(){
		List list = getSelected();
		return list.size()>0 ? list.get(0) : null;
	}
	@JSONField(serialize = false,deserialize = false)
	public CheckOption setSwitch(E value){
		return setSelected(Arrays.asList(value));
	}

	@JSONField(serialize = false,deserialize = false)
	@Override
	public CheckOption setSelected(List sel) {
		sel = MoreObjects.firstNonNull(sel, Collections.emptyList());
		if(!sel.isEmpty()){
			checkArgument(sel.size()==1,"TOO MANY SELECTED OPTIONS");
		}
		return super.setSelected(sel);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy