javaslang.collection.Set Maven / Gradle / Ivy
/* / \____ _ _ ____ ______ / \ ____ __ _______
* / / \/ \ / \/ \ / /\__\/ // \/ \ // /\__\ JΛVΛSLΛNG
* _/ / /\ \ \/ / /\ \\__\\ \ // /\ \ /\\/ \ /__\ \ Copyright 2014-2016 Javaslang, http://javaslang.io
* /___/\_/ \_/\____/\_/ \_/\__\/__/\__\_/ \_// \__/\_____/ Licensed under the Apache License, Version 2.0
*/
package javaslang.collection;
import javaslang.Function1;
import javaslang.Tuple2;
import javaslang.Tuple3;
import javaslang.control.Option;
import java.util.Comparator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
/**
* An immutable {@code Set} interface.
*
* @param Component type
* @author Daniel Dietrich, Ruslan Sennov
* @since 2.0.0
*/
public interface Set extends Traversable, Function1 {
long serialVersionUID = 1L;
/**
* Narrows a widened {@code Set extends T>} to {@code Set}
* by performing a type safe-cast. This is eligible because immutable/read-only
* collections are covariant.
*
* @param set A {@code Set}.
* @param Component type of the {@code Set}.
* @return the given {@code set} instance as narrowed type {@code Set}.
*/
@SuppressWarnings("unchecked")
static Set narrow(Set extends T> set) {
return (Set) set;
}
/**
* Add the given element to this set, if it is not already contained.
*
* @param element The element to be added.
* @return A new set containing all elements of this set and also {@code element}.
*/
Set add(T element);
/**
* Adds all of the given elements to this set, if not already contained.
*
* @param elements The elements to be added.
* @return A new set containing all elements of this set and the given {@code elements}, if not already contained.
*/
Set addAll(Iterable extends T> elements);
/**
* Tests if a given {@code element} is contained in this {@code Set}.
*
* This method is equivalent to {@link #contains(Object)}.
*
* @param element the element to test for membership.
* @return {@code true} if the given {@code element} is contained, {@code false} otherwise.
*/
@Override
default Boolean apply(T element) {
return contains(element);
}
/**
* Calculates the difference between this set and another set.
*
* See also {@link #removeAll(Iterable)}.
*
* @param that Elements to be removed from this set.
* @return A new Set containing all elements of this set which are not located in {@code that} set.
*/
Set diff(Set extends T> that);
/**
* Computes the intersection between this set and another set.
*
* See also {@link #retainAll(Iterable)}.
*
* @param that the set to intersect with.
* @return A new Set consisting of all elements that are both in this set and in the given set {@code that}.
*/
Set intersect(Set extends T> that);
/**
* Removes a specific element from this set, if present.
*
* @param element The element to be removed from this set.
* @return A new set consisting of the elements of this set, without the given {@code element}.
*/
Set remove(T element);
/**
* Removes all of the given elements from this set, if present.
*
* @param elements The elements to be removed from this set.
* @return A new set consisting of the elements of this set, without the given {@code elements}.
*/
Set removeAll(Iterable extends T> elements);
/**
* Converts this Javaslang {@code Set} to a {@code java.util.Set} while preserving characteristics
* like insertion order ({@code LinkedHashSet}) and sort order ({@code SortedSet}).
*
* @return a new {@code java.util.Set} instance
*/
@Override
java.util.Set toJavaSet();
/**
* Adds all of the elements of {@code that} set to this set, if not already present.
*
* See also {@link #addAll(Iterable)}.
*
* @param that The set to form the union with.
* @return A new set that contains all distinct elements of this and {@code that} set.
*/
Set union(Set extends T> that);
// -- Adjusted return types of Traversable methods
@Override
boolean contains(T element);
@Override
Set distinct();
@Override
Set distinctBy(Comparator super T> comparator);
@Override
Set distinctBy(Function super T, ? extends U> keyExtractor);
@Override
Set drop(long n);
@Override
Set dropRight(long n);
@Override
Set dropUntil(Predicate super T> predicate);
@Override
Set dropWhile(Predicate super T> predicate);
@Override
Set filter(Predicate super T> predicate);
@Override
Set flatMap(Function super T, ? extends Iterable extends U>> mapper);
@Override
Map> groupBy(Function super T, ? extends C> classifier);
@Override
Iterator extends Set> grouped(long size);
@Override
Set init();
@Override
Option extends Set> initOption();
@Override
Iterator iterator();
@Override
int length();
@Override
Set map(Function super T, ? extends U> mapper);
@Override
Tuple2 extends Set, ? extends Set> partition(Predicate super T> predicate);
@Override
Set peek(Consumer super T> action);
@Override
Set replace(T currentElement, T newElement);
@Override
Set replaceAll(T currentElement, T newElement);
@Override
Set retainAll(Iterable extends T> elements);
@Override
Set scan(T zero, BiFunction super T, ? super T, ? extends T> operation);
@Override
Set scanLeft(U zero, BiFunction super U, ? super T, ? extends U> operation);
@Override
Set scanRight(U zero, BiFunction super T, ? super U, ? extends U> operation);
@Override
Iterator extends Set> sliding(long size);
@Override
Iterator extends Set> sliding(long size, long step);
@Override
Tuple2 extends Set, ? extends Set> span(Predicate super T> predicate);
@Override
default Spliterator spliterator() {
return Spliterators.spliterator(iterator(), length(), Spliterator.ORDERED | Spliterator.IMMUTABLE);
}
@Override
Set tail();
@Override
Option extends Set> tailOption();
@Override
Set take(long n);
@Override
Set takeRight(long n);
@Override
Set takeUntil(Predicate super T> predicate);
@Override
Set takeWhile(Predicate super T> predicate);
@Override
Tuple2 extends Set, ? extends Set> unzip(Function super T, Tuple2 extends T1, ? extends T2>> unzipper);
@Override
Tuple3 extends Set, ? extends Set, ? extends Set> unzip3(Function super T, Tuple3 extends T1, ? extends T2, ? extends T3>> unzipper);
@Override
Set> zip(Iterable extends U> that);
@Override
Set> zipAll(Iterable extends U> that, T thisElem, U thatElem);
@Override
Set> zipWithIndex();
}