package common.properties;

import java.util.Collection;
import java.util.Set;

import common.collect.ImmutableSet;
import common.collect.Sets;
import common.util.Identifyable;

public class PropertyEnum<T extends Enum<T> & Identifyable> extends Property<T> {
	private final ImmutableSet<T> states;

	public static <T extends Enum<T> & Identifyable> PropertyEnum<T> create(String name, Class<T> clazz) {
		return new PropertyEnum(name, clazz, clazz.getEnumConstants());
	}

	public static <T extends Enum<T> & Identifyable> PropertyEnum<T> create(String name, Class<T> clazz, T... values) {
		return new PropertyEnum(name, clazz, values);
	}

	private PropertyEnum(String name, Class<T> type, T[] states) {
		super(name, type);
		Set<String> check = Sets.newHashSet();
		for(T state : states) {
			String id = state.getName();
			if(check.contains(id))
				throw new IllegalArgumentException("Mehrere Werte haben den Namen '" + id + "'");
			check.add(id);
		}
		check.clear();
		this.states = ImmutableSet.copyOf(states);
	}

	public Collection<T> getStates() {
		return this.states;
	}

	public String getName(T value) {
		return value.getName();
	}

	public boolean equals(Object obj) {
		if(this == obj)
			return true;
		if(obj != null && this.getClass() == obj.getClass()) {
			if(!super.equals(obj))
				return false;
			PropertyEnum other = (PropertyEnum)obj;
			return this.states.equals(other.states);
		}
		return false;
	}

	public int hashCode() {
		int i = super.hashCode();
		i = 31 * i + this.states.hashCode();
		return i;
	}
}
