aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/store/src/main/java/tools/refinery/store/map/IteratorBasedCursor.java
blob: 0ed9b73056916433d16ead36747b432cf30dd9ca (plain) (blame)
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
/*
 * SPDX-FileCopyrightText: 2023 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */
package tools.refinery.store.map;

import java.util.Iterator;
import java.util.Map;

public class IteratorBasedCursor<K, V> implements Cursor<K, V> {
	private final Iterator<Map.Entry<K, V>> iterator;
	private Map.Entry<K, V> entry;
	private boolean terminated;

	public IteratorBasedCursor(Iterator<Map.Entry<K, V>> iterator) {
		this.iterator = iterator;
	}

	@Override
	public K getKey() {
		return entry.getKey();
	}

	@Override
	public V getValue() {
		return entry.getValue();
	}

	@Override
	public boolean isTerminated() {
		return terminated;
	}

	@Override
	public boolean move() {
		if (!terminated && iterator.hasNext()) {
			entry = iterator.next();
			return true;
		}
		terminated = true;
		return false;
	}
}