dev.marksman.collectionviews.WrappedSet Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of collection-views Show documentation
Show all versions of collection-views Show documentation
Low overhead, protected views over Java collections
package dev.marksman.collectionviews;
import dev.marksman.enhancediterables.FiniteIterable;
import java.util.Iterator;
import java.util.Set;
import static dev.marksman.collectionviews.ProtectedIterator.protectedIterator;
final class WrappedSet extends ConcreteSet
implements NonEmptySet, Primitive {
/**
* underlying must contain at least one element
*/
private final java.util.Set underlying;
WrappedSet(Set underlying) {
this.underlying = underlying;
}
@Override
public A head() {
return underlying.iterator().next();
}
@Override
public FiniteIterable tail() {
Iterator iterator = iterator();
iterator.next();
return () -> iterator;
}
@Override
public int size() {
return underlying.size();
}
@Override
public boolean contains(A element) {
return underlying.contains(element);
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public Iterator iterator() {
return protectedIterator(underlying.iterator());
}
}