diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2021-09-29 02:45:57 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2021-09-29 03:16:01 +0200 |
commit | a155f6ba02e08a75ce6e474a86900b8363f506e8 (patch) | |
tree | b78804c1c0f0968a9625f0656e08f5dadc16924c /store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java | |
parent | Simplify branding (diff) | |
download | refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.gz refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.tar.zst refinery-a155f6ba02e08a75ce6e474a86900b8363f506e8.zip |
build: migration to Gradle 7
Diffstat (limited to 'store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java')
-rw-r--r-- | store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java b/store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java new file mode 100644 index 00000000..b29b3119 --- /dev/null +++ b/store/src/main/java/org/eclipse/viatra/solver/data/map/CursorAsIterator.java | |||
@@ -0,0 +1,57 @@ | |||
1 | package org.eclipse.viatra.solver.data.map; | ||
2 | |||
3 | import java.util.Iterator; | ||
4 | import java.util.NoSuchElementException; | ||
5 | import java.util.function.BiFunction; | ||
6 | import java.util.function.BiPredicate; | ||
7 | |||
8 | public class CursorAsIterator<K,V,D> implements Iterator<D> { | ||
9 | private final Cursor<K, V> internal; | ||
10 | private final BiFunction<K, V, D> entryTransformation; | ||
11 | private final BiPredicate<K,V> filtering; | ||
12 | |||
13 | D lastValidElement; | ||
14 | |||
15 | public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation, BiPredicate<K,V> filtering) { | ||
16 | this.internal = internal; | ||
17 | this.entryTransformation = entryTransformation; | ||
18 | this.filtering = filtering; | ||
19 | |||
20 | moveToNext(); | ||
21 | } | ||
22 | public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation) { | ||
23 | this.internal = internal; | ||
24 | this.entryTransformation = entryTransformation; | ||
25 | this.filtering = ((k,v)->true); | ||
26 | |||
27 | moveToNext(); | ||
28 | } | ||
29 | |||
30 | private void moveToNext() { | ||
31 | internal.move(); | ||
32 | while(!internal.isTerminated() && !filtering.test(internal.getKey(), internal.getValue())) { | ||
33 | internal.move(); | ||
34 | } | ||
35 | if(!internal.isTerminated()) { | ||
36 | lastValidElement = entryTransformation.apply(internal.getKey(), internal.getValue()); | ||
37 | } | ||
38 | } | ||
39 | |||
40 | |||
41 | @Override | ||
42 | public boolean hasNext() { | ||
43 | return !internal.isTerminated(); | ||
44 | } | ||
45 | @Override | ||
46 | public D next() { | ||
47 | if(hasNext()) { | ||
48 | D last = lastValidElement; | ||
49 | moveToNext(); | ||
50 | return last; | ||
51 | } else { | ||
52 | throw new NoSuchElementException(); | ||
53 | } | ||
54 | |||
55 | } | ||
56 | |||
57 | } | ||