-
Notifications
You must be signed in to change notification settings - Fork 14
/
PendingAssertionRegistry.java
101 lines (83 loc) · 3.22 KB
/
PendingAssertionRegistry.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
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
package cz.cvut.kbss.jopa.oom;
import cz.cvut.kbss.ontodriver.model.Assertion;
import cz.cvut.kbss.ontodriver.model.NamedResource;
import java.net.URI;
import java.util.*;
/**
* Used to track references to unpersisted instances during transaction.
* <p>
* The general rule is that on commit, this registry should be empty.
*/
class PendingAssertionRegistry {
private final Map<Object, Set<PendingAssertion>> pendingAssertions = new HashMap<>();
/**
* Registers a new pending assertion.
*
* @param owner Subject of the assertion
* @param assertion The assertion representation
* @param object The value of the assertion. Always an individual identifier
* @param context Context into which the assertion will be added
*/
void addPendingAssertion(NamedResource owner, Assertion assertion, Object object, URI context) {
assert owner != null;
assert assertion != null;
assert object != null;
final PendingAssertion pa = new PendingAssertion(owner, assertion, context);
if (!pendingAssertions.containsKey(object)) {
pendingAssertions.put(object, new HashSet<>());
}
pendingAssertions.get(object).add(pa);
}
/**
* Removes any pending persists with the specified object (value of the assertion).
*
* @param object Object which is no longer considered pending, so all assertions referencing it should be removed
* from this registry
*/
Set<PendingAssertion> removeAndGetPendingAssertionsWith(Object object) {
assert object != null;
final Set<PendingAssertion> pending = pendingAssertions.remove(object);
return pending != null ? pending : Collections.emptySet();
}
public static class PendingAssertion {
private final NamedResource owner;
private final Assertion assertion;
private final URI context;
private PendingAssertion(NamedResource owner, Assertion assertion, URI context) {
this.owner = owner;
this.assertion = assertion;
this.context = context;
}
public NamedResource getOwner() {
return owner;
}
public Assertion getAssertion() {
return assertion;
}
public URI getContext() {
return context;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PendingAssertion that = (PendingAssertion) o;
return owner.equals(that.owner) && assertion.equals(that.assertion) && (context != null ?
context.equals(that.context) : that.context == null);
}
@Override
public int hashCode() {
int result = owner.hashCode();
result = 31 * result + assertion.hashCode();
result = 31 * result + (context != null ? context.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "PendingAssertion{" +
owner + " -> " + assertion +
", context=" + context +
'}';
}
}
}