diff --git a/src/main/java/oving5/BinaryComputingIterator.java b/src/main/java/oving5/BinaryComputingIterator.java index 18fbe86..072b0b6 100644 --- a/src/main/java/oving5/BinaryComputingIterator.java +++ b/src/main/java/oving5/BinaryComputingIterator.java @@ -4,18 +4,63 @@ import java.util.Iterator; import java.util.function.BinaryOperator; public class BinaryComputingIterator { + private Iterator iterator1; + private Iterator iterator2; + + private Double default1; + private Double default2; + + private BinaryOperator operator; + BinaryComputingIterator(Iterator iterator1, Iterator iterator2, BinaryOperator operator) { + if (iterator1 == null || iterator2 == null || operator == null) { + throw new IllegalArgumentException("argument cannot be null"); + } + this.iterator1 = iterator1; + this.iterator2 = iterator2; + this.operator = operator; } BinaryComputingIterator(Iterator iterator1, Iterator iterator2, Double default1, Double default2, BinaryOperator operator) { + if (iterator1 == null || iterator2 == null || operator == null) { + throw new IllegalArgumentException("argument cannot be null"); + } + this.iterator1 = iterator1; + this.iterator2 = iterator2; + this.operator = operator; + this.default1 = default1; + this.default2 = default2; } boolean hasNext() { + if (!iterator1.hasNext() && !iterator2.hasNext()) { + return false; + } + if ((iterator1.hasNext() || default1 != null) && (iterator2.hasNext() || default2 != null)) { + return true; + } return false; } Double next() { - return 0.0; + if (hasNext()) { + Double a = 0.0; + if (iterator1.hasNext()) { + a = iterator1.next(); + } else { + a = default1; + } + + Double b = 0.0; + if (iterator2.hasNext()) { + b = iterator2.next(); + } else { + b = default2; + } + + return operator.apply(a, b); + } + throw new IllegalArgumentException("empty things"); } } diff --git a/src/main/java/oving5/debugging/StringMergingIterator.java b/src/main/java/oving5/debugging/StringMergingIterator.java index edc53c5..d1016bf 100644 --- a/src/main/java/oving5/debugging/StringMergingIterator.java +++ b/src/main/java/oving5/debugging/StringMergingIterator.java @@ -27,18 +27,17 @@ public class StringMergingIterator implements Iterator { String result = null; - if (!this.first.hasNext()) { + if (this.turnSwitch && this.first.hasNext()) { result = this.first.next(); - } else if (!this.second.hasNext()) { + } else if (!this.turnSwitch && this.second.hasNext()) { result = this.second.next(); } else { - if (this.turnSwitch) { + if (this.first.hasNext()) { result = this.first.next(); - this.turnSwitch = false; - } - if (!this.turnSwitch) { + } else if (this.second.hasNext()) { result = this.second.next(); - this.turnSwitch = true; + } else { + throw new NoSuchElementException(); } } diff --git a/src/test/java/oving5/BinaryComputingIteratorTest.java.unimplemented b/src/test/java/oving5/BinaryComputingIteratorTest.java similarity index 100% rename from src/test/java/oving5/BinaryComputingIteratorTest.java.unimplemented rename to src/test/java/oving5/BinaryComputingIteratorTest.java