package common.properties;

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

import common.collect.ImmutableSet;
import common.collect.Sets;

public class PropertyInteger extends Property<Integer> {
	private final ImmutableSet<Integer> values;

	public static PropertyInteger create(String name, int min, int max) {
		return new PropertyInteger(name, min, max);
	}

	private PropertyInteger(String name, int min, int max) {
		super(name, Integer.class);
		if(min < 0)
			throw new IllegalArgumentException("Mindestwert von " + name + " muss 0 oder höher sein");
		else if(max <= min)
			throw new IllegalArgumentException("Maximalwert von " + name + " muss größer als der Mindestwert sein (" + min + ")");
		Set<Integer> set = Sets.<Integer>newHashSetWithExpectedSize(1 + max - min);
		for(int z = min; z <= max; z++) {
			set.add(z);
		}
		this.values = ImmutableSet.copyOf(set);
	}

	public Collection<Integer> getStates() {
		return this.values;
	}

	public String getName(Integer value) {
		return value.toString();
	}

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

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