mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-31 12:24:10 -08:00
Add missing Optional methods and fix bug in equals (#533)
This commit is contained in:
parent
dfef5ffd24
commit
baeb2a28a7
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
package org.teavm.classlib.java.util;
|
package org.teavm.classlib.java.util;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
@ -90,6 +91,15 @@ public final class TOptional<T> {
|
||||||
return value != null ? mapper.apply(value) : (TOptional<U>) this;
|
return value != null ? mapper.apply(value) : (TOptional<U>) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public TOptional<T> or(Supplier<? extends TOptional<? extends T>> supplier) {
|
||||||
|
if (value != null) {
|
||||||
|
return this;
|
||||||
|
} else {
|
||||||
|
return Objects.requireNonNull((TOptional<T>) supplier.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public T orElse(T other) {
|
public T orElse(T other) {
|
||||||
return value != null ? value : other;
|
return value != null ? value : other;
|
||||||
}
|
}
|
||||||
|
@ -98,6 +108,14 @@ public final class TOptional<T> {
|
||||||
return value != null ? value : other.get();
|
return value != null ? value : other.get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) {
|
||||||
|
if (value != null) {
|
||||||
|
action.accept(value);
|
||||||
|
} else {
|
||||||
|
emptyAction.run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
throw exceptionSupplier.get();
|
throw exceptionSupplier.get();
|
||||||
|
@ -105,6 +123,13 @@ public final class TOptional<T> {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public T orElseThrow() {
|
||||||
|
if (value == null) {
|
||||||
|
throw new NoSuchElementException();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.NoSuchElementException;
|
||||||
import java.util.function.DoubleConsumer;
|
import java.util.function.DoubleConsumer;
|
||||||
import java.util.function.DoubleSupplier;
|
import java.util.function.DoubleSupplier;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import org.teavm.classlib.java.util.stream.TDoubleStream;
|
||||||
|
|
||||||
public class TOptionalDouble {
|
public class TOptionalDouble {
|
||||||
private static TOptionalDouble emptyInstance;
|
private static TOptionalDouble emptyInstance;
|
||||||
|
@ -51,6 +52,10 @@ public class TOptionalDouble {
|
||||||
return this != emptyInstance;
|
return this != emptyInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return this == emptyInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public void ifPresent(DoubleConsumer consumer) {
|
public void ifPresent(DoubleConsumer consumer) {
|
||||||
if (this != emptyInstance) {
|
if (this != emptyInstance) {
|
||||||
consumer.accept(value);
|
consumer.accept(value);
|
||||||
|
@ -65,19 +70,42 @@ public class TOptionalDouble {
|
||||||
return this != emptyInstance ? value : other.getAsDouble();
|
return this != emptyInstance ? value : other.getAsDouble();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ifPresentOrElse(DoubleConsumer action, Runnable emptyAction) {
|
||||||
|
if (this == emptyInstance) {
|
||||||
|
emptyAction.run();
|
||||||
|
} else {
|
||||||
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
public <X extends Throwable> double orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
||||||
if (!isPresent()) {
|
if (this == emptyInstance) {
|
||||||
throw exceptionSupplier.get();
|
throw exceptionSupplier.get();
|
||||||
}
|
}
|
||||||
return value;
|
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
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof TOptionalDouble)) {
|
if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalDouble)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.NoSuchElementException;
|
||||||
import java.util.function.IntConsumer;
|
import java.util.function.IntConsumer;
|
||||||
import java.util.function.IntSupplier;
|
import java.util.function.IntSupplier;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import org.teavm.classlib.java.util.stream.TIntStream;
|
||||||
|
|
||||||
public class TOptionalInt {
|
public class TOptionalInt {
|
||||||
private static TOptionalInt emptyInstance;
|
private static TOptionalInt emptyInstance;
|
||||||
|
@ -51,6 +52,10 @@ public class TOptionalInt {
|
||||||
return this != emptyInstance;
|
return this != emptyInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return this == emptyInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public void ifPresent(IntConsumer consumer) {
|
public void ifPresent(IntConsumer consumer) {
|
||||||
if (this != emptyInstance) {
|
if (this != emptyInstance) {
|
||||||
consumer.accept(value);
|
consumer.accept(value);
|
||||||
|
@ -65,19 +70,42 @@ public class TOptionalInt {
|
||||||
return this != emptyInstance ? value : other.getAsInt();
|
return this != emptyInstance ? value : other.getAsInt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ifPresentOrElse(IntConsumer action, Runnable emptyAction) {
|
||||||
|
if (this == emptyInstance) {
|
||||||
|
emptyAction.run();
|
||||||
|
} else {
|
||||||
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
public <X extends Throwable> int orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
||||||
if (!isPresent()) {
|
if (this == emptyInstance) {
|
||||||
throw exceptionSupplier.get();
|
throw exceptionSupplier.get();
|
||||||
}
|
}
|
||||||
return value;
|
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
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof TOptionalInt)) {
|
if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalInt)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.NoSuchElementException;
|
||||||
import java.util.function.LongConsumer;
|
import java.util.function.LongConsumer;
|
||||||
import java.util.function.LongSupplier;
|
import java.util.function.LongSupplier;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import org.teavm.classlib.java.util.stream.TLongStream;
|
||||||
|
|
||||||
public class TOptionalLong {
|
public class TOptionalLong {
|
||||||
private static TOptionalLong emptyInstance;
|
private static TOptionalLong emptyInstance;
|
||||||
|
@ -51,6 +52,10 @@ public class TOptionalLong {
|
||||||
return this != emptyInstance;
|
return this != emptyInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return this == emptyInstance;
|
||||||
|
}
|
||||||
|
|
||||||
public void ifPresent(LongConsumer consumer) {
|
public void ifPresent(LongConsumer consumer) {
|
||||||
if (this != emptyInstance) {
|
if (this != emptyInstance) {
|
||||||
consumer.accept(value);
|
consumer.accept(value);
|
||||||
|
@ -65,19 +70,42 @@ public class TOptionalLong {
|
||||||
return this != emptyInstance ? value : other.getAsLong();
|
return this != emptyInstance ? value : other.getAsLong();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void ifPresentOrElse(LongConsumer action, Runnable emptyAction) {
|
||||||
|
if (this == emptyInstance) {
|
||||||
|
emptyAction.run();
|
||||||
|
} else {
|
||||||
|
action.accept(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public <X extends Throwable> long orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
public <X extends Throwable> long orElseThrow(Supplier<? extends X> exceptionSupplier) throws X {
|
||||||
if (!isPresent()) {
|
if (this == emptyInstance) {
|
||||||
throw exceptionSupplier.get();
|
throw exceptionSupplier.get();
|
||||||
}
|
}
|
||||||
return value;
|
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
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (!(obj instanceof TOptionalLong)) {
|
if (this == emptyInstance || obj == emptyInstance || !(obj instanceof TOptionalLong)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user