Package at.jku.ssw.fp.coll
Class FSet<E>
- java.lang.Object
-
- at.jku.ssw.fp.coll.FSet<E>
-
- Type Parameters:
E
- The type of elements
- All Implemented Interfaces:
Serializable
,Iterable<E>
- Direct Known Subclasses:
FSet.Cons
,FSet.Empty
public abstract class FSet<E> extends Object implements Iterable<E>, Serializable
Generic functional set implementation. Functional sets are immutable. This implementation largely is a copy of the function listFList
class with the difference that equal elements are stored only once. Moreover, only a subset of methods are supported.- Author:
- Herbert Praehofer
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FSet.Builder<E>
Mutable builder for building functional sets.static class
FSet.Cons<E>
Class representing a functional set.static class
FSet.Empty<E>
Class for the empty set.
-
Constructor Summary
Constructors Constructor Description FSet()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description FSet<E>
add(E elem)
Creates a new set with the elements of this set and the new element.boolean
contains(Object o)
Tests if the provided object is contained in this set.static <E> FSet<E>
empty()
Returns the empty set.FSet<E>
filter(Predicate<? super E> predicate)
Returns a functional set consisting of the elements of this set that match the given predicate.<R> FSet<R>
flatMap(Function<? super E,? extends FSet<R>> mapper)
Returns a functional set consisting of the results of replacing each element of this set with the contents of a mapped set produced by applying the provided mapping function to each element.void
forEach(Consumer<? super E> action)
Performs an action for each element of this set.abstract E
head()
Returns the first element of this set.abstract boolean
isEmpty()
Tests if this list is the empty set.Iterator<E>
iterator()
Returns an iterator for this set.<R> FSet<R>
map(Function<? super E,? extends R> mapper)
Returns a functional set consisting of the results of applying the given function to the elements of this set.static <E> FSet<E>
of(E... elems)
Creates a functional set with the given elements.static <E> FSet<E>
of(List<E> elems)
Creates a functional set from a list of elements.static <E> FSet<E>
of(Set<E> elems)
Creates a functional set from a set of elements.abstract int
size()
Returns the size of this set.abstract FSet<E>
tail()
Returns the tail set for this set.Object[]
toArray()
Creates an object array with the elements of this set.E[]
toArray(E[] a)
Creates an array with the elements of this set.String
toString()
Returns a string representation of this set.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.lang.Iterable
spliterator
-
-
-
-
Field Detail
-
EMPTY
private static final FSet EMPTY
The single object representing the empty set
-
-
Method Detail
-
empty
public static <E> FSet<E> empty()
Returns the empty set.- Type Parameters:
E
- the element type- Returns:
- the empty list
-
of
public static <E> FSet<E> of(E... elems)
Creates a functional set with the given elements.- Type Parameters:
E
- the element type- Parameters:
elems
- the array of elements for this set- Returns:
- the functional set with the given elements
-
of
public static <E> FSet<E> of(List<E> elems)
Creates a functional set from a list of elements.- Type Parameters:
E
- the element type- Parameters:
elems
- the list of elements- Returns:
- the functional set with the given elements
-
of
public static <E> FSet<E> of(Set<E> elems)
Creates a functional set from a set of elements.- Type Parameters:
E
- the element type- Parameters:
elems
- the set of elements- Returns:
- the functional set with the given elements
-
add
public FSet<E> add(E elem)
Creates a new set with the elements of this set and the new element.- Parameters:
elem
- the new element- Returns:
- the functional set with the elements of this set and the new element
-
isEmpty
public abstract boolean isEmpty()
Tests if this list is the empty set.- Returns:
true
, if this set is the empty set;false
otherwise
-
size
public abstract int size()
Returns the size of this set.- Returns:
- the size of this set
-
head
public abstract E head()
Returns the first element of this set.- Returns:
- the first element
-
contains
public boolean contains(Object o)
Tests if the provided object is contained in this set.- Parameters:
o
- the object to look for- Returns:
true
if object found,false
otherwise
-
toString
public String toString()
Returns a string representation of this set.
-
map
public <R> FSet<R> map(Function<? super E,? extends R> mapper)
Returns a functional set consisting of the results of applying the given function to the elements of this set.- Type Parameters:
R
- The element type of the new set- Parameters:
mapper
- a function to apply to each element- Returns:
- the new set with the mapped elements
-
filter
public FSet<E> filter(Predicate<? super E> predicate)
Returns a functional set consisting of the elements of this set that match the given predicate.- Parameters:
predicate
- a predicate to apply to each element to determine if it should be included- Returns:
- the new set with the elements filtered
-
flatMap
public <R> FSet<R> flatMap(Function<? super E,? extends FSet<R>> mapper)
Returns a functional set consisting of the results of replacing each element of this set with the contents of a mapped set produced by applying the provided mapping function to each element.- Type Parameters:
R
- The element type of the new set- Parameters:
mapper
- a function to apply to each element which produces a set of new values- Returns:
- the new set with the mapped elements
-
forEach
public void forEach(Consumer<? super E> action)
Performs an action for each element of this set.
-
toArray
public Object[] toArray()
Creates an object array with the elements of this set.- Returns:
- an object array with the elements of this set
-
-