-
Notifications
You must be signed in to change notification settings - Fork 5
/
Differential.java
192 lines (163 loc) · 6.94 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package org.genericsystem.common;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.reactivex.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
/**
* @author Nicolas Feybesse
*
*/
public class Differential implements IDifferential<Generic> {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
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);
}
@Override
public ObservableValue<IDifferential<Generic>> getDifferentialProperty() {
return subDifferential.getDifferentialProperty();
}
@Override
public Observable<Differential> getDifferentialObservable() {
return (Observable<Differential>) subDifferential.getDifferentialObservable();
}
@Override
public Map<Generic, ObservableList<Generic>> getDependenciesAsOservableListCacheMap() {
return subDifferential.getDependenciesAsOservableListCacheMap();
}
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);
return generic;
}
protected void unplug(Generic generic) {
if (!adds.remove(generic))
removes.add(generic);
}
protected void unplugAll(Collection<Generic> generics, Checker checker) {
removes.addAll(generics, adds, checker);
}
@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)));
}
};
}
@Override
public ObservableList<Generic> toObservableList() {
ObservableList<Generic> result = getDependenciesAsOservableListCacheMap().get(generic);
if (result == null) {
final ObservableList<Generic> result_ = FXCollections.observableArrayList(getDifferentialProperty().getValue().getDependencies(generic).toList());
Observable<Differential> differentialObs = getDifferentialObservable();
Observable<Generic> adds = differentialObs.flatMap(diff -> diff.getAddsObservable(generic));
Observable<Generic> removes = differentialObs.flatMap(diff -> diff.getRemovesObservable(generic));
adds.subscribe(g -> {
logger.debug("Generic added, {}", g);
result_.add(g);
}, e -> logger.error("Exception while computing observable list.", e));
removes.subscribe(g -> {
logger.debug("Generic removed, {}", g);
result_.remove(g);
}, e -> logger.error("Exception while computing observable list.", e));
result = result_;
getDependenciesAsOservableListCacheMap().put(generic, result);
}
return result;
}
};
}
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 ObservableValue<?> getObservable(Generic generic) {
return BindingsTools.create(getSubDifferential().getObservable(generic), adds.getFilteredInvalidator(generic::isDirectAncestorOf), removes.getFilteredInvalidator(generic::isDirectAncestorOf));
}
@Override
public Observable<Generic> getAddsObservable(Generic generic) {
return Observable.merge(getSubDifferential().getAddsObservable(generic),
adds.getFilteredAdds(generic::isDirectAncestorOf),
removes.getFilteredRemoves(generic::isDirectAncestorOf));
}
@Override
public Observable<Generic> getRemovesObservable(Generic generic) {
return Observable.merge(getSubDifferential().getRemovesObservable(generic),
removes.getFilteredAdds(generic::isDirectAncestorOf),
adds.getFilteredRemoves(generic::isDirectAncestorOf));
}
}