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 \"updateBehaviour\" for initialisation is null.");
    }

    public static  LambdaDecor create(Behaviour behaviour) {
        return new DefaultLambdaDecor<>(behaviour);
    }

    public static  LambdaDecor create() {
        return create(DefaultBehaviour.newBehaviour());
    }

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

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy