This commit is contained in:
2026-03-03 01:47:32 +01:00
parent 86a691b044
commit 0dfde2ddfb
3 changed files with 52 additions and 8 deletions

View File

@@ -4,18 +4,63 @@ import java.util.Iterator;
import java.util.function.BinaryOperator;
public class BinaryComputingIterator {
private Iterator<Double> iterator1;
private Iterator<Double> iterator2;
private Double default1;
private Double default2;
private BinaryOperator<Double> operator;
BinaryComputingIterator(Iterator<Double> iterator1, Iterator<Double> iterator2, BinaryOperator<Double> 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<Double> iterator1, Iterator<Double> iterator2, Double default1, Double default2,
BinaryOperator<Double> 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");
}
}

View File

@@ -27,18 +27,17 @@ public class StringMergingIterator implements Iterator<String> {
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();
}
}