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

com.github.paulosalonso.spel.builder.common.Instance Maven / Gradle / Ivy

The newest version!
package com.github.paulosalonso.spel.builder.common;

import static com.github.paulosalonso.spel.builder.common.InstanceMethod.instanceMethod;

public abstract class Instance extends Expression {
    private String methodChain = "";
    protected ChainState chainState = ChainState.STARTING;

    public InstanceMethod method(String methodName, Parameter... parameters) {
        InstanceMethod method = instanceMethod(this, methodName, parameters);
        methodChain = method.build();
        chainState = ChainState.IDLE;
        return method;
    }

    @Override
    protected String getPrefix() {
        return String.format("%s%s", methodChain, chainState.operator);
    }

    @Override
    protected String getSuffix() {
        return "";
    }

    @Override
    public String build() {
        if(ChainState.IDLE.equals(chainState)) {
            String buildResult = methodChain;
            methodChain = "";
            chainState = ChainState.STARTING;
            return buildResult;
        } else if (ChainState.WAITING_CHAIN.equals(chainState)) {
            return methodChain;
        }

        return super.build();
    }

    protected T and() {
        chainState = ChainState.WAITING_AND;
        return (T) this;
    }

    protected T or() {
        chainState = ChainState.WAITING_OR;
        return (T) this;
    }

    protected T chain() {
        chainState = ChainState.WAITING_CHAIN;
        return (T) this;
    }

    enum ChainState {
        STARTING(""),
        IDLE(""),
        WAITING_CHAIN("."),
        WAITING_AND(" && "),
        WAITING_OR(" || ");

        private final String operator;

        ChainState(String operator) {
            this.operator = operator;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy