-
Notifications
You must be signed in to change notification settings - Fork 5
/
MetaBinding.java
99 lines (79 loc) · 4 KB
/
MetaBinding.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
package org.genericsystem.reactor;
import java.util.Arrays;
import java.util.function.BiFunction;
import java.util.function.Function;
import org.genericsystem.api.core.Snapshot;
import org.genericsystem.common.Generic;
import org.genericsystem.defaults.tools.RxJavaHelpers;
import io.reactivex.Observable;
public class MetaBinding<BETWEEN> {
private Function<Context, Observable<Snapshot<BETWEEN>>> betweenChildren;
private final BiFunction<Context, BETWEEN, Context> modelBuilder;
private static BiFunction<Context, Generic, Context> MODEL_BUILDER = (model, generic) -> new Context(model, Context.addToGenerics(generic, model.getGenerics()));
private static BiFunction<Context, Context, Context> MODEL_CLONER = (model, subModel) -> new Context(model, subModel.getGenerics());
public MetaBinding(Function<Context, Observable<Snapshot<BETWEEN>>> betweenChildren, BiFunction<Context, BETWEEN, Context> modelBuilder) {
this.betweenChildren = betweenChildren;
this.modelBuilder = modelBuilder;
}
private Observable<Snapshot<Context>> buildChildren(Context context, Tag childTag) {
return betweenChildren.apply(context).map(snapshot -> snapshot.map(g -> modelBuilder.apply(context, g)));
}
public Observable<IndexedSubContext> buildFilteredChildren(Context context, Tag childTag) {
return buildChildren(context, childTag).doOnNext(unused -> {
// Delete existing subContexts when a new Snapshot is received.
context.getSubContexts(childTag).forEach(subContext -> subContext.destroy());
context.getSubContexts(childTag).clear();
}).switchMap(snapshot -> snapshot.getIndexedElements().filter(indexedContext -> indexedContext.getIndex() >= 0)
.flatMap(indexedSubContext -> buildObservableBySubContext(snapshot, indexedSubContext.getElement(), childTag, indexedSubContext.getIndex())));
}
@SuppressWarnings("unchecked")
private Observable<IndexedSubContext> buildObservableBySubContext(Snapshot<Context> snapshot, Context subContext, Tag childTag, int index) {
return RxJavaHelpers.changesOf(childTag.getObservableSwitchers()).switchMap(list -> list.isEmpty() ? Observable.just(new IndexedSubContext(subContext, true, index)) :
Observable.combineLatest(list.stream().map(switcher -> RxJavaHelpers.valuesOf(switcher.apply(subContext, childTag))).toArray(Observable[]::new),
args -> new IndexedSubContext(subContext, Arrays.stream(args).allMatch(v -> Boolean.TRUE.equals(v)), index)).distinctUntilChanged())
.mergeWith(snapshot.getRemovals().filter(context -> context.equals(subContext)).map(context -> new IndexedSubContext(context, false, -1)))
.takeUntil(indexedContext -> ((IndexedSubContext) indexedContext).getIndex() < 0);
}
public Function<Context, Observable<Snapshot<BETWEEN>>> getBetweenChildren() {
return betweenChildren;
}
public static MetaBinding<Context> selectMetaBinding(Function<Context, Observable<Snapshot<Context>>> betweenChildren) {
return new MetaBinding<Context>(betweenChildren, MODEL_CLONER);
}
public static MetaBinding<Generic> forEachMetaBinding(Function<Context, Observable<Snapshot<Generic>>> betweenChildren) {
return new MetaBinding<Generic>(betweenChildren, MODEL_BUILDER);
}
public void setBetweenChildren(Function<Context, Observable<Snapshot<BETWEEN>>> betweenChildren) {
this.betweenChildren = betweenChildren;
}
public static class IndexedSubContext {
private Context context;
private boolean create;
private int index;
public IndexedSubContext(Context context, boolean create, int index) {
this.context = context;
this.create = create;
this.index = index;
}
public Context getContext() {
return context;
}
public boolean getCreate() {
return create;
}
public int getIndex() {
return index;
}
@Override
public int hashCode() {
return context.hashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IndexedSubContext))
return false;
IndexedSubContext other = (IndexedSubContext) obj;
return create == other.create && context.equals(other.context) && index == other.index;
}
}
}