Added missing methods to Optional and ArrayList

This commit is contained in:
Ihromant 2020-06-12 13:58:24 +03:00 committed by Alexey Andreev
parent 7271a394db
commit 0c15600700
2 changed files with 22 additions and 0 deletions

View File

@ -16,6 +16,7 @@
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import java.util.Arrays; import java.util.Arrays;
import java.util.function.Consumer;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.lang.*; import org.teavm.classlib.java.lang.*;
import org.teavm.classlib.java.util.function.TUnaryOperator; import org.teavm.classlib.java.util.function.TUnaryOperator;
@ -187,4 +188,11 @@ public class TArrayList<E> extends TAbstractList<E> implements TCloneable, TSeri
array[i] = operator.apply(array[i]); array[i] = operator.apply(array[i]);
} }
} }
@Override
public void forEach(Consumer<? super E> action) {
for (int i = 0; i < size; i++) {
action.accept(array[i]);
}
}
} }

View File

@ -21,6 +21,8 @@ import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.teavm.classlib.java.util.stream.TStream;
public final class TOptional<T> { public final class TOptional<T> {
private static TOptional<?> emptyInstance; private static TOptional<?> emptyInstance;
private final T value; private final T value;
@ -56,6 +58,18 @@ public final class TOptional<T> {
return value != null; return value != null;
} }
public boolean isEmpty() {
return value == null;
}
public TStream<T> stream() {
if (!isPresent()) {
return TStream.empty();
} else {
return TStream.of(value);
}
}
public void ifPresent(Consumer<? super T> consumer) { public void ifPresent(Consumer<? super T> consumer) {
if (value != null) { if (value != null) {
consumer.accept(value); consumer.accept(value);