package datastructures.set;

import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class CustomStrictSet<T> implements CustomSet<T> {

	private final Set<T> set;

	public CustomStrictSet(Set<T> set) {
		this.set = set;
	}

	@Override
	public Stream<T> stream() {
		return this.set.stream();
	}

	@Override
	public Optional<T> find(Predicate<T> predicate) {
		return set.stream().filter(predicate).findFirst();
	}

	@Override
	public Set<T> getSet() {
		return this.set;
	}

	@Override
	public boolean add(T value) {

		if (this.set.contains(value)) {
			throw new ElementAlreadyInSet("Tried adding " + value + " of class " + value
					.getClass()
					.getName() + " but the set already contains it.");
		}
		return this.set.add(value);
	}

	@Override
	public boolean remove(T value) {
		if (!this.set.contains(value)) {
			throw new ElementNotInSet("Tried removing " + value + " of class " + value
					.getClass()
					.getName() + " but the set not contains it.");
		}
		return this.set.remove(value);
	}
}
