-
Notifications
You must be signed in to change notification settings - Fork 5
/
Snapshot.java
204 lines (175 loc) · 5.26 KB
/
Snapshot.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
193
194
195
196
197
198
199
200
201
202
203
204
package org.genericsystem.api.core;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Represents a <code>Set</code> of results <em>aware</em> of its context.
* <p>
* This is a functional interface whose functional method is {@link #get}.
* </p>
*
* @author Nicolas Feybesse
*
* @param <T>
* the type of element contained by the <code>Snapshot</code>.
*/
@FunctionalInterface
public interface Snapshot<T> extends Iterable<T> {
default Snapshot<T> getParent() {
return null;
}
default IndexFilter getFilter() {
return null;
}
default Snapshot<T> createChild(Snapshot<T> parent, IndexFilter filter) {
return new Snapshot<T>() {
@Override
public Snapshot<T> getParent() {
return parent;
}
@Override
public IndexFilter getFilter() {
return filter;
}
@Override
public Stream<T> unfilteredStream() {
throw new UnsupportedOperationException("unfilteredStream() should be called only on unfiltered snapshots.");
}
};
}
@Override
default Iterator<T> iterator() {
return stream().iterator();
}
/**
* Returns a <code>Stream</code> of this <code>Snapshot</code>.
*
* @return a <code>Stream</code> of this <code>Snapshot</code>.
*/
default Stream<T> stream() {
List<IndexFilter> filters = new LinkedList<>();
Snapshot<T> current = this;
while (current.getParent() != null) {
filters.add(0, current.getFilter());
current = current.getParent();
}
if (!filters.isEmpty())
return current.filter(filters).unfilteredStream();
return unfilteredStream();
}
/**
* Returns a <code>Stream</code> of this <code>Snapshot</code>. Used only if this <code>Snapshot</code> is not filtered.
*
* @return a <code>Stream</code> of this <code>Snapshot</code>.
*/
public Stream<T> unfilteredStream();
/**
* Returns the number of elements in this snapshot.
*
* @return the number of elements in this snapshot.
*/
default int size() {
return (int) stream().count();
}
/**
* Returns <code>true</code> if this snapshot contains no elements.
*
* @return <code>true</code> if this snapshot contains no elements.
*/
default boolean isEmpty() {
return stream().count() == 0;
}
/**
* Returns <code>true</code> if this snapshot contains the specified element.
*
* @param o
* element whose presence in this snapshot is to be tested.
* @return <code>true</code> if this snapshot contains the specified element.
*/
default boolean contains(Object o) {
return o.equals(get(o));
}
/**
* Returns <code>true</code> if this snapshot contains all of the elements in the specified snapshot.
*
* @param c
* collection to be checked for containment in this snapshot.
* @return <code>true</code> if this snapshot contains all of the elements in the specified snapshot.
*/
default boolean containsAll(Collection<?> c) {
return c.stream().allMatch(this::contains);
}
/**
* Returns the first element in this snapshot equals to the specified object or <code>null</code> if no element in this snapshot is equal to the specified object.
*
* @param o
* object to be tested for equality.
* @return the first element in this snapshot equals to the specified object or <code>null</code> if no element in this snapshot is equal to the specified object.
*/
default T get(Object o) {
return stream().filter(o::equals).findFirst().orElse(null);
}
/**
* Returns a <code>String</code> representation of all vertices contained in this snapshot.
*
* @return a <code>String</code> representation of all vertices contained in this snapshot.
*/
default String info() {
return stream().collect(Collectors.toList()).toString();
}
/**
* Returns the first element of this snapshot or <code>null</code> if this snapshot is empty.
*
* @return the first element of this snapshot or <code>null</code> if this snapshot is empty.
*/
default T first() {
return (iterator().hasNext() ? iterator().next() : null);
}
default T getByIndex(int index) {
Iterator<T> iterator = iterator();
int i = 0;
while (iterator.hasNext()) {
if (index == i)
return iterator.next();
iterator.next();
i++;
}
return null;
}
default Snapshot<T> filter(Predicate<T> predicate) {
return new Snapshot<T>() {
@Override
public Stream<T> unfilteredStream() {
return Snapshot.this.stream().filter(predicate);
}
@Override
public T get(Object o) {
T result = Snapshot.this.get(o);
return result != null && predicate.test(result) ? result : null;
}
};
}
default Snapshot<T> filter(IndexFilter filter) {
return createChild(this, filter);
}
default Snapshot<T> filter(List<IndexFilter> filters) {
return new Snapshot<T>() {
@Override
public Stream<T> unfilteredStream() {
return Snapshot.this.stream().filter(g -> filters.stream().allMatch(filter -> filter.test((IGeneric<?>) g)));
}
@Override
public T get(Object o) {
T result = Snapshot.this.get(o);
return result != null && filters.stream().allMatch(filter -> filter.test((IGeneric<?>) result)) ? result : null;
}
};
}
default List<T> toList() {
return stream().collect(Collectors.toList());
}
}