diff options
| author | 2024-08-18 23:29:37 +0800 | |
|---|---|---|
| committer | 2024-08-18 23:29:37 +0800 | |
| commit | c7116f9bd0471f8638b888472426e383f64cbcdc (patch) | |
| tree | 7ff2679d4b526338fad0b317db379ee76fd6f5fc /utils | |
| parent | Added Nothing/empty-tuple arguments (diff) | |
| download | orang-c7116f9bd0471f8638b888472426e383f64cbcdc.tar.gz orang-c7116f9bd0471f8638b888472426e383f64cbcdc.tar.xz orang-c7116f9bd0471f8638b888472426e383f64cbcdc.zip | |
Some more modularisation
Diffstat (limited to 'utils')
| -rw-r--r-- | utils/src/main/java/lv/enes/orang/utils/Codepoint.java | 8 | ||||
| -rw-r--r-- | utils/src/main/java/lv/enes/orang/utils/PeekableStream.java | 38 |
2 files changed, 46 insertions, 0 deletions
diff --git a/utils/src/main/java/lv/enes/orang/utils/Codepoint.java b/utils/src/main/java/lv/enes/orang/utils/Codepoint.java new file mode 100644 index 0000000..a981c5e --- /dev/null +++ b/utils/src/main/java/lv/enes/orang/utils/Codepoint.java | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | package lv.enes.orang.utils; | ||
| 2 | |||
| 3 | public record Codepoint(int cp) { | ||
| 4 | @Override | ||
| 5 | public String toString() { | ||
| 6 | return Character.toString(cp); | ||
| 7 | } | ||
| 8 | } | ||
diff --git a/utils/src/main/java/lv/enes/orang/utils/PeekableStream.java b/utils/src/main/java/lv/enes/orang/utils/PeekableStream.java new file mode 100644 index 0000000..7607a50 --- /dev/null +++ b/utils/src/main/java/lv/enes/orang/utils/PeekableStream.java | |||
| @@ -0,0 +1,38 @@ | |||
| 1 | package lv.enes.orang.utils; | ||
| 2 | |||
| 3 | import java.util.ArrayDeque; | ||
| 4 | import java.util.Deque; | ||
| 5 | import java.util.Iterator; | ||
| 6 | |||
| 7 | public class PeekableStream<T> implements Iterator<T> { | ||
| 8 | private final Iterator<T> input; | ||
| 9 | private final Deque<T> buffer = new ArrayDeque<>(); | ||
| 10 | |||
| 11 | public PeekableStream(Iterator<T> input) { | ||
| 12 | this.input = input; | ||
| 13 | } | ||
| 14 | |||
| 15 | @Override | ||
| 16 | public boolean hasNext() { | ||
| 17 | return !buffer.isEmpty() || input.hasNext(); | ||
| 18 | } | ||
| 19 | |||
| 20 | @Override | ||
| 21 | public T next() { | ||
| 22 | if (!buffer.isEmpty()) { | ||
| 23 | return buffer.pop(); | ||
| 24 | } else { | ||
| 25 | return input.next(); | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | public T peek() { | ||
| 30 | var value = next(); | ||
| 31 | putBack(value); | ||
| 32 | return value; | ||
| 33 | } | ||
| 34 | |||
| 35 | public void putBack(T value) { | ||
| 36 | buffer.push(value); | ||
| 37 | } | ||
| 38 | } | ||