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

de.schlegel11.lambdadecor.DefaultLambdaDecor Maven / Gradle / Ivy

There is a newer version: 1.2
Show newest version
package de.schlegel11.lambdadecor;

import java.util.Objects;
import java.util.function.Function;

/**
 * @author Marcel Schlegel (schlegel11)
 * @since 1.0
 */
public class DefaultLambdaDecor implements LambdaDecor {

    private Behaviour behaviour;
    private Unappliable unappliable = Unappliable.EMPTY;

    private DefaultLambdaDecor(Behaviour behaviour) {
        this.behaviour = Objects.requireNonNull(behaviour,
                "Behaviour argument \"behaviour\" for initialisation is null.");
    }

    /**
     * Creates a new instance.
     *
     * @param behaviour the {@link Behaviour} for this class
     * @param        type for this behaviour
     * @return the new instance
     * @throws NullPointerException if {@code behaviour} is null
     */
    public static  LambdaDecor create(Behaviour behaviour) {
        return new DefaultLambdaDecor<>(behaviour);
    }

    /**
     * Creates a new instance.
     *
     * @param behaviourFunction the {@link Function} that provides an new {@link DefaultBehaviour} instance and returns one.
     * @param  type for this behaviour
     * @return the new instance
     * @throws NullPointerException if the {@code function} return value is null
     */
    public static  LambdaDecor create(Function, Behaviour> behaviourFunction) {
        return create(Objects.requireNonNull(DefaultBehaviour.newBehaviour(behaviourFunction), "Behaviour is null."));
    }

    /**
     * Creates a new instance.
     *
     * @param  type for this behaviour
     * @return the new instance
     */
    public static  LambdaDecor create() {
        return create(DefaultBehaviour.newBehaviour());
    }

    @Override
    public LambdaDecor updateBehaviour(Function, Behaviour> behaviourFunction) {
        Objects.requireNonNull(behaviourFunction,
                "Function argument \"behaviourFunction\" in \"updateBehaviour(behaviourFunction)\" is null.");
        behaviour = Objects.requireNonNull(behaviourFunction.apply(behaviour), "Behaviour is null.");
        return this;
    }

    @Override
    public T apply(T type) {
        DecorPair pair = behaviour.apply(type);
        unappliable = pair._Unapply;
        return pair._Behaviour;
    }

    @Override
    public void unapply() {
        unappliable.unapply();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy