de.invation.code.toval.constraint.StringOperator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TOVAL Show documentation
Show all versions of TOVAL Show documentation
TOVAL comprises a set of java classes for common programming issues. It includes utils for arrays, lists, sets and collections for convenient handling and modification, but also support for mathematic definitions concerning logic (clauses + resolution) together with some algorithms for permutations, powersets and resolution. Additionally it contains a number of types for multisets, matrices with object keys and much more.
The newest version!
package de.invation.code.toval.constraint;
import de.invation.code.toval.validate.ParameterException;
import de.invation.code.toval.validate.Validate;
import de.invation.code.toval.validate.ParameterException.ErrorCode;
public enum StringOperator implements Operator {
EQUAL(OperatorFormats.EQUAL, String.format(OperatorFormats.COMPARISON_FORMAT, OperatorFormats.EQUAL)),
NOT_EQUAL(OperatorFormats.NOT_EQUAL, String.format(OperatorFormats.COMPARISON_FORMAT, OperatorFormats.NOT_EQUAL));
private String[] argumentNames = new String[] {"Attribute", "Comparator"};
private String sign = null;
private String toStringFormat = null;
private StringOperator(String sign, String toStringFormat){
this.sign = sign;
this.toStringFormat = toStringFormat;
}
private StringOperator(String[] arguments, String sign, String toStringFormat){
this(sign, toStringFormat);
this.argumentNames = arguments;
}
@Override
public int getRequiredArguments(){
return argumentNames.length;
}
@Override
public String[] getArgumentNames(){
return argumentNames;
}
protected void validateValueType(Object value) throws ParameterException{
if(!String.class.isAssignableFrom(value.getClass()))
throw new ParameterException(ErrorCode.TYPE, "Wrong type of validation value, expected type: " + String.class);
}
@Override
public boolean validate(Object value, String... parameters) throws ParameterException {
Validate.notNull(value);
validateValueType(value);
Validate.notNull(parameters);
Validate.noNullElements(parameters);
Validate.isFalse(parameters.length + 1 != getRequiredArguments());
switch(this){
case EQUAL: return value.equals(parameters[0]);
case NOT_EQUAL: return !value.equals(parameters[0]);
default: return false;
}
}
@Override
public String getStringFormat(){
return toStringFormat;
}
@Override
public String toString(){
return sign;
}
public static StringOperator parse(String operatorString){
if(operatorString.equals("=="))
return StringOperator.EQUAL;
if(operatorString.equals("!="))
return StringOperator.NOT_EQUAL;
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy