Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.landawn.abacus.util.u Maven / Gradle / Ivy
/*
* 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.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.DoubleSupplier;
import java.util.function.IntSupplier;
import java.util.function.LongSupplier;
import java.util.function.Supplier;
import com.landawn.abacus.annotation.Beta;
import com.landawn.abacus.annotation.SuppressFBWarnings;
import com.landawn.abacus.util.function.ByteSupplier;
import com.landawn.abacus.util.function.CharSupplier;
import com.landawn.abacus.util.function.FloatSupplier;
import com.landawn.abacus.util.function.ShortSupplier;
import com.landawn.abacus.util.stream.ByteStream;
import com.landawn.abacus.util.stream.CharStream;
import com.landawn.abacus.util.stream.DoubleStream;
import com.landawn.abacus.util.stream.FloatStream;
import com.landawn.abacus.util.stream.IntStream;
import com.landawn.abacus.util.stream.LongStream;
import com.landawn.abacus.util.stream.ShortStream;
import com.landawn.abacus.util.stream.Stream;
public class u { // NOSONAR
private static final String NO_VALUE_PRESENT = "No value present"; // should change it to InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX
private u() {
// utility class
}
/**
* The Class Optional.
*
* @param
*/
@com.landawn.abacus.annotation.Immutable
public static final class Optional implements Immutable {
/** Presents {@code Boolean.TRUE}. */
public static final Optional TRUE = new Optional<>(Boolean.TRUE);
/** Presents {@code Boolean.FALSE}. */
public static final Optional FALSE = new Optional<>(Boolean.FALSE);
private static final Optional EMPTY_STRING = new Optional<>(Strings.EMPTY_STRING);
/** The Constant EMPTY. */
private static final Optional> EMPTY = new Optional<>();
/** The value. */
private final T value;
/**
* Instantiates a new optional.
*/
private Optional() {
value = null;
}
/**
* Instantiates a new optional.
*
* @param value
* @throws NullPointerException if {@code value} is {@code null}
*/
private Optional(final T value) throws NullPointerException {
this.value = Objects.requireNonNull(value);
}
/**
*
* @param
* @return
*/
public static Optional empty() {
return (Optional) EMPTY;
}
/**
*
* @param value
* @return
* @throws NullPointerException if {@code value} is {@code null}
*/
public static Optional of(final String value) throws NullPointerException {
Objects.requireNonNull(value);
if (value.isEmpty()) {
return EMPTY_STRING;
}
return new Optional<>(value);
}
/**
*
* @param
* @param value
* @return
* @throws NullPointerException if {@code value} is {@code null}
*/
public static Optional of(final T value) throws NullPointerException {
return new Optional<>(value);
}
/**
*
* @param value
* @return
*/
public static Optional ofNullable(final String value) {
if (value == null) {
return empty();
} else if (value.isEmpty()) {
return EMPTY_STRING;
}
return new Optional<>(value);
}
/**
*
* @param
* @param value
* @return
*/
public static Optional ofNullable(final T value) {
if (value == null) {
return empty();
}
return new Optional<>(value);
}
/**
*
* @param
* @param op
* @return
*/
public static Optional from(final java.util.Optional op) {
if (op == null || op.isEmpty()) {
return empty();
} else {
return of(op.get());
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public T get() throws NoSuchElementException {
return orElseThrow();
}
/**
* Checks if is present.
*
* @return {@code true}, if is present
*/
public boolean isPresent() {
return value != null;
}
/**
* Checks if is empty.
*
* @return {@code true}, if is empty
*/
public boolean isEmpty() {
return value == null;
}
/**
*
* @param
* @param action
* @return itself
* @throws IllegalArgumentException
* @throws E the e
*/
public Optional ifPresent(final Throwables.Consumer super T, E> action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent()) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return itself
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public Optional ifPresentOrElse(final Throwables.Consumer super T, E> action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent()) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Optional filter(final Throwables.Predicate super T, E> predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent() && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Nullable map(final Throwables.Function super T, ? extends U, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Optional mapToNonNull(final Throwables.Function super T, ? extends U, E> mapper)
throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return Optional.of(mapper.apply(value));
} else {
return Optional.empty();
}
}
/**
* Map to boolean.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean mapToBoolean(final Throwables.ToBooleanFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalBoolean.of(mapper.applyAsBoolean(value));
} else {
return OptionalBoolean.empty();
}
}
/**
* Map to char.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar mapToChar(final Throwables.ToCharFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalChar.of(mapper.applyAsChar(value));
} else {
return OptionalChar.empty();
}
}
/**
* Map to byte.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalByte mapToByte(final Throwables.ToByteFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalByte.of(mapper.applyAsByte(value));
} else {
return OptionalByte.empty();
}
}
/**
* Map to short.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalShort mapToShort(final Throwables.ToShortFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalShort.of(mapper.applyAsShort(value));
} else {
return OptionalShort.empty();
}
}
/**
* Map to int.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt mapToInt(final Throwables.ToIntFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
/**
* Map to long.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalLong mapToLong(final Throwables.ToLongFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return OptionalLong.empty();
}
}
/**
* Map to float.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalFloat mapToFloat(final Throwables.ToFloatFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalFloat.of(mapper.applyAsFloat(value));
} else {
return OptionalFloat.empty();
}
}
/**
* Map to double.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalDouble mapToDouble(final Throwables.ToDoubleFunction super T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Optional flatMap(final Throwables.Function super T, Optional, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
/**
*
* @param valueToFind
* @return
*/
public boolean contains(final T valueToFind) {
return isPresent() && N.equals(value, valueToFind);
}
/**
*
* @param supplier
* @return
* @throws IllegalArgumentException
*/
public Optional or(final Supplier> supplier) throws IllegalArgumentException {
N.checkArgNotNull(supplier, cs.Supplier);
if (isPresent()) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
// /**
// *
// * @return
// * @deprecated using {@link #orElseNull()}
// */
// @Deprecated
// public T orNull() {
// return isPresent() ? value : null;
// }
@Beta
public T orElseNull() {
return isPresent() ? value : null;
}
/**
*
* @param other
* @return
*/
public T orElse(final T other) {
return isPresent() ? value : other;
}
/**
* Or else get.
*
* @param other
* @return
*/
public T orElseGet(final Supplier extends T> other) {
if (isPresent()) {
return value;
} else {
return other.get();
}
}
// public T orElseNull() {
// return isPresent() ? value : null;
// }
/**
* Or else throw.
*
* @return
* @throws NoSuchElementException the no such element exception
*/
public T orElseThrow() throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(InternalUtil.ERROR_MSG_FOR_NO_SUCH_EX);
}
}
/**
* Or else throw.
* @param errorMessage
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public T orElseThrow(final String errorMessage) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(errorMessage);
}
}
/**
* Or else throw.
* @param errorMessage
* @param param
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public T orElseThrow(final String errorMessage, final Object param) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public T orElseThrow(final String errorMessage, final Object param1, final Object param2) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
* @param param3
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public T orElseThrow(final String errorMessage, final Object param1, final Object param2, final Object param3) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2, param3));
}
}
/**
* Or else throw.
* @param errorMessage
* @param params
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public T orElseThrow(final String errorMessage, final Object... params) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, params));
}
}
/**
* Or else throw.
*
* @param
* @param exceptionSupplier
* @return
* @throws E
*/
public T orElseThrow(final Supplier extends E> exceptionSupplier) throws E {
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 N.newHashSet();
}
}
/**
* To immutable list.
*
* @return
*/
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList.empty();
}
}
/**
* To immutable set.
*
* @return
*/
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet.empty();
}
}
@Beta
public Nullable toNullable() {
if (isPresent()) {
return Nullable.of(value);
} else {
return Nullable.empty();
}
}
public java.util.Optional toJdkOptional() {
if (isPresent()) {
return java.util.Optional.of(value);
} else {
return java.util.Optional.empty();
}
}
/**
*
* @return
* @deprecated to be removed in future version.
*/
@Deprecated
public java.util.Optional __() {//NOSONAR
return toJdkOptional();
}
/**
*
* @param obj
* @return
*/
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof final Optional> other) {
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";
}
}
/**
* The Class OptionalBoolean.
*/
@com.landawn.abacus.annotation.Immutable
public static final class OptionalBoolean implements Comparable, Immutable {
/** Presents {@code true}. */
public static final OptionalBoolean TRUE = new OptionalBoolean(true);
/** Presents {@code true}. */
public static final OptionalBoolean FALSE = new OptionalBoolean(false);
/** The Constant EMPTY. */
private static final OptionalBoolean EMPTY = new OptionalBoolean();
/** The value. */
private final boolean value;
/** The is present. */
private final boolean isPresent;
/**
* Instantiates a new optional boolean.
*/
private OptionalBoolean() {
value = false;
isPresent = false;
}
/**
* Instantiates a new optional boolean.
*
* @param value
*/
private OptionalBoolean(final boolean value) {
this.value = value;
isPresent = true;
}
public static OptionalBoolean empty() {
return EMPTY;
}
/**
*
* @param value
* @return
*/
public static OptionalBoolean of(final boolean value) {
return value ? TRUE : FALSE;
}
/**
*
* @param val
* @return
*/
public static OptionalBoolean ofNullable(final Boolean val) {
if (val == null) {
return empty();
} else {
return of(val);
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public boolean get() throws NoSuchElementException { // NOSONAR
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
/**
*
* @param
* @param action
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean ifPresent(final Throwables.BooleanConsumer action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public OptionalBoolean ifPresentOrElse(final Throwables.BooleanConsumer action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean filter(final Throwables.BooleanPredicate predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean map(final Throwables.BooleanUnaryOperator mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalBoolean.of(mapper.applyAsBoolean(value));
} else {
return empty();
}
}
/**
* Map to char.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar mapToChar(final Throwables.ToCharFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalChar.of(mapper.applyAsChar(value));
} else {
return OptionalChar.empty();
}
}
/**
* Map to int.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt mapToInt(final Throwables.ToIntFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
/**
* Map to Long.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalLong mapToLong(final Throwables.ToLongFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalLong.of(mapper.applyAsLong(value));
} else {
return OptionalLong.empty();
}
}
/**
* Map to Double.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalDouble mapToDouble(final Throwables.ToDoubleFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalDouble.of(mapper.applyAsDouble(value));
} else {
return OptionalDouble.empty();
}
}
/**
* Map to obj.
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Nullable mapToObj(final Throwables.BooleanFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E
*/
public Optional mapToNonNull(final Throwables.BooleanFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Optional.of(mapper.apply(value));
} else {
return Optional.empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean flatMap(final Throwables.BooleanFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final boolean valueToFind) {
// return isPresent() && N.equals(this.value, valueToFind);
// }
//
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final Boolean valueToFind) {
// return valueToFind != null && isPresent() && N.equals(this.value, valueToFind.booleanValue());
// }
/**
*
* @param supplier
* @return
*/
public OptionalBoolean or(final Supplier supplier) {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
// /**
// *
// * @return
// * @deprecated use {@link #orElse(false)}
// */
// @Deprecated
// public boolean orFalse() {
// return isPresent ? value : false;
// }
// public boolean orElseFalse() {
// return isPresent ? value : false;
// }
// /**
// *
// * @return
// * @deprecated use {@link #orElse(true)}
// */
// @Deprecated
// public boolean orTrue() {
// return isPresent ? value : true;
// }
// public boolean orElseTrue() {
// return isPresent ? value : true;
// }
/**
*
* @param other
* @return
*/
public boolean orElse(final boolean other) {
return isPresent ? value : other;
}
/**
* Or else get.
*
* @param other
* @return
* @throws IllegalArgumentException
*/
public boolean orElseGet(final BooleanSupplier other) throws IllegalArgumentException {
N.checkArgNotNull(other, cs.other);
if (isPresent) {
return value;
} else {
return other.getAsBoolean();
}
}
/**
* Or else throw.
*
* @return
* @throws NoSuchElementException the no such element exception
*/
public boolean orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException(NO_VALUE_PRESENT);
}
}
/**
* Or else throw.
* @param errorMessage
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public boolean orElseThrow(final String errorMessage) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(errorMessage);
}
}
/**
* Or else throw.
* @param errorMessage
* @param param
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public boolean orElseThrow(final String errorMessage, final Object param) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public boolean orElseThrow(final String errorMessage, final Object param1, final Object param2) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
* @param param3
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public boolean orElseThrow(final String errorMessage, final Object param1, final Object param2, final Object param3) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2, param3));
}
}
/**
* Or else throw.
* @param errorMessage
* @param params
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public boolean orElseThrow(final String errorMessage, final Object... params) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, params));
}
}
/**
* Or else throw.
*
* @param
* @param exceptionSupplier
* @return
* @throws IllegalArgumentException
* @throws E
*/
public boolean orElseThrow(final Supplier extends E> exceptionSupplier) throws IllegalArgumentException, E {
N.checkArgNotNull(exceptionSupplier, cs.exceptionSupplier);
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 N.newHashSet();
}
}
/**
* To immutable list.
*
* @return
*/
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList.empty();
}
}
/**
* To immutable set.
*
* @return
*/
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional.empty();
}
}
/**
*
* @param optional
* @return
*/
@Override
public int compareTo(final OptionalBoolean optional) {
if (optional == null || !optional.isPresent) {
return isPresent ? 1 : 0;
}
if (!isPresent) {
return -1;
}
return Boolean.compare(get(), optional.get());
}
/**
*
* @param obj
* @return
*/
@SuppressFBWarnings
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof final OptionalBoolean other) {
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";
}
}
/**
* The Class OptionalChar.
*/
@com.landawn.abacus.annotation.Immutable
public static final class OptionalChar implements Comparable, Immutable {
/** The Constant EMPTY. */
private static final OptionalChar EMPTY = new OptionalChar();
/** The Constant MIN_CACHED_VALUE. */
private static final char MIN_CACHED_VALUE = 0;
/** The Constant MAX_CACHED_VALUE. */
private static final char MAX_CACHED_VALUE = 128;
/** The Constant cached. */
private static final OptionalChar[] cached = new OptionalChar[MAX_CACHED_VALUE - MIN_CACHED_VALUE + 1];
static {
for (int i = 0; i < cached.length; i++) {
cached[i] = new OptionalChar((char) (i + MIN_CACHED_VALUE));
}
}
/** The value. */
private final char value;
/** The is present. */
private final boolean isPresent;
/**
* Instantiates a new optional char.
*/
private OptionalChar() {
value = 0;
isPresent = false;
}
/**
* Instantiates a new optional char.
*
* @param value
*/
private OptionalChar(final char value) {
this.value = value;
isPresent = true;
}
public static OptionalChar empty() {
return EMPTY;
}
/**
*
* @param value
* @return
*/
public static OptionalChar of(final char value) {
//noinspection ConstantValue
return value >= MIN_CACHED_VALUE && value <= MAX_CACHED_VALUE ? cached[value - MIN_CACHED_VALUE] : new OptionalChar(value);
}
/**
*
* @param val
* @return
*/
public static OptionalChar ofNullable(final Character val) {
if (val == null) {
return empty();
} else {
return of(val);
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public char get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
/**
*
* @param
* @param action
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar ifPresent(final Throwables.CharConsumer action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent()) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public OptionalChar ifPresentOrElse(final Throwables.CharConsumer action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent()) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar filter(final Throwables.CharPredicate predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent() && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar map(final Throwables.CharUnaryOperator mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return OptionalChar.of(mapper.applyAsChar(value));
} else {
return empty();
}
}
/**
* Map to boolean.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalBoolean mapToBoolean(final Throwables.ToBooleanFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalBoolean.of(mapper.applyAsBoolean(value));
} else {
return OptionalBoolean.empty();
}
}
/**
* Map to int.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt mapToInt(final Throwables.ToIntFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
/**
* Map to obj.
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Nullable mapToObj(final Throwables.CharFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E
*/
public Optional mapToNonNull(final Throwables.CharFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Optional.of(mapper.apply(value));
} else {
return Optional.empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalChar flatMap(final Throwables.CharFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent()) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final char valueToFind) {
// return isPresent() && N.equals(this.value, valueToFind);
// }
//
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final Character valueToFind) {
// return valueToFind != null && isPresent() && N.equals(this.value, valueToFind.charValue());
// }
/**
*
* @param supplier
* @return
*/
public OptionalChar or(final Supplier supplier) {
if (isPresent()) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
// /**
// *
// * @return
// * @deprecated use {@link #orElseZero()}
// */
// @Deprecated
// public char orZero() {
// return isPresent() ? value : 0;
// }
public char orElseZero() {
return isPresent() ? value : 0;
}
/**
*
* @param other
* @return
*/
public char orElse(final char other) {
return isPresent() ? value : other;
}
/**
* Or else get.
*
* @param other
* @return
* @throws IllegalArgumentException
*/
public char orElseGet(final CharSupplier other) throws IllegalArgumentException {
N.checkArgNotNull(other, cs.other);
if (isPresent()) {
return value;
} else {
return other.getAsChar();
}
}
/**
* Or else throw.
*
* @return
* @throws NoSuchElementException the no such element exception
*/
public char orElseThrow() throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(NO_VALUE_PRESENT);
}
}
/**
* Or else throw.
* @param errorMessage
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public char orElseThrow(final String errorMessage) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(errorMessage);
}
}
/**
* Or else throw.
* @param errorMessage
* @param param
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public char orElseThrow(final String errorMessage, final Object param) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public char orElseThrow(final String errorMessage, final Object param1, final Object param2) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
* @param param3
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public char orElseThrow(final String errorMessage, final Object param1, final Object param2, final Object param3) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2, param3));
}
}
/**
* Or else throw.
* @param errorMessage
* @param params
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public char orElseThrow(final String errorMessage, final Object... params) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, params));
}
}
/**
* Or else throw.
*
* @param
* @param exceptionSupplier
* @return
* @throws IllegalArgumentException
* @throws E
*/
public char orElseThrow(final Supplier extends E> exceptionSupplier) throws IllegalArgumentException, E {
N.checkArgNotNull(exceptionSupplier, cs.exceptionSupplier);
if (isPresent()) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public CharStream stream() {
if (isPresent) {
return CharStream.of(value);
} else {
return CharStream.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 N.newHashSet();
}
}
/**
* To immutable list.
*
* @return
*/
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList.empty();
}
}
/**
* To immutable set.
*
* @return
*/
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet.empty();
}
}
public Optional boxed() {
if (isPresent()) {
return Optional.of(value);
} else {
return Optional.empty();
}
}
/**
*
* @param optional
* @return
*/
@Override
public int compareTo(final OptionalChar optional) {
if (optional == null || !optional.isPresent()) {
return isPresent ? 1 : 0;
}
if (!isPresent) {
return -1;
}
return Character.compare(get(), optional.get());
}
/**
*
* @param obj
* @return
*/
@SuppressFBWarnings
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof final OptionalChar other) {
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";
}
}
/**
* The Class OptionalByte.
*/
@com.landawn.abacus.annotation.Immutable
public static final class OptionalByte implements Comparable, Immutable {
/** The Constant EMPTY. */
private static final OptionalByte EMPTY = new OptionalByte();
/** The Constant MIN_CACHED_VALUE. */
private static final byte MIN_CACHED_VALUE = Byte.MIN_VALUE;
/** The Constant MAX_CACHED_VALUE. */
private static final byte MAX_CACHED_VALUE = Byte.MAX_VALUE;
/** The Constant cached. */
private static final OptionalByte[] cached = new OptionalByte[MAX_CACHED_VALUE - MIN_CACHED_VALUE + 1];
static {
for (int i = 0; i < cached.length; i++) {
cached[i] = new OptionalByte((byte) (i + MIN_CACHED_VALUE));
}
}
/** The value. */
private final byte value;
/** The is present. */
private final boolean isPresent;
/**
* Instantiates a new optional byte.
*/
private OptionalByte() {
value = 0;
isPresent = false;
}
/**
* Instantiates a new optional byte.
*
* @param value
*/
private OptionalByte(final byte value) {
this.value = value;
isPresent = true;
}
public static OptionalByte empty() {
return EMPTY;
}
/**
*
* @param value
* @return
*/
public static OptionalByte of(final byte value) {
return cached[value - MIN_CACHED_VALUE];
}
/**
*
* @param val
* @return
*/
public static OptionalByte ofNullable(final Byte val) {
if (val == null) {
return empty();
} else {
return OptionalByte.of(val);
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public byte get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
/**
*
* @param
* @param action
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalByte ifPresent(final Throwables.ByteConsumer action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public OptionalByte ifPresentOrElse(final Throwables.ByteConsumer action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalByte filter(final Throwables.BytePredicate predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalByte map(final Throwables.ByteUnaryOperator mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalByte.of(mapper.applyAsByte(value));
} else {
return empty();
}
}
/**
* Map to int.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt mapToInt(final Throwables.ToIntFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
/**
* Map to obj.
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Nullable mapToObj(final Throwables.ByteFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E
*/
public Optional mapToNonNull(final Throwables.ByteFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Optional.of(mapper.apply(value));
} else {
return Optional.empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalByte flatMap(final Throwables.ByteFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final byte valueToFind) {
// return isPresent() && N.equals(this.value, valueToFind);
// }
//
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final Byte valueToFind) {
// return valueToFind != null && isPresent() && N.equals(this.value, valueToFind.byteValue());
// }
/**
*
* @param supplier
* @return
*/
public OptionalByte or(final Supplier supplier) {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
// /**
// *
// * @return
// * @deprecated use {@link #orElseZero()}
// */
// @Deprecated
// public byte orZero() {
// return isPresent ? value : 0;
// }
public byte orElseZero() {
return isPresent ? value : 0;
}
/**
*
* @param other
* @return
*/
public byte orElse(final byte other) {
return isPresent ? value : other;
}
/**
* Or else get.
*
* @param other
* @return
* @throws IllegalArgumentException
*/
public byte orElseGet(final ByteSupplier other) throws IllegalArgumentException {
N.checkArgNotNull(other, cs.other);
if (isPresent) {
return value;
} else {
return other.getAsByte();
}
}
/**
* Or else throw.
*
* @return
* @throws NoSuchElementException the no such element exception
*/
public byte orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException(NO_VALUE_PRESENT);
}
}
/**
* Or else throw.
* @param errorMessage
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public byte orElseThrow(final String errorMessage) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(errorMessage);
}
}
/**
* Or else throw.
* @param errorMessage
* @param param
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public byte orElseThrow(final String errorMessage, final Object param) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public byte orElseThrow(final String errorMessage, final Object param1, final Object param2) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
* @param param3
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public byte orElseThrow(final String errorMessage, final Object param1, final Object param2, final Object param3) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2, param3));
}
}
/**
* Or else throw.
* @param errorMessage
* @param params
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public byte orElseThrow(final String errorMessage, final Object... params) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, params));
}
}
/**
* Or else throw.
*
* @param
* @param exceptionSupplier
* @return
* @throws IllegalArgumentException
* @throws E
*/
public byte orElseThrow(final Supplier extends E> exceptionSupplier) throws IllegalArgumentException, E {
N.checkArgNotNull(exceptionSupplier, cs.exceptionSupplier);
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public ByteStream stream() {
if (isPresent) {
return ByteStream.of(value);
} else {
return ByteStream.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 N.newHashSet();
}
}
/**
* To immutable list.
*
* @return
*/
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList.empty();
}
}
/**
* To immutable set.
*
* @return
*/
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional.empty();
}
}
/**
*
* @param optional
* @return
*/
@Override
public int compareTo(final OptionalByte optional) {
if (optional == null || !optional.isPresent) {
return isPresent ? 1 : 0;
}
if (!isPresent) {
return -1;
}
return Byte.compare(get(), optional.get());
}
/**
*
* @param obj
* @return
*/
@SuppressFBWarnings
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof final OptionalByte other) {
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";
}
}
/**
* The Class OptionalShort.
*/
@com.landawn.abacus.annotation.Immutable
public static final class OptionalShort implements Comparable, Immutable {
/** The Constant EMPTY. */
private static final OptionalShort EMPTY = new OptionalShort();
/** The Constant MIN_CACHED_VALUE. */
private static final short MIN_CACHED_VALUE = -128;
/** The Constant MAX_CACHED_VALUE. */
private static final short MAX_CACHED_VALUE = 256;
/** The Constant cached. */
private static final OptionalShort[] cached = new OptionalShort[MAX_CACHED_VALUE - MIN_CACHED_VALUE + 1];
static {
for (int i = 0; i < cached.length; i++) {
cached[i] = new OptionalShort((short) (i + MIN_CACHED_VALUE));
}
}
/** The value. */
private final short value;
/** The is present. */
private final boolean isPresent;
/**
* Instantiates a new optional short.
*/
private OptionalShort() {
value = 0;
isPresent = false;
}
/**
* Instantiates a new optional short.
*
* @param value
*/
private OptionalShort(final short value) {
this.value = value;
isPresent = true;
}
public static OptionalShort empty() {
return EMPTY;
}
/**
*
* @param value
* @return
*/
public static OptionalShort of(final short value) {
return value >= MIN_CACHED_VALUE && value <= MAX_CACHED_VALUE ? cached[value - MIN_CACHED_VALUE] : new OptionalShort(value);
}
/**
*
* @param val
* @return
*/
public static OptionalShort ofNullable(final Short val) {
if (val == null) {
return empty();
} else {
return OptionalShort.of(val);
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public short get() throws NoSuchElementException {
return orElseThrow();
}
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
/**
*
* @param
* @param action
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalShort ifPresent(final Throwables.ShortConsumer action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public OptionalShort ifPresentOrElse(final Throwables.ShortConsumer action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalShort filter(final Throwables.ShortPredicate predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalShort map(final Throwables.ShortUnaryOperator mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalShort.of(mapper.applyAsShort(value));
} else {
return empty();
}
}
/**
* Map to int.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt mapToInt(final Throwables.ToIntFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return OptionalInt.empty();
}
}
/**
* Map to obj.
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public Nullable mapToObj(final Throwables.ShortFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Nullable.of(mapper.apply(value));
} else {
return Nullable.empty();
}
}
/**
*
* @param
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E
*/
public Optional mapToNonNull(final Throwables.ShortFunction extends T, E> mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Optional.of(mapper.apply(value));
} else {
return Optional.empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalShort flatMap(final Throwables.ShortFunction mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return Objects.requireNonNull(mapper.apply(value));
} else {
return empty();
}
}
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final short valueToFind) {
// return isPresent() && N.equals(this.value, valueToFind);
// }
//
// /**
// *
// *
// * @param element
// * @return
// */
// public boolean contains(final Short valueToFind) {
// return valueToFind != null && isPresent() && N.equals(this.value, valueToFind.shortValue());
// }
/**
*
* @param supplier
* @return
*/
public OptionalShort or(final Supplier supplier) {
if (isPresent) {
return this;
} else {
return Objects.requireNonNull(supplier.get());
}
}
// /**
// *
// * @return
// * @deprecated use {@link #orElseZero()}
// */
// @Deprecated
// public short orZero() {
// return isPresent ? value : 0;
// }
public short orElseZero() {
return isPresent ? value : 0;
}
/**
*
* @param other
* @return
*/
public short orElse(final short other) {
return isPresent ? value : other;
}
/**
* Or else get.
*
* @param other
* @return
* @throws IllegalArgumentException
*/
public short orElseGet(final ShortSupplier other) throws IllegalArgumentException {
N.checkArgNotNull(other, cs.other);
if (isPresent) {
return value;
} else {
return other.getAsShort();
}
}
/**
* Or else throw.
*
* @return
* @throws NoSuchElementException the no such element exception
*/
public short orElseThrow() throws NoSuchElementException {
if (isPresent) {
return value;
} else {
throw new NoSuchElementException(NO_VALUE_PRESENT);
}
}
/**
* Or else throw.
* @param errorMessage
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public short orElseThrow(final String errorMessage) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(errorMessage);
}
}
/**
* Or else throw.
* @param errorMessage
* @param param
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public short orElseThrow(final String errorMessage, final Object param) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public short orElseThrow(final String errorMessage, final Object param1, final Object param2) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2));
}
}
/**
* Or else throw.
* @param errorMessage
* @param param1
* @param param2
* @param param3
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public short orElseThrow(final String errorMessage, final Object param1, final Object param2, final Object param3) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, param1, param2, param3));
}
}
/**
* Or else throw.
* @param errorMessage
* @param params
*
* @return
* @throws NoSuchElementException the no such element exception
*/
@Beta
public short orElseThrow(final String errorMessage, final Object... params) throws NoSuchElementException {
if (isPresent()) {
return value;
} else {
throw new NoSuchElementException(N.format(errorMessage, params));
}
}
/**
* Or else throw.
*
* @param
* @param exceptionSupplier
* @return
* @throws IllegalArgumentException
* @throws E
*/
public short orElseThrow(final Supplier extends E> exceptionSupplier) throws IllegalArgumentException, E {
N.checkArgNotNull(exceptionSupplier, cs.exceptionSupplier);
if (isPresent) {
return value;
} else {
throw exceptionSupplier.get();
}
}
public ShortStream stream() {
if (isPresent) {
return ShortStream.of(value);
} else {
return ShortStream.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 N.newHashSet();
}
}
/**
* To immutable list.
*
* @return
*/
public ImmutableList toImmutableList() {
if (isPresent()) {
return ImmutableList.of(value);
} else {
return ImmutableList.empty();
}
}
/**
* To immutable set.
*
* @return
*/
public ImmutableSet toImmutableSet() {
if (isPresent()) {
return ImmutableSet.of(value);
} else {
return ImmutableSet.empty();
}
}
public Optional boxed() {
if (isPresent) {
return Optional.of(value);
} else {
return Optional.empty();
}
}
/**
*
* @param optional
* @return
*/
@Override
public int compareTo(final OptionalShort optional) {
if (optional == null || !optional.isPresent) {
return isPresent ? 1 : 0;
}
if (!isPresent) {
return -1;
}
return Short.compare(get(), optional.get());
}
/**
*
* @param obj
* @return
*/
@SuppressFBWarnings
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof final OptionalShort other) {
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";
}
}
/**
* The Class OptionalInt.
*/
@com.landawn.abacus.annotation.Immutable
public static final class OptionalInt implements Comparable, Immutable {
/** The Constant EMPTY. */
private static final OptionalInt EMPTY = new OptionalInt();
/** The Constant MIN_CACHED_VALUE. */
private static final int MIN_CACHED_VALUE = -256;
/** The Constant MAX_CACHED_VALUE. */
private static final int MAX_CACHED_VALUE = 1024;
/** The Constant cached. */
private static final OptionalInt[] cached = new OptionalInt[MAX_CACHED_VALUE - MIN_CACHED_VALUE + 1];
static {
for (int i = 0; i < cached.length; i++) {
cached[i] = new OptionalInt(i + MIN_CACHED_VALUE);
}
}
/** The value. */
private final int value;
/** The is present. */
private final boolean isPresent;
/**
* Instantiates a new optional int.
*/
private OptionalInt() {
value = 0;
isPresent = false;
}
/**
* Instantiates a new optional int.
*
* @param value
*/
private OptionalInt(final int value) {
this.value = value;
isPresent = true;
}
public static OptionalInt empty() {
return EMPTY;
}
/**
*
* @param value
* @return
*/
public static OptionalInt of(final int value) {
return value >= MIN_CACHED_VALUE && value <= MAX_CACHED_VALUE ? cached[value - MIN_CACHED_VALUE] : new OptionalInt(value);
}
/**
*
* @param val
* @return
*/
public static OptionalInt ofNullable(final Integer val) {
if (val == null) {
return empty();
} else {
return OptionalInt.of(val);
}
}
/**
*
* @param op
* @return
*/
public static OptionalInt from(final java.util.OptionalInt op) {
if (op.isPresent()) {
return of(op.getAsInt());
} else {
return empty();
}
}
/**
* Returns the value if present, otherwise throws {@code NoSuchElementException}.
*
* @return the value if present
* @throws NoSuchElementException if no value is present
*/
public int get() throws NoSuchElementException {
return orElseThrow();
}
// public int getAsInt() throws NoSuchElementException {
// return orElseThrow();
// }
public boolean isPresent() {
return isPresent;
}
public boolean isEmpty() {
return !isPresent;
}
/**
*
* @param
* @param action
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt ifPresent(final Throwables.IntConsumer action) throws IllegalArgumentException, E {
N.checkArgNotNull(action, cs.action);
if (isPresent) {
action.accept(value);
}
return this;
}
/**
* If present or else.
*
* @param
* @param
* @param action
* @param emptyAction
* @return
* @throws IllegalArgumentException
* @throws E the e
* @throws E2 the e2
*/
public OptionalInt ifPresentOrElse(final Throwables.IntConsumer action,
final Throwables.Runnable emptyAction) throws IllegalArgumentException, E, E2 {
N.checkArgNotNull(action, cs.action);
N.checkArgNotNull(emptyAction, cs.emptyAction);
if (isPresent) {
action.accept(value);
} else {
emptyAction.run();
}
return this;
}
/**
*
* @param
* @param predicate
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt filter(final Throwables.IntPredicate predicate) throws IllegalArgumentException, E {
N.checkArgNotNull(predicate, cs.Predicate);
if (isPresent && predicate.test(value)) {
return this;
} else {
return empty();
}
}
/**
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public OptionalInt map(final Throwables.IntUnaryOperator mapper) throws IllegalArgumentException, E {
N.checkArgNotNull(mapper, cs.mapper);
if (isPresent) {
return OptionalInt.of(mapper.applyAsInt(value));
} else {
return empty();
}
}
/**
* Map to boolean.
*
* @param
* @param mapper
* @return
* @throws IllegalArgumentException
* @throws E the e
*/
public