-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
ContentCollection.java
46 lines (35 loc) · 1.51 KB
/
ContentCollection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package net.fortuna.ical4j.model;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Implementors of this interface support the immutable collection contract specified by this interface.
*
* The contract states that any mutation function will not modify the underlying collection, but rather
* will return a copy of the collection with the applied mutation.
*
* @param <T>
*/
public interface ContentCollection<T extends Content> extends Serializable {
ContentCollection<T> add(T content);
ContentCollection<T> addAll(Collection<T> content);
ContentCollection<T> remove(T content);
ContentCollection<T> removeAll(String... name);
ContentCollection<T> replace(T content);
List<T> getAll();
@SuppressWarnings("unchecked")
default <R extends T> List<R> get(String name) {
return getAll().stream().filter(c -> c.getName().equalsIgnoreCase(name)).map(c -> (R) c).collect(Collectors.toList());
}
@SuppressWarnings("unchecked")
default <R extends T> Optional<R> getFirst(String name) {
return (Optional<R>) getAll().stream().filter(c -> c.getName().equalsIgnoreCase(name)).findFirst();
}
@SuppressWarnings("unchecked")
default <R extends T> R getRequired(String name) throws ConstraintViolationException {
return (R) getFirst(name).orElseThrow(() -> new ConstraintViolationException(
String.format("Missing required %s", name)));
}
}