diff options
author | 2021-08-15 15:17:53 +0200 | |
---|---|---|
committer | 2021-08-15 15:17:53 +0200 | |
commit | 2ff73d68f76d873b783175a50d80626c0be7f1ef (patch) | |
tree | b7946a8e8352dc7c8c5738fed42221216c8f054a /model-data | |
parent | views of a for RelationRepresentations that can be queried. (diff) | |
download | refinery-2ff73d68f76d873b783175a50d80626c0be7f1ef.tar.gz refinery-2ff73d68f76d873b783175a50d80626c0be7f1ef.tar.zst refinery-2ff73d68f76d873b783175a50d80626c0be7f1ef.zip |
Added filter
Diffstat (limited to 'model-data')
-rw-r--r-- | model-data/src/main/java/org/eclipse/viatra/solver/data/util/CollectionsUtil.java | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/model-data/src/main/java/org/eclipse/viatra/solver/data/util/CollectionsUtil.java b/model-data/src/main/java/org/eclipse/viatra/solver/data/util/CollectionsUtil.java index d950228d..7b6bf9b3 100644 --- a/model-data/src/main/java/org/eclipse/viatra/solver/data/util/CollectionsUtil.java +++ b/model-data/src/main/java/org/eclipse/viatra/solver/data/util/CollectionsUtil.java | |||
@@ -1,7 +1,9 @@ | |||
1 | package org.eclipse.viatra.solver.data.util; | 1 | package org.eclipse.viatra.solver.data.util; |
2 | 2 | ||
3 | import java.util.Iterator; | 3 | import java.util.Iterator; |
4 | import java.util.NoSuchElementException; | ||
4 | import java.util.function.Function; | 5 | import java.util.function.Function; |
6 | import java.util.function.Predicate; | ||
5 | 7 | ||
6 | public final class CollectionsUtil { | 8 | public final class CollectionsUtil { |
7 | private CollectionsUtil() { | 9 | private CollectionsUtil() { |
@@ -26,4 +28,45 @@ public final class CollectionsUtil { | |||
26 | static <S,T> Iterable<T> map(Iterable<S> source, Function<S, T> transformation) { | 28 | static <S,T> Iterable<T> map(Iterable<S> source, Function<S, T> transformation) { |
27 | return (()->map(source.iterator(),transformation)); | 29 | return (()->map(source.iterator(),transformation)); |
28 | } | 30 | } |
31 | |||
32 | static <T> Iterator<T> filter(Iterator<T> source, Predicate<T> condition) { | ||
33 | return new Iterator<T>() { | ||
34 | T internalNext = move(); | ||
35 | boolean internalHasNext; | ||
36 | |||
37 | private T move() { | ||
38 | internalHasNext = source.hasNext(); | ||
39 | if(internalHasNext) { | ||
40 | internalNext = source.next(); | ||
41 | } | ||
42 | while(internalHasNext && !condition.test(internalNext)) { | ||
43 | internalHasNext = source.hasNext(); | ||
44 | if(internalHasNext) { | ||
45 | internalNext = source.next(); | ||
46 | } | ||
47 | } | ||
48 | return internalNext; | ||
49 | } | ||
50 | |||
51 | @Override | ||
52 | public boolean hasNext() { | ||
53 | return internalHasNext; | ||
54 | } | ||
55 | |||
56 | @Override | ||
57 | public T next() { | ||
58 | if(!internalHasNext) { | ||
59 | throw new NoSuchElementException(); | ||
60 | } else { | ||
61 | T result = internalNext; | ||
62 | move(); | ||
63 | return result; | ||
64 | } | ||
65 | } | ||
66 | }; | ||
67 | } | ||
68 | |||
69 | static <T> Iterable<T> filter(Iterable<T> source, Predicate<T> condition) { | ||
70 | return (()->filter(source.iterator(),condition)); | ||
71 | } | ||
29 | } | 72 | } |