1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.mycore.access.facts;
19
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.Optional;
23 import java.util.Set;
24 import java.util.StringJoiner;
25
26 import org.mycore.access.facts.model.MCRFact;
27 import org.mycore.access.facts.model.MCRFactComputable;
28
29
30
31
32
33
34
35
36
37
38
39
40 public class MCRFactsHolder {
41
42 private Collection<MCRFactComputable<MCRFact<?>>> computers;
43
44 private final Set<MCRFact<?>> facts = new HashSet<MCRFact<?>>();
45
46 public MCRFactsHolder(Collection<MCRFactComputable<MCRFact<?>>> computers) {
47 this.computers = computers;
48 }
49
50 public void add(MCRFact<?> fact) {
51 facts.add(fact);
52 }
53
54 public boolean isFact(String factName, String term) {
55 Optional<MCRFact<?>> osc = facts.stream()
56 .filter(f -> factName.equals(f.getName()))
57 .filter(f -> term.equals(f.getTerm()))
58 .findFirst();
59 return osc.isPresent();
60 }
61
62 public Optional<MCRFact<?>> require(String factName, MCRFactComputable<MCRFact<?>> factComputer) {
63 Optional<MCRFact<?>> osc = facts.stream().filter(f -> factName.equals(f.getName())).findFirst();
64 if (osc.isPresent()) {
65 return osc;
66 } else {
67 Optional<MCRFact<?>> fact = factComputer.computeFact(this);
68 if (fact.isPresent()) {
69 facts.add(fact.get());
70 return fact;
71 }
72 }
73 return Optional.empty();
74 }
75
76 @SuppressWarnings("unchecked")
77 public <F extends MCRFact<?>> Optional<F> require(String factName) {
78 Optional<F> osc = (Optional<F>) facts.stream().filter(f -> factName.equals(f.getName())).findFirst();
79 if (osc.isPresent()) {
80 return osc;
81 } else {
82 Optional<MCRFactComputable<MCRFact<?>>> theComputer = computers.stream()
83 .filter(c -> factName.equals(c.getFactName())).findFirst();
84 if (theComputer.isPresent()) {
85 Optional<MCRFact<?>> fact = theComputer.get().computeFact(this);
86 if (fact.isPresent()) {
87 facts.add(fact.get());
88 return (Optional<F>) fact;
89 }
90 }
91 }
92 return Optional.empty();
93 }
94
95 @Override
96 public String toString() {
97 StringJoiner sj = new StringJoiner(" & ");
98 facts.stream().forEach(f -> sj.add(f.toString()));
99 return sj.toString();
100 }
101 }