com.landawn.abacus.util.u Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of abacus-android Show documentation
Show all versions of abacus-android Show documentation
A general and simple library for Android
/*
* Copyright (c) 2019, Haiyang Li.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.landawn.abacus.util;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import com.landawn.abacus.util.function.Supplier;
import com.landawn.abacus.util.stream.DoubleStream;
import com.landawn.abacus.util.stream.IntStream;
import com.landawn.abacus.util.stream.LongStream;
import com.landawn.abacus.util.stream.Stream;
public class u {
private u() {
// utility class
}
public static final class Optional {
private static final Optional> EMPTY = new Optional<>();
private final T value;
private Optional() {
this.value = null;
}
private Optional(T value) {
this.value = N.checkArgNotNull(value);
}
public static Optional empty() {
return (Optional) EMPTY;
}
public static Optional of(T value) {
return new Optional<>(value);
}
public static Optional ofNullable(T value) {
if (value == null) {
return empty();
}
return new Optional<>(value);
}
public static Optional from(java.util.Optional op) {
if (op.isPresent()) {
return of(op.get());
} else {
return empty();
}
}
public T get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return value != null;
}
public boolean isEmpty() {
return value == null;
}
/**
*
* @param action
* @return itself
* @throws E
*/
public Optional ifPresent(Try.Consumer super T, E> action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent()) {
action.accept(value);
}
return this;
}
/**
*
* @param action
* @param emptyAction
* @return itself
* @throws E
* @throws E2
*/
public Optional ifPresentOrElse(Try.Consumer super T, E> action, Try.Runnable emptyAction)
throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent()) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public Optional filter(Try.Predicate super T, E> predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent() && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public Nullable map(final Try.Function super T, ? extends U, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return Nullable. of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalBoolean mapToBoolean(final Try.ToBooleanFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalBoolean.of(mapper.applyAsBoolean(value));
} else {
return OptionalBoolean.empty();
}
}
public OptionalChar mapToChar(final Try.ToCharFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalChar.of(mapper.applyAsChar(value));
} else {
return OptionalChar.empty();
}
}
public OptionalByte mapToByte(final Try.ToByteFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalByte.of(mapper.applyAsByte(value));
} else {
return OptionalByte.empty();
}
}
public OptionalShort mapToShort(final Try.ToShortFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalShort.of(mapper.applyAsShort(value));
} else {
return OptionalShort.empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public OptionalLong mapToLong(final Try.ToLongFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return OptionalLong.empty();
}
}
public OptionalFloat mapToFloat(final Try.ToFloatFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalFloat.of(mapper.applyAsFloat(value));
} else {
return OptionalFloat.empty();
}
}
public OptionalDouble mapToDouble(final Try.ToDoubleFunction super T, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
public Optional flatMap(Try.Function super T, Optional, E> mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public Optional or(Try.Supplier, E> supplier) throws E {
N.checkArgNotNull(supplier, "supplier");
if (isPresent()) {
return this;
} else {
return Objects.requireNonNull((Optional) supplier.get());
}
}
public T orNull() {
return isPresent() ? value : null;
}
public T orElse(T other) {
return isPresent() ? value : other;
}
public T orElseGet(Try.Supplier extends T, E> other) throws E {
if (isPresent()) {
return value;
} else {
return other.get();
}
}
// public T orElseNull() {
// return isPresent() ? value : null;
// }
public T orElseThrow() throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException("No value is present");
}
}
public T orElseThrow(Supplier extends X> exceptionSupplier) throws X {
if (isPresent()) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public Stream stream() {
if (isPresent()) {
return Stream.of(value);
} else {
return Stream. empty();
}
}
public List toList() {
if (isPresent()) {
return N.asList(value);
} else {
return new ArrayList<>();
}
}
public Set toSet() {
if (isPresent()) {
return N.asSet(value);
} else {
return new HashSet<>();
}
}
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList. empty();
}
}
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet. empty();
}
}
public java.util.Optional __() {
if (isPresent()) {
return java.util.Optional.of(value);
} else {
return java.util.Optional.empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof Optional) {
final Optional> other = (Optional>) obj;
return N.equals(value, other.value);
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent()) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent()) {
return String.format("Optional[%s]", N.toString(value));
}
return "Optional.empty";
}
}
public static final class OptionalBoolean implements Comparable {
private static final OptionalBoolean EMPTY = new OptionalBoolean();
private static final OptionalBoolean TRUE = new OptionalBoolean(true);
private static final OptionalBoolean FALSE = new OptionalBoolean(false);
private final boolean value;
private final boolean isPresent;
private OptionalBoolean() {
this.value = false;
this.isPresent = false;
}
private OptionalBoolean(boolean value) {
this.value = value;
this.isPresent = true;
}
public static OptionalBoolean empty() {
return EMPTY;
}
public static OptionalBoolean of(boolean value) {
return value ? TRUE : FALSE;
}
public static OptionalBoolean ofNullable(Boolean val) {
if (val == null) {
return empty();
} else {
return of(val);
}
}
public boolean get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalBoolean ifPresent(Try.BooleanConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalBoolean ifPresentOrElse(Try.BooleanConsumer action, Try.Runnable emptyAction)
throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalBoolean filter(Try.BooleanPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalBoolean map(final Try.BooleanUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalBoolean.of(mapper.applyAsBoolean(value));
} else {
return empty();
}
}
public Nullable mapToObj(final Try.BooleanFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalBoolean flatMap(Try.BooleanFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalBoolean or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public boolean orFalse() {
return isPresent ? value : false;
}
// public boolean orElseFalse() {
// return isPresent ? value : false;
// }
public boolean orTrue() {
return isPresent ? value : true;
}
// public boolean orElseTrue() {
// return isPresent ? value : true;
// }
public boolean orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public boolean orElse(boolean other) {
return isPresent ? value : other;
}
public boolean orElseGet(Try.BooleanSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsBoolean();
}
}
public boolean orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalBoolean optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Boolean.compare(this.get(), optional.get());
}
public Stream stream() {
if (isPresent) {
return Stream.of(value);
} else {
return Stream.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalBoolean) {
final OptionalBoolean other = (OptionalBoolean) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalBoolean[%s]", value);
}
return "OptionalBoolean.empty";
}
}
public static final class OptionalChar implements Comparable {
private static final OptionalChar EMPTY = new OptionalChar();
private static final OptionalChar CHAR_0 = new OptionalChar(N.CHAR_0);
private final char value;
private final boolean isPresent;
private OptionalChar() {
this.value = 0;
this.isPresent = false;
}
private OptionalChar(char value) {
this.value = value;
this.isPresent = true;
}
public static OptionalChar empty() {
return EMPTY;
}
public static OptionalChar of(char value) {
return value == N.CHAR_0 ? CHAR_0 : new OptionalChar(value);
}
public static OptionalChar ofNullable(Character val) {
if (val == null) {
return empty();
} else {
return of(val);
}
}
public char get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalChar ifPresent(Try.CharConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent()) {
action.accept(value);
}
return this;
}
public OptionalChar ifPresentOrElse(Try.CharConsumer action, Try.Runnable emptyAction) throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent()) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalChar filter(Try.CharPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent() && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalChar map(final Try.CharUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return OptionalChar.of(mapper.applyAsChar(value));
} else {
return empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public Nullable mapToObj(final Try.CharFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalChar flatMap(Try.CharFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent()) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalChar or(Try.Supplier supplier) throws E {
if (isPresent()) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public char orZero() {
return isPresent() ? value : 0;
}
// public char orElseZero() {
// return isPresent() ? value : 0;
// }
public char orElseThrow() throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public char orElse(char other) {
return isPresent() ? value : other;
}
public char orElseGet(Try.CharSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent()) {
return value;
} else {
return other.getAsChar();
}
}
public char orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent()) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalChar optional) {
if (optional == null || optional.isPresent() == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Character.compare(this.get(), optional.get());
}
public Optional boxed() {
if (isPresent()) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalChar) {
final OptionalChar other = (OptionalChar) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent()) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent()) {
return String.format("OptionalChar[%s]", value);
}
return "OptionalChar.empty";
}
}
public static final class OptionalByte implements Comparable {
private static final OptionalByte EMPTY = new OptionalByte();
private static final OptionalByte[] POOL = new OptionalByte[256];
static {
for (int i = 0; i < 256; i++) {
POOL[i] = new OptionalByte((byte) (i - 128));
}
}
private final byte value;
private final boolean isPresent;
private OptionalByte() {
this.value = 0;
this.isPresent = false;
}
private OptionalByte(byte value) {
this.value = value;
this.isPresent = true;
}
public static OptionalByte empty() {
return EMPTY;
}
public static OptionalByte of(byte value) {
return POOL[value - Byte.MIN_VALUE];
}
public static OptionalByte ofNullable(Byte val) {
if (val == null) {
return empty();
} else {
return OptionalByte.of(val);
}
}
public byte get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalByte ifPresent(Try.ByteConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalByte ifPresentOrElse(Try.ByteConsumer action, Try.Runnable emptyAction) throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalByte filter(Try.BytePredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalByte map(final Try.ByteUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalByte.of(mapper.applyAsByte(value));
} else {
return empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public Nullable mapToObj(final Try.ByteFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalByte flatMap(Try.ByteFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalByte or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public byte orZero() {
return isPresent ? value : 0;
}
// public byte orElseZero() {
// return isPresent ? value : 0;
// }
public byte orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public byte orElse(byte other) {
return isPresent ? value : other;
}
public byte orElseGet(Try.ByteSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsByte();
}
}
public byte orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalByte optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Byte.compare(this.get(), optional.get());
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalByte) {
final OptionalByte other = (OptionalByte) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalByte[%s]", value);
}
return "OptionalByte.empty";
}
}
public static final class OptionalShort implements Comparable {
private static final OptionalShort EMPTY = new OptionalShort();
private final short value;
private final boolean isPresent;
private OptionalShort() {
this.value = 0;
this.isPresent = false;
}
private OptionalShort(short value) {
this.value = value;
this.isPresent = true;
}
public static OptionalShort empty() {
return EMPTY;
}
public static OptionalShort of(short value) {
return new OptionalShort(value);
}
public static OptionalShort ofNullable(Short val) {
if (val == null) {
return empty();
} else {
return OptionalShort.of(val);
}
}
public short get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalShort ifPresent(Try.ShortConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalShort ifPresentOrElse(Try.ShortConsumer action, Try.Runnable emptyAction)
throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalShort filter(Try.ShortPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalShort map(final Try.ShortUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalShort.of(mapper.applyAsShort(value));
} else {
return empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public Nullable mapToObj(final Try.ShortFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalShort flatMap(Try.ShortFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalShort or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public short orZero() {
return isPresent ? value : 0;
}
// public short orElseZero() {
// return isPresent ? value : 0;
// }
public short orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public short orElse(short other) {
return isPresent ? value : other;
}
public short orElseGet(Try.ShortSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsShort();
}
}
public short orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalShort optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Short.compare(this.get(), optional.get());
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalShort) {
final OptionalShort other = (OptionalShort) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalShort[%s]", value);
}
return "OptionalShort.empty";
}
}
public static final class OptionalInt implements Comparable {
private static final OptionalInt EMPTY = new OptionalInt();
private static final int MIN_CACHED_VALUE = -128;
private static final int MAX_CACHED_VALUE = 1025;
private static final OptionalInt[] POOL = new OptionalInt[MAX_CACHED_VALUE - MIN_CACHED_VALUE];
static {
for (int i = 0, to = MAX_CACHED_VALUE - MIN_CACHED_VALUE; i < to; i++) {
POOL[i] = new OptionalInt(i + MIN_CACHED_VALUE);
}
}
private final int value;
private final boolean isPresent;
private OptionalInt() {
this.value = 0;
this.isPresent = false;
}
private OptionalInt(int value) {
this.value = value;
this.isPresent = true;
}
public static OptionalInt empty() {
return EMPTY;
}
public static OptionalInt of(int value) {
return value >= MIN_CACHED_VALUE && value < MAX_CACHED_VALUE ? POOL[value - MIN_CACHED_VALUE] : new OptionalInt(value);
}
public static OptionalInt ofNullable(Integer val) {
if (val == null) {
return empty();
} else {
return OptionalInt.of(val);
}
}
public static OptionalInt from(java.util.OptionalInt op) {
if (op.isPresent()) {
return of(op.getAsInt());
} else {
return empty();
}
}
public int get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalInt ifPresent(Try.IntConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalInt ifPresentOrElse(Try.IntConsumer action, Try.Runnable emptyAction) throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalInt filter(Try.IntPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalInt map(final Try.IntUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return empty();
}
}
public OptionalLong mapToLong(final Try.ToLongFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return OptionalLong.empty();
}
}
public OptionalDouble mapToDouble(final Try.ToDoubleFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
public Nullable mapToObj(final Try.IntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalInt flatMap(Try.IntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalInt or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public int orZero() {
return isPresent ? value : 0;
}
// public int orElseZero() {
// return isPresent ? value : 0;
// }
public int orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public int orElse(int other) {
return isPresent ? value : other;
}
public int orElseGet(Try.IntSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsInt();
}
}
public int orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalInt optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Integer.compare(this.get(), optional.get());
}
public IntStream stream() {
if (isPresent) {
return IntStream.of(value);
} else {
return IntStream.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
public java.util.OptionalInt __() {
if (isPresent) {
return java.util.OptionalInt.of(value);
} else {
return java.util.OptionalInt.empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalInt) {
final OptionalInt other = (OptionalInt) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalInt[%s]", value);
}
return "OptionalInt.empty";
}
}
public static final class OptionalLong implements Comparable {
private static final OptionalLong EMPTY = new OptionalLong();
private final long value;
private final boolean isPresent;
private OptionalLong() {
this.value = 0;
this.isPresent = false;
}
private OptionalLong(long value) {
this.value = value;
this.isPresent = true;
}
public static OptionalLong empty() {
return EMPTY;
}
public static OptionalLong of(long value) {
return new OptionalLong(value);
}
public static OptionalLong ofNullable(Long val) {
if (val == null) {
return empty();
} else {
return OptionalLong.of(val);
}
}
public static OptionalLong from(java.util.OptionalLong op) {
if (op.isPresent()) {
return of(op.getAsLong());
} else {
return empty();
}
}
public long get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalLong ifPresent(Try.LongConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalLong ifPresentOrElse(Try.LongConsumer action, Try.Runnable emptyAction) throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalLong filter(Try.LongPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalLong map(final Try.LongUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public OptionalDouble mapToDouble(final Try.ToDoubleFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
public Nullable mapToObj(final Try.LongFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalLong flatMap(Try.LongFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalLong or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public long orZero() {
return isPresent ? value : 0;
}
// public long orElseZero() {
// return isPresent ? value : 0;
// }
public long orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public long orElse(long other) {
return isPresent ? value : other;
}
public long orElseGet(Try.LongSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsLong();
}
}
public long orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalLong optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Long.compare(this.get(), optional.get());
}
public LongStream stream() {
if (isPresent) {
return LongStream.of(value);
} else {
return LongStream.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
public java.util.OptionalLong __() {
if (isPresent) {
return java.util.OptionalLong.of(value);
} else {
return java.util.OptionalLong.empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalLong) {
final OptionalLong other = (OptionalLong) obj;
return (isPresent && other.isPresent) ? value == other.value : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalLong[%s]", value);
}
return "OptionalLong.empty";
}
}
public static final class OptionalFloat implements Comparable {
private static final OptionalFloat EMPTY = new OptionalFloat();
private final float value;
private final boolean isPresent;
private OptionalFloat() {
this.value = 0;
this.isPresent = false;
}
private OptionalFloat(float value) {
this.value = value;
this.isPresent = true;
}
public static OptionalFloat empty() {
return EMPTY;
}
public static OptionalFloat of(float value) {
return new OptionalFloat(value);
}
public static OptionalFloat ofNullable(Float val) {
if (val == null) {
return empty();
} else {
return OptionalFloat.of(val);
}
}
public float get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalFloat ifPresent(Try.FloatConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalFloat ifPresentOrElse(Try.FloatConsumer action, Try.Runnable emptyAction)
throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalFloat filter(Try.FloatPredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalFloat map(final Try.FloatUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalFloat.of(mapper.applyAsFloat(value));
} else {
return empty();
}
}
public OptionalDouble mapToDouble(final Try.ToDoubleFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
public Nullable mapToObj(final Try.FloatFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalFloat flatMap(Try.FloatFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalFloat or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public float orZero() {
return isPresent ? value : 0;
}
// public float orElseZero() {
// return isPresent ? value : 0;
// }
public float orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public float orElse(float other) {
return isPresent ? value : other;
}
public float orElseGet(Try.FloatSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsFloat();
}
}
public float orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalFloat optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Float.compare(this.get(), optional.get());
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional. empty();
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof OptionalFloat) {
final OptionalFloat other = (OptionalFloat) obj;
return (isPresent && other.isPresent) ? N.equals(value, other.value) : isPresent == other.isPresent;
}
return false;
}
@Override
public int hashCode() {
return N.hashCode(isPresent) * 31 + N.hashCode(value);
}
@Override
public String toString() {
if (isPresent) {
return String.format("OptionalFloat[%s]", value);
}
return "OptionalFloat.empty";
}
}
public static final class OptionalDouble implements Comparable {
private static final OptionalDouble EMPTY = new OptionalDouble();
private final double value;
private final boolean isPresent;
private OptionalDouble() {
this.value = 0;
this.isPresent = false;
}
private OptionalDouble(double value) {
this.value = value;
this.isPresent = true;
}
public static OptionalDouble empty() {
return EMPTY;
}
public static OptionalDouble of(double value) {
return new OptionalDouble(value);
}
public static OptionalDouble ofNullable(Double val) {
if (val == null) {
return empty();
} else {
return OptionalDouble.of(val);
}
}
public static OptionalDouble from(java.util.OptionalDouble op) {
if (op.isPresent()) {
return of(op.getAsDouble());
} else {
return empty();
}
}
public double get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public OptionalDouble ifPresent(Try.DoubleConsumer action) throws E {
N.checkArgNotNull(action, "action");
if (isPresent) {
action.accept(value);
}
return this;
}
public OptionalDouble ifPresentOrElse(Try.DoubleConsumer action, Try.Runnable emptyAction)
throws E, E2 {
N.checkArgNotNull(action, "action");
N.checkArgNotNull(emptyAction, "emptyAction");
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
public OptionalDouble filter(Try.DoublePredicate predicate) throws E {
N.checkArgNotNull(predicate, "predicate");
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
public OptionalDouble map(final Try.DoubleUnaryOperator mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return empty();
}
}
public OptionalInt mapToInt(final Try.ToIntFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
public OptionalLong mapToLong(final Try.ToLongFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return OptionalLong.empty();
}
}
public Nullable mapToObj(final Try.DoubleFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable. empty();
}
}
public OptionalDouble flatMap(Try.DoubleFunction mapper) throws E {
N.checkArgNotNull(mapper, "mapper");
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
public OptionalDouble or(Try.Supplier supplier) throws E {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
public double orZero() {
return isPresent ? value : 0;
}
// public double orElseZero() {
// return isPresent ? value : 0;
// }
public double orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException("No value present");
}
}
public double orElse(double other) {
return isPresent ? value : other;
}
public double orElseGet(Try.DoubleSupplier other) throws E {
N.checkArgNotNull(other, "other");
if (isPresent) {
return value;
} else {
return other.getAsDouble();
}
}
public double orElseThrow(Supplier extends X> exceptionSupplier) throws X {
N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
@Override
public int compareTo(OptionalDouble optional) {
if (optional == null || optional.isPresent == false) {
return isPresent ? 1 : 0;
}
if (isPresent == false) {
return -1;
}
return Double.compare(this.get(), optional.get());
}
public DoubleStream stream() {
if (isPresent) {
return DoubleStream.of(value);
} else {
return DoubleStream.empty();
}
}
public Optional