-
Notifications
You must be signed in to change notification settings - Fork 5
/
Snapshot.java
155 lines (133 loc) · 4.02 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
package org.genericsystem.api.core;
import java.util.Collection;
import java.util.Iterator;
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> {
@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>.
*/
abstract Stream<T> stream();
/**
* 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> stream() {
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 <U extends IGeneric<U>> Snapshot<T> filter(Filters filter, U generic) {
return new Snapshot<T>() {
@Override
public Stream<T> stream() {
return Snapshot.this.stream().filter(g -> filter.getFilter(generic).test((U) g));
}
@Override
public T get(Object o) {
T result = Snapshot.this.get(o);
return result != null && filter.getFilter(generic).test((U) result) ? result : null;
}
};
}
default List<T> toList() {
return stream().collect(Collectors.toList());
}
}