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

eu.lunisolar.magma.basics.builder.PerCaseBuilderWithProduct Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is part of "lunisolar-magma".
 *
 * (C) Copyright 2014-2019 Lunisolar (http://lunisolar.eu/).
 *
 * 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 eu.lunisolar.magma.basics.builder;

import javax.annotation.Nonnull;
import java.util.function.*;

/**
 * Abstract implementation of a per case builder of a complex implementation of a functional interface.
 *
 * Each case consist of a appropriate condition - predicate, supplier, or operator depending on the functional interface type - and a evaluating function
 * (same type as overall function) that will be called when condition expression evaluates to **true**.
 *
 * Cases are evaluated in a order one by one. First condition that returns **true** will decide what function will be called. Eventually if no condition is
 * evaluating to **true** a last resort function _eventually_ is called. By default _eventually_ will throw an exception that there is no case that will cover
 * the input data. This default _eventually_ behavior can be overridden.
 */
@SuppressWarnings("unchecked")
public abstract class PerCaseBuilderWithProduct, P, F, R, PC extends PartialCaseWithProduct> extends PerCaseBuilder {

    protected @Nonnull final Function directToFunction;

    protected PerCaseBuilderWithProduct(@Nonnull F eventually, @Nonnull Function directToFunction, @Nonnull Supplier subCasesFactory) {
        super(eventually, subCasesFactory);
        this.directToFunction = directToFunction;
    }

    // 

    /** Sets the function to evaluate _eventually_ when input data do not match any case. */
    public final PCB eventuallyProduce(@Nonnull R directValue) {
        eventually = directToFunction.apply(directValue);
        return self();
    }

    // 

    protected PC partialCaseFactoryMethod(P casePredicate) {
        return (PC) new PartialCaseWithProduct(PerCaseBuilderWithProduct.this, casePredicate, subCasesFactory);
    }

    public static abstract class Base, P, F, R> extends PerCaseBuilderWithProduct> {
        protected Base(@Nonnull F eventually, @Nonnull Function directToFunction, @Nonnull Supplier subCasesFactory) {
            super(eventually, directToFunction, subCasesFactory);
        }

        @Override
        protected PartialCaseWithProduct.The partialCaseFactoryMethod(P casePredicate) {
            return new PartialCaseWithProduct.The(self(), casePredicate, subCasesFactory);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy