1 package de.fhdw.wtf.context.model.collections;
2
3 import de.fhdw.wtf.context.model.Anything;
4 import de.fhdw.wtf.context.model.collections.functors.EquivalenceRelation;
5 import de.fhdw.wtf.context.model.collections.functors.Function;
6 import de.fhdw.wtf.context.model.collections.functors.Operator;
7 import de.fhdw.wtf.context.model.collections.functors.Predicate;
8
9 /**
10 * An interface to represent an abstraction over ImmutableCollections. Every operation provides a new
11 * ImmutableCollection. This empowers a functional programming style.
12 *
13 * @param <T>
14 * All elements of this immutable collection have to by of type T which is a subtype to Anything.
15 */
16 public interface ImmutableCollection<T extends Anything> extends Collection<T> {
17
18 /**
19 * Provides a new Collection with the given elment added.
20 *
21 * @param element
22 * The newly added Element.
23 * @return Provides a new Collection with a new element added.
24 */
25 ImmutableCollection<T> add(T element);
26
27 /**
28 * Provides the first element of this collection.
29 *
30 * @return The element of the front of this collection.
31 */
32 T front();
33
34 /**
35 * Provides a new collection without the first element.
36 *
37 * @return Another collection without the first element of this collection.
38 */
39 ImmutableCollection<T> tail();
40
41 /**
42 * Provides anew collection with the elements of this collection in a reverse order.
43 *
44 * @return Another reversed collection.
45 */
46 ImmutableCollection<T> reverse();
47
48 /**
49 * Applies a given function to any element of this collection and provides a collection of the results of the
50 * function.
51 *
52 * @param <U>
53 * The result type of the function.
54 * @param function
55 * A unary function, which can be applied to any element of the collection.
56 * @return Provides a collection of the single results of the function.
57 */
58 <U extends Anything> ImmutableCollection<U> map(Function<T, U> function);
59
60 /**
61 * This method aggregates the elements of this collection to a single result by a given Aggregation Function.
62 *
63 * @param <U>
64 * The result type of the aggregation.
65 * @param aggregation
66 * A binary operation +: U x T -> U.
67 * @return Provides the aggregation result of the operation.
68 */
69 <U extends Anything> U reduce(Operator<T, U> aggregation);
70
71 /**
72 * This method performs an aggregation of different equivalence classes of this collection. First the collection is
73 * divided into multiple sub-collections by an equivalence relation. Then these sub-collection are aggregated by the
74 * given aggregation function. The result is a collection of these results.
75 *
76 * @param <U>
77 * The result type of the aggregation.
78 * @param equi
79 * An equivalence relation which divides this collection into smaller parts.
80 * @param aggregation
81 * A binary operation +: U x T -> U.
82 * @return A Collection of the results of the different aggregations.
83 */
84 <U extends Anything> ImmutableCollection<U> reduceEquivalent(EquivalenceRelation<T> equi, Operator<T, U> aggregation);
85
86 /**
87 * Provides a new Collection, where each element of the new collection suffices the predicate.
88 *
89 * @param predicate
90 * A predicate for an element T.
91 * @return Produces a new collection with elements of this collection, which suffice the predicate.
92 */
93 ImmutableCollection<T> filter(Predicate<T> predicate);
94
95 /**
96 * This operation searches for the first element of this collection, which suffices the Predicate. If no element is
97 * found, a NoSuchElementException is thrown.
98 *
99 * @param predicate
100 * A predicate for an element of T.
101 * @return Provides the first found element which suffices the predicate.
102 */
103 T find(Predicate<T> predicate);
104
105 }