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

org.dbflute.optional.OptionalObject Maven / Gradle / Ivy

There is a newer version: 1.2.8
Show newest version
/*
 * Copyright 2014-2023 the original author or authors.
 *
 * 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 org.dbflute.optional;

import java.util.stream.Stream;

import org.dbflute.helper.function.IndependentProcessor;

/**
 * @param  The type of object.
 * @author jflute
 * @since 1.1.0-sp1 (2015/01/19 Monday)
 */
public class OptionalObject extends BaseOptional {

    // ===================================================================================
    //                                                                          Definition
    //                                                                          ==========
    private static final long serialVersionUID = 1L; // basically cannot use (for optional entity)

    protected static final OptionalObject EMPTY_INSTANCE;
    static {
        EMPTY_INSTANCE = new OptionalObject(null, () -> {
            String msg = "The empty optional so the value is null.";
            throw new IllegalStateException(msg);
        });
    }
    protected static final OptionalThingExceptionThrower NOWAY_THROWER = () -> {
        throw new IllegalStateException("no way");
    };

    // ===================================================================================
    //                                                                         Constructor
    //                                                                         ===========
    /**
     * @param thing The wrapped instance of thing. (NullAllowed)
     * @param thrower The exception thrower when illegal access. (NotNull)
     */
    public OptionalObject(OBJ thing, OptionalThingExceptionThrower thrower) { // basically called by DBFlute
        super(thing, thrower);
    }

    /**
     * @param  The type of empty optional object.
     * @return The fixed instance as empty. (NotNull)
     */
    @SuppressWarnings("unchecked")
    public static  OptionalObject empty() {
        return (OptionalObject) EMPTY_INSTANCE;
    }

    /**
     * @param  The type of object wrapped in the optional object.
     * @param object The wrapped thing which is optional. (NotNull)
     * @return The new-created instance as existing optional object. (NotNull)
     */
    public static  OptionalObject of(OBJ object) {
        if (object == null) {
            String msg = "The argument 'object' should not be null.";
            throw new IllegalArgumentException(msg);
        }
        return new OptionalObject(object, NOWAY_THROWER);
    }

    /**
     * @param  The type of object wrapped in the optional object.
     * @param object The wrapped instance or thing. (NullAllowed)
     * @param noArgLambda The callback for exception when illegal access. (NotNull)
     * @return The new-created instance as existing or empty optional object. (NotNull)
     */
    public static  OptionalObject ofNullable(OBJ object, OptionalThingExceptionThrower noArgLambda) {
        if (object != null) {
            return of(object);
        } else {
            return new OptionalObject(object, noArgLambda);
        }
    }

    // ===================================================================================
    //                                                                   Standard Handling
    //                                                                   =================
    // -----------------------------------------------------
    //                                             ifPresent
    //                                             ---------
    /** {@inheritDoc} */
    public OptionalThingIfPresentAfter ifPresent(OptionalThingConsumer oneArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        return callbackIfPresent(oneArgLambda);
    }

    /** {@inheritDoc} */
    public void ifPresentOrElse(OptionalThingConsumer oneArgLambda, IndependentProcessor noArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        assertNoArgLambdaNotNull(noArgLambda);
        callbackIfPresentOrElse(oneArgLambda, noArgLambda);
    }

    // -----------------------------------------------------
    //                                         isPresent/get
    //                                         -------------
    /** {@inheritDoc} */
    public boolean isPresent() {
        return determinePresent();
    }

    /** {@inheritDoc} */
    public boolean isEmpty() {
        return determineEmpty();
    }

    /** {@inheritDoc} */
    public OBJ get() {
        return directlyGet();
    }

    // -----------------------------------------------------
    //                                             or/orElse
    //                                             ---------
    /** {@inheritDoc} */
    public OptionalThing or(OptionalThingSupplier> noArgLambda) {
        assertNoArgLambdaNotNull(noArgLambda);
        return callbackOr(noArgLambda);
    }

    /** {@inheritDoc} */
    public OBJ orElse(OBJ other) {
        return directlyGetOrElse(other);
    }

    /** {@inheritDoc} */
    public OBJ orElseGet(OptionalThingSupplier noArgLambda) {
        assertNoArgLambdaNotNull(noArgLambda);
        return callbackGetOrElseGet(noArgLambda);
    }

    /** {@inheritDoc} */
    public OBJ orElseThrow() {
        return directlyGet();
    }

    /** {@inheritDoc} */
    @Override
    public  OBJ orElseThrow(OptionalThingSupplier noArgLambda) throws CAUSE {
        assertNoArgLambdaNotNull(noArgLambda);
        return callbackGetOrElseThrow(noArgLambda);
    }

    /** {@inheritDoc} */
    public  OBJ orElseTranslatingThrow(
            OptionalThingFunction causeLambda) throws TRANSLATED {
        assertCauseLambdaNotNull(causeLambda);
        return callbackGetOrElseTranslatingThrow(causeLambda);
    }

    // -----------------------------------------------------
    //                                            filter/map
    //                                            ----------
    /** {@inheritDoc} */
    public OptionalObject filter(OptionalThingPredicate oneArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        return (OptionalObject) callbackFilter(oneArgLambda);
    }

    /** {@inheritDoc} */
    @Override
    protected  OptionalObject createOptionalFilteredObject(ARG obj) {
        return new OptionalObject(obj, _thrower);
    }

    /** {@inheritDoc} */
    @SuppressWarnings("unchecked")
    public  OptionalThing map(OptionalThingFunction oneArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        return (OptionalThing) callbackMapping(oneArgLambda); // downcast allowed because factory is overridden
    }

    /** {@inheritDoc} */
    @Override
    protected  OptionalObject createOptionalMappedObject(ARG obj) {
        return new OptionalObject(obj, _thrower);
    }

    /** {@inheritDoc} */
    public  OptionalThing flatMap(OptionalThingFunction> oneArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        return callbackFlatMapping(oneArgLambda);
    }

    /** {@inheritDoc} */
    @Override
    protected  OptionalObject createOptionalFlatMappedObject(ARG obj) {
        return new OptionalObject(obj, _thrower);
    }

    // -----------------------------------------------------
    //                                                stream
    //                                                ------
    /** {@inheritDoc} */
    public Stream stream() {
        return convertToStream();
    }

    // ===================================================================================
    //                                                                   DBFlute Extension
    //                                                                   =================
    /** {@inheritDoc} */
    public void alwaysPresent(OptionalThingConsumer oneArgLambda) {
        assertOneArgLambdaNotNull(oneArgLambda);
        callbackAlwaysPresent(oneArgLambda);
    }
}