com.github.paulosalonso.spel.builder.common.Instance Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spel-builder Show documentation
Show all versions of spel-builder Show documentation
A facilitator for creating Spring expressions (SpEL).
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;
}
}
}