All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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.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 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 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 predicate) throws E {
            N.checkArgNotNull(predicate, "predicate");

            if (isPresent() && predicate.test(value)) {
                return this;
            } else {
                return empty();
            }
        }

        public  Nullable map(final Try.Function 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 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 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 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 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 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  OptionalFloat mapToFloat(final Try.ToFloatFunction 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 mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isPresent()) {
                return OptionalDouble.of(mapper.applyAsDouble(value));
            } else {
                return OptionalDouble.empty();
            }
        }

        public  Optional flatMap(Try.Function, 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 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 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 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 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 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 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 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 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 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 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 boxed() {
            if (isPresent) {
                return Optional.of(value);
            } else {
                return Optional. empty();
            }
        }

        public java.util.OptionalDouble __() {
            if (isPresent) {
                return java.util.OptionalDouble.of(value);
            } else {
                return java.util.OptionalDouble.empty();
            }
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }

            if (obj instanceof OptionalDouble) {
                final OptionalDouble other = (OptionalDouble) 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("OptionalDouble[%s]", value);
            }

            return "OptionalDouble.empty";
        }
    }

    public static final class Nullable {
        private static final Nullable EMPTY = new Nullable<>();

        private final T value;
        private final boolean isPresent;

        private Nullable() {
            this.value = null;
            this.isPresent = false;
        }

        private Nullable(T value) {
            this.value = value;
            this.isPresent = true;
        }

        public static  Nullable empty() {
            return (Nullable) EMPTY;
        }

        public static  Nullable of(T value) {
            return new Nullable<>(value);
        }

        public static  Nullable from(Optional optional) {
            if (optional.isPresent()) {
                return new Nullable<>(optional.get());
            } else {
                return Nullable. empty();
            }
        }

        public static  Nullable from(java.util.Optional optional) {
            if (optional.isPresent()) {
                return new Nullable<>(optional.get());
            } else {
                return Nullable. empty();
            }
        }

        public T get() throws NoSuchElementException {
            return orElseThrow();
        }

        /**
         * Returns {@code true} if the value is present, otherwise returns {@code false}.
         * 
         * @return
         */
        public boolean isPresent() {
            return isPresent;
        }

        /**
         * Returns {@code true} if the value is not present, otherwise returns {@code false}.
         * 
         * @return
         */
        public boolean isNotPresent() {
            return isPresent == false;
        }

        /**
         * Returns {@code true} if the value is not present, otherwise returns {@code false}.
         * 
         * @return
         * @deprecated replaced by {@link #isNotPresent()}
         */
        @Deprecated
        public boolean isEmpty() {
            return isPresent == false;
        }

        /**
         * Returns {@code true} if the value is not present, or it is present but it's {@code null}, otherwise returns {@code false}.
         * 
         * @return
         */
        public boolean isNull() {
            return value == null;
        }

        /**
         * Returns {@code true} if the value is present and it's not {@code null}, otherwise returns {@code false}.
         * 
         * @return
         */
        public boolean isNotNull() {
            return value != null;
        }

        /**
         * 
         * @param action
         * @return itself
         * @throws E
         */
        public  Nullable ifPresent(Try.Consumer 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  Nullable ifPresentOrElse(Try.Consumer 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;
        }

        /**
         * 
         * @param action
         * @return itself
         * @throws E
         */
        public  Nullable ifNotNull(Try.Consumer action) throws E {
            N.checkArgNotNull(action, "action");

            if (isNotNull()) {
                action.accept(value);
            }

            return this;
        }

        /**
         * 
         * @param action
         * @param emptyAction
         * @return itself
         * @throws E
         * @throws E2
         */
        public  Nullable ifNotNullOrElse(Try.Consumer action, Try.Runnable emptyAction)
                throws E, E2 {
            N.checkArgNotNull(action, "action");
            N.checkArgNotNull(emptyAction, "emptyAction");

            if (isNotNull()) {
                action.accept(value);
            } else {
                emptyAction.run();
            }

            return this;
        }

        public  Nullable filter(Try.Predicate predicate) throws E {
            N.checkArgNotNull(predicate, "predicate");

            if (isPresent() && predicate.test(value)) {
                return this;
            } else {
                return empty();
            }
        }

        public  Optional filterIfNotNull(Try.Predicate predicate) throws E {
            N.checkArgNotNull(predicate, "predicate");

            if (isNotNull() && predicate.test(value)) {
                return Optional.of(value);
            } else {
                return Optional.empty();
            }
        }

        public  Nullable map(Try.Function mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isPresent()) {
                return Nullable.of((U) mapper.apply(value));
            } else {
                return empty();
            }
        }

        public  OptionalBoolean mapToBoolean(final Try.ToBooleanFunction 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 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 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 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 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  OptionalFloat mapToFloat(final Try.ToFloatFunction 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 mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isPresent()) {
                return OptionalDouble.of(mapper.applyAsDouble(value));
            } else {
                return OptionalDouble.empty();
            }
        }

        public  Nullable mapIfNotNull(Try.Function mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return Nullable.of((U) mapper.apply(value));
            } else {
                return empty();
            }
        }

        public  OptionalBoolean mapToBooleanIfNotNull(final Try.ToBooleanFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalBoolean.of(mapper.applyAsBoolean(value));
            } else {
                return OptionalBoolean.empty();
            }
        }

        public  OptionalChar mapToCharIfNotNull(final Try.ToCharFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalChar.of(mapper.applyAsChar(value));
            } else {
                return OptionalChar.empty();
            }
        }

        public  OptionalByte mapToByteIfNotNull(final Try.ToByteFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalByte.of(mapper.applyAsByte(value));
            } else {
                return OptionalByte.empty();
            }
        }

        public  OptionalShort mapToShortIfNotNull(final Try.ToShortFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalShort.of(mapper.applyAsShort(value));
            } else {
                return OptionalShort.empty();
            }
        }

        public  OptionalInt mapToIntIfNotNull(final Try.ToIntFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalInt.of(mapper.applyAsInt(value));
            } else {
                return OptionalInt.empty();
            }
        }

        public  OptionalLong mapToLongIfNotNull(final Try.ToLongFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalLong.of(mapper.applyAsLong(value));
            } else {
                return OptionalLong.empty();
            }
        }

        public  OptionalFloat mapToFloatIfNotNull(final Try.ToFloatFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalFloat.of(mapper.applyAsFloat(value));
            } else {
                return OptionalFloat.empty();
            }
        }

        public  OptionalDouble mapToDoubleIfNotNull(final Try.ToDoubleFunction mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return OptionalDouble.of(mapper.applyAsDouble(value));
            } else {
                return OptionalDouble.empty();
            }
        }

        public  Nullable flatMap(Try.Function, E> mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isPresent()) {
                return Objects.requireNonNull(mapper.apply(value));
            } else {
                return empty();
            }
        }

        public  Nullable flatMapIfNotNull(Try.Function, E> mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return Objects.requireNonNull(mapper.apply(value));
            } else {
                return empty();
            }
        }

        public  Nullable or(Try.Supplier, E> supplier) throws E {
            N.checkArgNotNull(supplier, "supplier");

            if (isPresent()) {
                return this;
            } else {
                return Objects.requireNonNull((Nullable) supplier.get());
            }
        }

        public  Nullable orIfNull(Try.Supplier, E> supplier) throws E {
            N.checkArgNotNull(supplier, "supplier");

            if (isNotNull()) {
                return this;
            } else {
                return Objects.requireNonNull((Nullable) supplier.get());
            }
        }

        public T orNull() {
            return isPresent() ? value : null;
        }

        //    public T orElseNull() {
        //        return isPresent() ? value : null;
        //    }

        public T orElse(T other) {
            return isPresent() ? value : other;
        }

        public  T orElseGet(Try.Supplier other) throws E {
            N.checkArgNotNull(other, "other");

            if (isPresent()) {
                return value;
            } else {
                return other.get();
            }
        }

        public T orElseThrow() throws NoSuchElementException {
            if (isPresent()) {
                return value;
            } else {
                throw new NoSuchElementException("No value is present");
            }
        }

        public  T orElseThrow(Supplier exceptionSupplier) throws X {
            N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");

            if (isPresent()) {
                return value;
            } else {
                throw exceptionSupplier.get();
            }
        }

        public T orElseIfNull(T other) {
            return isNotNull() ? value : other;
        }

        public  T orElseGetIfNull(Try.Supplier other) throws E {
            N.checkArgNotNull(other, "other");

            if (isNotNull()) {
                return value;
            } else {
                return other.get();
            }
        }

        public T orElseThrowIfNull() throws NoSuchElementException {
            if (isNotNull()) {
                return value;
            } else {
                throw new NoSuchElementException("No value is present");
            }
        }

        public  T orElseThrowIfNull(Supplier exceptionSupplier) throws X {
            N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");

            if (isNotNull()) {
                return value;
            } else {
                throw exceptionSupplier.get();
            }
        }

        public Stream stream() {
            if (isPresent()) {
                return Stream.of(value);
            } else {
                return Stream. empty();
            }
        }

        public Stream streamIfNotNull() {
            if (isNotNull()) {
                return Stream.of(value);
            } else {
                return Stream. empty();
            }
        }

        public List toList() {
            if (isPresent()) {
                return N.asList(value);
            } else {
                return new ArrayList<>();
            }
        }

        public List toListIfNotNull() {
            if (isNotNull()) {
                return N.asList(value);
            } else {
                return new ArrayList<>();
            }
        }

        public Set toSet() {
            if (isPresent()) {
                return N.asSet(value);
            } else {
                return new HashSet<>();
            }
        }

        public Set toSetIfNotNull() {
            if (isNotNull()) {
                return N.asSet(value);
            } else {
                return new HashSet<>();
            }
        }

        public ImmutableList toImmutableList() {
            if (isPresent()) {
                return ImmutableList.of(value);
            } else {
                return ImmutableList.empty();
            }
        }

        public ImmutableList toImmutableListIfNotNull() {
            if (isNotNull()) {
                return ImmutableList.of(value);
            } else {
                return ImmutableList.empty();
            }
        }

        public ImmutableSet toImmutableSet() {
            if (isPresent()) {
                return ImmutableSet.of(value);
            } else {
                return ImmutableSet.empty();
            }
        }

        public ImmutableSet toImmutableSetIfNotNull() {
            if (isNotNull()) {
                return ImmutableSet.of(value);
            } else {
                return ImmutableSet.empty();
            }
        }

        public Optional toOptional() {
            if (value == null) {
                return Optional. empty();
            } else {
                return Optional.of(value);
            }
        }

        public java.util.Optional toJdkOptional() {
            if (value == null) {
                return java.util.Optional. empty();
            } else {
                return java.util.Optional.of(value);
            }
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }

            if (obj instanceof Nullable) {
                final Nullable other = (Nullable) obj;

                return N.equals(isPresent, other.isPresent) && N.equals(value, other.value);
            }

            return false;
        }

        @Override
        public int hashCode() {
            return N.hashCode(isPresent) * 31 + N.hashCode(value);
        }

        @Override
        public String toString() {
            if (value == null) {
                return isPresent ? "Nullable[null]" : "Nullable.empty";
            } else {
                return String.format("Nullable[%s]", N.toString(value));
            }
        }
    }

    public static final class Holder extends Reference> {
        public Holder() {
            this(null);
        }

        Holder(T value) {
            super(value);
        }

        public static  Holder of(T value) {
            return new Holder<>(value);
        }
    }

    public static final class R extends Reference> {
        public R() {
            this(null);
        }

        R(T value) {
            super(value);
        }

        public static  R of(T value) {
            return new R<>(value);
        }
    }

    static abstract class Reference> {
        private T value;

        protected Reference() {
            this(null);
        }

        protected Reference(T value) {
            this.value = value;
        }

        public T value() {
            return value;
        }

        /**
         * 
         * @return
         * @deprecated replace by {@link #value()}.
         */
        @Deprecated
        public T getValue() {
            return value;
        }

        public void setValue(final T value) {
            this.value = value;
        }

        public T getAndSet(final T value) {
            final T result = this.value;
            this.value = value;
            return result;
        }

        public T setAndGet(final T value) {
            this.value = value;
            return this.value;
        }

        public final  T getAndUpdate(Try.UnaryOperator updateFunction) throws E {
            final T res = value;
            this.value = updateFunction.apply(value);
            return res;
        }

        public final  T updateAndGet(Try.UnaryOperator updateFunction) throws E {
            this.value = updateFunction.apply(value);
            return value;
        }

        /**
         * Set with the specified new value and returns true if predicate returns true.
         * Otherwise just return false without setting the value to new value.
         * 
         * @param newValue
         * @param predicate - test the current value.
         * @return
         */
        public  boolean setIf(final T newValue, final Try.Predicate predicate) throws E {
            if (predicate.test(value)) {
                this.value = newValue;
                return true;
            }

            return false;
        }

        /**
         * Set with the specified new value and returns true if predicate returns true.
         * Otherwise just return false without setting the value to new value.
         * 
         * @param newValue
         * @param predicate the first parameter is the current value, the second parameter is the new value.
         * @return
         */
        public  boolean setIf(final T newValue, final Try.BiPredicate predicate) throws E {
            if (predicate.test(value, newValue)) {
                this.value = newValue;
                return true;
            }

            return false;
        }

        public boolean isNull() {
            return value == null;
        }

        public boolean isNotNull() {
            return value != null;
        }

        public  void ifNotNull(Try.Consumer action) throws E {
            N.checkArgNotNull(action, "action");

            if (isNotNull()) {
                action.accept(value);
            }
        }

        public  void ifNotNullOrElse(Try.Consumer action, Try.Runnable emptyAction) throws E, E2 {
            N.checkArgNotNull(action, "action");
            N.checkArgNotNull(emptyAction, "emptyAction");

            if (isNotNull()) {
                action.accept(value);
            } else {
                emptyAction.run();
            }
        }

        public  void accept(final Try.Consumer action) throws E {
            action.accept(value);
        }

        @Deprecated
        public  void acceptIfNotNull(final Try.Consumer action) throws E {
            N.checkArgNotNull(action, "action");

            if (isNotNull()) {
                action.accept(value);
            }
        }

        public  U map(final Try.Function mapper) throws E {
            return mapper.apply(value);
        }

        public  Nullable mapIfNotNull(final Try.Function mapper) throws E {
            N.checkArgNotNull(mapper, "mapper");

            if (isNotNull()) {
                return Nullable.of((U) mapper.apply(value));
            } else {
                return Nullable. empty();
            }
        }

        public  Nullable filter(final Try.Predicate predicate) throws E {
            if (predicate.test(value)) {
                return Nullable.of(value);
            } else {
                return Nullable. empty();
            }
        }

        public  Optional filterIfNotNull(final Try.Predicate predicate) throws E {
            N.checkArgNotNull(predicate, "predicate");

            if (isNotNull() && predicate.test(value)) {
                return Optional.of(value);
            } else {
                return Optional. empty();
            }
        }

        public Stream stream() {
            return Stream.of(value);
        }

        public Stream streamIfNotNull() {
            if (isNotNull()) {
                return Stream.of(value);
            } else {
                return Stream. empty();
            }
        }

        public T orElseIfNull(T other) {
            return isNotNull() ? value : other;
        }

        public  T orElseGetIfNull(Try.Supplier other) throws E {
            N.checkArgNotNull(other, "other");

            if (isNotNull()) {
                return value;
            } else {
                return other.get();
            }
        }

        public  T orElseThrowIfNull(Supplier exceptionSupplier) throws X {
            N.checkArgNotNull(exceptionSupplier, "exceptionSupplier");

            if (isNotNull()) {
                return value;
            } else {
                throw exceptionSupplier.get();
            }
        }

        /**
         * Returns a non-empty {@code Nullable} with the {@code value}.
         * 
         * @return
         */
        public Nullable toNullable() {
            return Nullable.of(value);
        }

        /**
         * Returns an {@code Optional} with the {@code value} if {@code value} is not null, otherwise an empty {@code Optional} is returned.
         * 
         * @return
         */
        public Optional toOptional() {
            return Optional.ofNullable(value);
        }

        @Override
        public int hashCode() {
            return (value == null) ? 0 : value.hashCode();
        }

        @SuppressWarnings("rawtypes")
        @Override
        public boolean equals(final Object obj) {
            return this == obj || (obj instanceof Reference && N.equals(((Reference) obj).value, value));
        }

        @Override
        public String toString() {
            if (value == null) {
                return "Reference[null]";
            } else {
                return String.format("Reference[%s]", N.toString(value));
            }
        }
    }

    //    public static final class t extends u {
    //        private t() {
    //            // utility class
    //        }
    //    }
    //
    //    public static final class m extends u {
    //        private m() {
    //            // utility class
    //        }
    //    }
}