aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java')
-rw-r--r--subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java b/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java
index 0a94d449..5e69e7af 100644
--- a/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java
+++ b/subprojects/store/src/main/java/tools/refinery/store/map/Cursors.java
@@ -5,6 +5,9 @@
5 */ 5 */
6package tools.refinery.store.map; 6package tools.refinery.store.map;
7 7
8import java.util.Iterator;
9import java.util.Map;
10
8public final class Cursors { 11public final class Cursors {
9 private Cursors() { 12 private Cursors() {
10 throw new IllegalStateException("This is a static utility class and should not be instantiated directly"); 13 throw new IllegalStateException("This is a static utility class and should not be instantiated directly");
@@ -14,6 +17,18 @@ public final class Cursors {
14 return new Empty<>(); 17 return new Empty<>();
15 } 18 }
16 19
20 public static <K, V> Cursor<K, V> singleton(K key, V value) {
21 return new Singleton<>(key, value);
22 }
23
24 public static <K, V> Cursor<K, V> of(Iterator<Map.Entry<K, V>> iterator) {
25 return new IteratorBasedCursor<>(iterator);
26 }
27
28 public static <K, V> Cursor<K, V> of(Map<K, V> map) {
29 return of(map.entrySet().iterator());
30 }
31
17 private static class Empty<K, V> implements Cursor<K, V> { 32 private static class Empty<K, V> implements Cursor<K, V> {
18 private boolean terminated = false; 33 private boolean terminated = false;
19 34
@@ -38,4 +53,53 @@ public final class Cursors {
38 return false; 53 return false;
39 } 54 }
40 } 55 }
56
57 private static class Singleton<K, V> implements Cursor<K, V> {
58 private State state = State.INITIAL;
59 private final K key;
60 private final V value;
61
62 public Singleton(K key, V value) {
63 this.key = key;
64 this.value = value;
65 }
66
67 @Override
68 public K getKey() {
69 if (state == State.STARTED) {
70 return key;
71 }
72 return null;
73 }
74
75 @Override
76 public V getValue() {
77 if (state == State.STARTED) {
78 return value;
79 }
80 return null;
81 }
82
83 @Override
84 public boolean isTerminated() {
85 return state == State.TERMINATED;
86 }
87
88 @Override
89 public boolean move() {
90 if (state == State.INITIAL) {
91 state = State.STARTED;
92 return true;
93 }
94 state = State.TERMINATED;
95 return false;
96 }
97
98
99 private enum State {
100 INITIAL,
101 STARTED,
102 TERMINATED
103 }
104 }
41} 105}