diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TOptional.java b/classlib/src/main/java/org/teavm/classlib/java/util/TOptional.java index a0f472e67..6da3e41ae 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TOptional.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TOptional.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.java.util; +import java.util.NoSuchElementException; import java.util.Objects; import java.util.function.Consumer; import java.util.function.Function; @@ -90,6 +91,15 @@ public final class TOptional { return value != null ? mapper.apply(value) : (TOptional) this; } + @SuppressWarnings("unchecked") + public TOptional or(Supplier> supplier) { + if (value != null) { + return this; + } else { + return Objects.requireNonNull((TOptional) supplier.get()); + } + } + public T orElse(T other) { return value != null ? value : other; } @@ -98,6 +108,14 @@ public final class TOptional { return value != null ? value : other.get(); } + public void ifPresentOrElse(Consumer action, Runnable emptyAction) { + if (value != null) { + action.accept(value); + } else { + emptyAction.run(); + } + } + public T orElseThrow(Supplier exceptionSupplier) throws X { if (value == null) { throw exceptionSupplier.get(); @@ -105,6 +123,13 @@ public final class TOptional { return value; } + public T orElseThrow() { + if (value == null) { + throw new NoSuchElementException(); + } + return value; + } + @Override public boolean equals(Object obj) { if (this == obj) { diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalDouble.java b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalDouble.java index 82b04a6bc..2d7434186 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalDouble.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalDouble.java @@ -19,6 +19,7 @@ import java.util.NoSuchElementException; import java.util.function.DoubleConsumer; import java.util.function.DoubleSupplier; import java.util.function.Supplier; +import org.teavm.classlib.java.util.stream.TDoubleStream; public class TOptionalDouble { private static TOptionalDouble emptyInstance; @@ -51,6 +52,10 @@ public class TOptionalDouble { return this != emptyInstance; } + public boolean isEmpty() { + return this == emptyInstance; + } + public void ifPresent(DoubleConsumer consumer) { if (this != emptyInstance) { consumer.accept(value); @@ -65,19 +70,42 @@ public class TOptionalDouble { return this != emptyInstance ? value : other.getAsDouble(); } + public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) { + if (this == emptyInstance) { + emptyAction.run(); + } else { + action.accept(value); + } + } + public double orElseThrow(Supplier exceptionSupplier) throws X { - if (!isPresent()) { + if (this == emptyInstance) { throw exceptionSupplier.get(); } return value; } + public double orElseThrow() { + if (this == emptyInstance) { + throw new NoSuchElementException(); + } + return value; + } + + public TDoubleStream stream() { + if (this == emptyInstance) { + return TDoubleStream.empty(); + } else { + return TDoubleStream.of(value); + } + } + @Override public boolean equals(Object obj) { if (this == obj) { return true; } - if (!(obj instanceof TOptionalDouble)) { + if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalDouble)) { return false; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalInt.java b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalInt.java index 4f92ef5ec..738f716cb 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalInt.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalInt.java @@ -19,6 +19,7 @@ import java.util.NoSuchElementException; import java.util.function.IntConsumer; import java.util.function.IntSupplier; import java.util.function.Supplier; +import org.teavm.classlib.java.util.stream.TIntStream; public class TOptionalInt { private static TOptionalInt emptyInstance; @@ -51,6 +52,10 @@ public class TOptionalInt { return this != emptyInstance; } + public boolean isEmpty() { + return this == emptyInstance; + } + public void ifPresent(IntConsumer consumer) { if (this != emptyInstance) { consumer.accept(value); @@ -65,19 +70,42 @@ public class TOptionalInt { return this != emptyInstance ? value : other.getAsInt(); } + public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) { + if (this == emptyInstance) { + emptyAction.run(); + } else { + action.accept(value); + } + } + public int orElseThrow(Supplier exceptionSupplier) throws X { - if (!isPresent()) { + if (this == emptyInstance) { throw exceptionSupplier.get(); } return value; } + public int orElseThrow() { + if (this == emptyInstance) { + throw new NoSuchElementException(); + } + return value; + } + + public TIntStream stream() { + if (this == emptyInstance) { + return TIntStream.empty(); + } else { + return TIntStream.of(value); + } + } + @Override public boolean equals(Object obj) { if (this == obj) { return true; } - if (!(obj instanceof TOptionalInt)) { + if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalInt)) { return false; } diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalLong.java b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalLong.java index a6f7d58d5..957527ef1 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalLong.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TOptionalLong.java @@ -19,6 +19,7 @@ import java.util.NoSuchElementException; import java.util.function.LongConsumer; import java.util.function.LongSupplier; import java.util.function.Supplier; +import org.teavm.classlib.java.util.stream.TLongStream; public class TOptionalLong { private static TOptionalLong emptyInstance; @@ -51,6 +52,10 @@ public class TOptionalLong { return this != emptyInstance; } + public boolean isEmpty() { + return this == emptyInstance; + } + public void ifPresent(LongConsumer consumer) { if (this != emptyInstance) { consumer.accept(value); @@ -65,19 +70,42 @@ public class TOptionalLong { return this != emptyInstance ? value : other.getAsLong(); } + public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) { + if (this == emptyInstance) { + emptyAction.run(); + } else { + action.accept(value); + } + } + public long orElseThrow(Supplier exceptionSupplier) throws X { - if (!isPresent()) { + if (this == emptyInstance) { throw exceptionSupplier.get(); } return value; } + public long orElseThrow() { + if (this == emptyInstance) { + throw new NoSuchElementException(); + } + return value; + } + + public TLongStream stream() { + if (this == emptyInstance) { + return TLongStream.empty(); + } else { + return TLongStream.of(value); + } + } + @Override public boolean equals(Object obj) { if (this == obj) { return true; } - if (!(obj instanceof TOptionalLong)) { + if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalLong)) { return false; }