From 1a6645482bcf5a7355760601a15b07efe3e87dee Mon Sep 17 00:00:00 2001 From: Andreas Omholt Olsen Date: Sun, 15 Feb 2026 17:13:35 +0100 Subject: [PATCH] Add debugging for oving5 --- .../debugging/StringMergingIterator.java | 47 +++++++++++++++++++ .../StringMergingIteratorProgram.java | 40 ++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 src/main/java/oving5/debugging/StringMergingIterator.java create mode 100644 src/main/java/oving5/debugging/StringMergingIteratorProgram.java diff --git a/src/main/java/oving5/debugging/StringMergingIterator.java b/src/main/java/oving5/debugging/StringMergingIterator.java new file mode 100644 index 0000000..edc53c5 --- /dev/null +++ b/src/main/java/oving5/debugging/StringMergingIterator.java @@ -0,0 +1,47 @@ +package oving5.debugging; + +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class StringMergingIterator implements Iterator { + + private final Iterator first; + private final Iterator second; + private boolean turnSwitch = true; + + public StringMergingIterator(Iterator first, Iterator second) { + this.first = first; + this.second = second; + } + + @Override + public boolean hasNext() { + return this.first.hasNext() || this.second.hasNext(); + } + + @Override + public String next() { + if (!this.hasNext()) { + throw new NoSuchElementException(); + } + + String result = null; + + if (!this.first.hasNext()) { + result = this.first.next(); + } else if (!this.second.hasNext()) { + result = this.second.next(); + } else { + if (this.turnSwitch) { + result = this.first.next(); + this.turnSwitch = false; + } + if (!this.turnSwitch) { + result = this.second.next(); + this.turnSwitch = true; + } + } + + return result; + } +} diff --git a/src/main/java/oving5/debugging/StringMergingIteratorProgram.java b/src/main/java/oving5/debugging/StringMergingIteratorProgram.java new file mode 100644 index 0000000..25abc7b --- /dev/null +++ b/src/main/java/oving5/debugging/StringMergingIteratorProgram.java @@ -0,0 +1,40 @@ +package oving5.debugging; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class StringMergingIteratorProgram { + + public static void main(String[] args) throws Exception { + Iterator one = List.of("a", "b").iterator(); + Iterator two = List.of("c", "d", "e").iterator(); + + StringMergingIterator mergeIterator = new StringMergingIterator(one, two); + + List values = new ArrayList<>(); + + while (mergeIterator.hasNext()) { + values.add(mergeIterator.next()); + } + + List expectedOutput = List.of("a", "c", "b", "d", "e"); + + if (values.size() != expectedOutput.size()) { + throw new Exception( + "The merged output did not contain the expected number of values. Try using " + + "the VS Code debugger to see the difference between the lists"); + } + + for (int i = 0; i < expectedOutput.size(); i++) { + if (!values.get(i).equals(expectedOutput.get(i))) { + throw new Exception( + "The iterator did not correctly merge the output. Try using the VS Code " + + "debugger to see the difference between the lists"); + } + } + + System.out.println( + "Success! StringMergingIterator correctly merged the output of the two lists"); + } +}