@Override public TreeSet<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@SuppressWarnings("unchecked") @Override public TreeSet<T> diff(Set<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty()) { return this; } else if (elements instanceof TreeSet) { final TreeSet<T> that = (TreeSet<T>) elements; return that.isEmpty() ? this : new TreeSet<>(tree.difference(that.tree)); } else { return removeAll(elements); } }
/** * Returns this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from result of evaluating supplier, using existing comparator. * * @param supplier An alternative {@code Traversable} * @return this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from result of evaluating supplier, using existing comparator. */ @Override public TreeSet<T> orElse(Supplier<? extends Iterable<? extends T>> supplier) { return isEmpty() ? ofAll(tree.comparator(), supplier.get()) : this; }
@Override public T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty TreeSet"); } else { return tree.min().get(); } }
@Override public T last() { if (isEmpty()) { throw new NoSuchElementException("last of empty TreeSet"); } else { return tree.max().get(); } }
/** * Returns this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from iterable, using existing comparator. * * @param other An alternative {@code Traversable} * @return this {@code TreeSet} if it is nonempty, * otherwise {@code TreeSet} created from iterable, using existing comparator. */ @Override public TreeSet<T> orElse(Iterable<? extends T> other) { return isEmpty() ? ofAll(tree.comparator(), other) : this; }
@SuppressWarnings("unchecked") @Override public TreeSet<T> union(Set<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (elements instanceof TreeSet) { final TreeSet<T> that = (TreeSet<T>) elements; return that.isEmpty() ? this : new TreeSet<>(tree.union(that.tree)); } else { return addAll(elements); } }
@SuppressWarnings("unchecked") @Override public TreeSet<T> intersect(Set<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); if (isEmpty()) { return this; } else if (elements instanceof TreeSet) { final TreeSet<T> that = (TreeSet<T>) elements; return new TreeSet<>(tree.intersection(that.tree)); } else { return retainAll(elements); } }
@Override public TreeSet<T> init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty TreeSet"); } else { return new TreeSet<>(tree.delete(tree.max().get())); } }
@Override public TreeSet<T> tail() { if (isEmpty()) { throw new UnsupportedOperationException("tail of empty TreeSet"); } else { return new TreeSet<>(tree.delete(tree.min().get())); } }
@Override public <U> TreeSet<T> distinctBy(Function<? super T, ? extends U> keyExtractor) { Objects.requireNonNull(keyExtractor, "keyExtractor is null"); return isEmpty() ? this : TreeSet.ofAll(tree.comparator(), iterator().distinctBy(keyExtractor)); }
@Override public TreeSet<T> distinctBy(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return isEmpty() ? this : TreeSet.ofAll(tree.comparator(), iterator().distinctBy(comparator)); }
@Override public Option<TreeSet<T>> tailOption() { return isEmpty() ? Option.none() : Option.some(tail()); }
@Override public Option<TreeSet<T>> initOption() { return isEmpty() ? Option.none() : Option.some(init()); }
@Override public TreeSet<T> drop(int n) { if (n <= 0 || isEmpty()) { return this; } else if (n >= length()) { return empty(tree.comparator()); } else { return TreeSet.ofAll(tree.comparator(), iterator().drop(n)); } }
@Override public TreeSet<T> dropRight(int n) { if (n <= 0 || isEmpty()) { return this; } else if (n >= length()) { return empty(tree.comparator()); } else { return TreeSet.ofAll(tree.comparator(), iterator().dropRight(n)); } }
@Override public TreeSet<T> peek(Consumer<? super T> action) { Objects.requireNonNull(action, "action is null"); if (!isEmpty()) { action.accept(head()); } return this; }
@Override public T head() { if (isEmpty()) { throw new NoSuchElementException("head of empty TreeSet"); } else { return tree.min().get(); } }
@Override public TreeSet<T> init() { if (isEmpty()) { throw new UnsupportedOperationException("init of empty TreeSet"); } else { return new TreeSet<>(tree.delete(tree.max().get())); } }
@Override public TreeSet<T> dropRight(int n) { if (n <= 0 || isEmpty()) { return this; } else if (n >= length()) { return empty(tree.comparator()); } else { return TreeSet.ofAll(tree.comparator(), iterator().dropRight(n)); } }