@SuppressWarnings("varargs") @SafeVarargs public static <T> TreeSet<T> of(Comparator<? super T> comparator, T... values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); return new TreeSet<>(RedBlackTree.of(comparator, values)); }
public static <T> TreeSet<T> empty(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return new TreeSet<>(RedBlackTree.empty(comparator)); }
public static <T> TreeSet<T> of(Comparator<? super T> comparator, T value) { Objects.requireNonNull(comparator, "comparator is null"); return new TreeSet<>(RedBlackTree.of(comparator, value)); }
@Override public TreeSet<T> remove(T element) { return new TreeSet<>(tree.delete(element)); }
@SuppressWarnings("unchecked") public static <T> TreeSet<T> ofAll(Comparator<? super T> comparator, Iterable<? extends T> values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); if (values instanceof TreeSet && ((TreeSet<?>) values).comparator() == comparator) { return (TreeSet<T>) values; } else { return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : empty(comparator); } }
@Override public TreeSet<T> addAll(Iterable<? extends T> elements) { Objects.requireNonNull(elements, "elements is null"); RedBlackTree<T> that = tree; for (T element : elements) { if (!that.contains(element)) { that = that.insert(element); } } if (tree == that) { return this; } else { return new TreeSet<>(that); } }
@Override public TreeSet<T> add(T element) { return contains(element) ? this : new TreeSet<>(tree.insert(element)); }
@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); } }
@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); } }
@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 TreeSet<T> replace(T currentElement, T newElement) { if (tree.contains(currentElement)) { return new TreeSet<>(tree.delete(currentElement).insert(newElement)); } else { 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); } }
@SuppressWarnings("varargs") @SafeVarargs public static <T> TreeSet<T> of(Comparator<? super T> comparator, T... values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); return new TreeSet<>(RedBlackTree.of(comparator, values)); }
public static <T> TreeSet<T> empty(Comparator<? super T> comparator) { Objects.requireNonNull(comparator, "comparator is null"); return new TreeSet<>(RedBlackTree.empty(comparator)); }
@SuppressWarnings("unchecked") public static <T> TreeSet<T> ofAll(Comparator<? super T> comparator, Iterable<? extends T> values) { Objects.requireNonNull(comparator, "comparator is null"); Objects.requireNonNull(values, "values is null"); if (values instanceof TreeSet && ((TreeSet<?>) values).comparator() == comparator) { return (TreeSet<T>) values; } else { return values.iterator().hasNext() ? new TreeSet<>(RedBlackTree.ofAll(comparator, values)) : empty(comparator); } }
@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); } }
@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 TreeSet<T> replace(T currentElement, T newElement) { if (tree.contains(currentElement)) { return new TreeSet<>(tree.delete(currentElement).insert(newElement)); } else { return this; } }