-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Requirements.java
47 lines (39 loc) · 1.69 KB
/
Requirements.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
47
package org.graylog.plugins.enterprise;
import com.google.common.collect.ForwardingMap;
import com.google.inject.assistedinject.Assisted;
import org.graylog.plugins.enterprise.search.views.PluginMetadataSummary;
import javax.inject.Inject;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Requirements<O> extends ForwardingMap<String, PluginMetadataSummary> {
private final Map<String, PluginMetadataSummary> requirements;
private final O dto;
@Inject
public Requirements(Set<? extends Requirement<O>> requirements, @Assisted O dto) {
this.dto = dto;
this.requirements = requirements.stream()
.map(requirement -> requirement.test(dto))
.flatMap(s -> s.entrySet().stream())
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (entry1, entry2) -> entry1));
}
@Override
protected Map<String, PluginMetadataSummary> delegate() {
return this.requirements;
}
public O rebuildRequirements(Function<O, Map<String, PluginMetadataSummary>> getter,
BiFunction<O, Map<String, PluginMetadataSummary>, O> setter) {
final Map<String, PluginMetadataSummary> requirements = Stream.concat(
getter.apply(dto).entrySet().stream(),
this.entrySet().stream()
)
.collect(Collectors.toMap(Entry::getKey, Entry::getValue, (entry1, entry2) -> entry1));
return setter.apply(dto, requirements);
}
public interface Factory<O> {
Requirements<O> create(O dto);
}
}