-
Notifications
You must be signed in to change notification settings - Fork 5
/
Differential.java
131 lines (109 loc) · 4.8 KB
/
Differential.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package org.genericsystem.common;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.genericsystem.api.core.FiltersBuilder;
import org.genericsystem.api.core.IndexFilter;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.api.core.exceptions.ConcurrencyControlException;
import org.genericsystem.api.core.exceptions.OptimisticLockConstraintViolationException;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.defaults.tools.BindingsTools;
import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
/**
* @author Nicolas Feybesse
*
*/
public class Differential implements IDifferential<Generic> {
private final IDifferential<Generic> subDifferential;
protected final PseudoConcurrentCollection<Generic> adds = new PseudoConcurrentCollection<>();
protected final PseudoConcurrentCollection<Generic> removes = new PseudoConcurrentCollection<>();
public Differential(IDifferential<Generic> subDifferential) {
this.subDifferential = subDifferential;
}
public Stream<Generic> getLivingToRespawn() {
return adds.stream().filter(g -> {
return g.getTs() == Long.MAX_VALUE && g.isAlive();
});
}
public PseudoConcurrentCollection<Generic> getAdds() {
return adds;
}
public IDifferential<Generic> getSubDifferential() {
return subDifferential;
}
public int getCacheLevel() {
return subDifferential instanceof Differential ? ((Differential) subDifferential).getCacheLevel() + 1 : 0;
}
public IntegerProperty getCacheLevelObservable() {
return subDifferential instanceof Differential ? new SimpleIntegerProperty(((Differential) subDifferential).getCacheLevel() + 1) : new SimpleIntegerProperty(0);
}
void checkConstraints(Checker checker) throws RollbackException {
adds.forEach(x -> checker.checkAfterBuild(true, true, x));
removes.forEach(x -> checker.checkAfterBuild(false, true, x));
}
protected Generic plug(Generic generic) {
// assert generic.getOtherTs()[0] == Long.MAX_VALUE;
adds.add(generic);
// System.out.println("Add : " + generic.info() + System.identityHashCode(generic));
return generic;
}
protected void unplug(Generic generic) {
if (!adds.remove(generic))
removes.add(generic);
// System.out.println("Remove : " + generic.info() + System.identityHashCode(generic));
}
@Override
public Snapshot<Generic> getDependencies(Generic generic) {
return new Snapshot<Generic>() {
@Override
public Generic get(Object o) {
Generic result = adds.get(o);
if (result != null)
return generic.isDirectAncestorOf(result) ? result : null;
return !removes.contains(o) ? subDifferential.getDependencies(generic).get(o) : null;
}
@Override
public Stream<Generic> unfilteredStream() {
return Stream.concat(adds.contains(generic) ? Stream.empty() : subDifferential.getDependencies(generic).filter(new IndexFilter(FiltersBuilder.NOT_CONTAINED_IN_PARAM, new ArrayList<>(removes.toList()))).stream(),
adds.filter(new IndexFilter(FiltersBuilder.IS_DIRECT_DEPENDENCY_OF, generic)).stream());
}
@Override
public Snapshot<Generic> filter(List<IndexFilter> filters) {
return new Snapshot<Generic>() {
@Override
public Stream<Generic> unfilteredStream() {
List<IndexFilter> filters_ = new ArrayList<>(filters);
filters_.add(new IndexFilter(FiltersBuilder.NOT_CONTAINED_IN_PARAM, new ArrayList<>(removes.toList())));
return Stream.concat(adds.contains(generic) ? Stream.empty() : subDifferential.getDependencies(generic).filter(filters_).stream(), adds.filter(filters).stream().filter(x -> generic.isDirectAncestorOf(x)));
}
};
}
};
}
void apply() throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
getSubDifferential().apply(removes, adds);
}
@Override
public void apply(Snapshot<Generic> removes, Snapshot<Generic> adds) throws ConcurrencyControlException, OptimisticLockConstraintViolationException {
this.removes.disableInvalidations();
this.adds.disableInvalidations();
for (Generic generic : removes)
unplug(generic);
for (Generic generic : adds)
plug(generic);
this.removes.enableInvalidations();
this.adds.enableInvalidations();
}
@Override
public long getTs() {
return getSubDifferential().getTs();
}
@Override
public final Observable getObservable(Generic generic) {
return BindingsTools.create(getSubDifferential().getObservable(generic), adds.getFilteredInvalidator(generic, generic::isDirectAncestorOf), removes.getFilteredInvalidator(generic, generic::isDirectAncestorOf));
// return ObservableBase.createObservable(getSubDifferential().getObservable(generic), adds.getFilteredInvalidator(generic, generic::isDirectAncestorOf), removes.getFilteredInvalidator(generic, generic::isDirectAncestorOf));
}
}