oving 5
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user