forked from eclipse-biscuit/biscuit-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactSet.java
More file actions
113 lines (92 loc) · 2.66 KB
/
FactSet.java
File metadata and controls
113 lines (92 loc) · 2.66 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright (c) 2019 Geoffroy Couprie <contact@geoffroycouprie.com> and Contributors to the Eclipse Foundation.
* SPDX-License-Identifier: Apache-2.0
*/
package org.eclipse.biscuit.datalog;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.stream.Stream;
public final class FactSet {
private final HashMap<Origin, HashSet<Fact>> facts;
public FactSet() {
facts = new HashMap<>();
}
public FactSet(Origin o, HashSet<Fact> factSet) {
facts = new HashMap<>();
facts.put(o, factSet);
}
public HashMap<Origin, HashSet<Fact>> facts() {
return this.facts;
}
public void add(Origin origin, Fact fact) {
if (!facts.containsKey(origin)) {
facts.put(origin, new HashSet<>());
}
facts.get(origin).add(fact);
}
public int size() {
int size = 0;
for (HashSet<Fact> h : facts.values()) {
size += h.size();
}
return size;
}
public FactSet clone() {
FactSet newFacts = new FactSet();
for (Map.Entry<Origin, HashSet<Fact>> entry : this.facts.entrySet()) {
HashSet<Fact> h = new HashSet<>(entry.getValue());
newFacts.facts.put(entry.getKey(), h);
}
return newFacts;
}
public void merge(FactSet other) {
for (Map.Entry<Origin, HashSet<Fact>> entry : other.facts.entrySet()) {
if (!facts.containsKey(entry.getKey())) {
facts.put(entry.getKey(), entry.getValue());
} else {
facts.get(entry.getKey()).addAll(entry.getValue());
}
}
}
public Stream stream(TrustedOrigins blockIds) {
return facts.entrySet().stream()
.filter(
entry -> {
Origin o = entry.getKey();
return blockIds.contains(o);
})
.flatMap(entry -> entry.getValue().stream().map(fact -> new Pair<>(entry.getKey(), fact)));
}
public Stream<Fact> stream() {
return facts.entrySet().stream().flatMap(entry -> entry.getValue().stream());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
FactSet factSet = (FactSet) o;
return facts.equals(factSet.facts);
}
@Override
public int hashCode() {
return facts.hashCode();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder("FactSet {");
for (Map.Entry<Origin, HashSet<Fact>> entry : this.facts.entrySet()) {
res.append("\n\t").append(entry.getKey()).append("[");
for (Fact fact : entry.getValue()) {
res.append("\n\t\t").append(fact);
}
res.append("\n]");
}
res.append("\n}");
return res.toString();
}
}