com.github.basking2.sdsai.itrex.functions.bool.CompareFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdsai-itrex Show documentation
Show all versions of sdsai-itrex Show documentation
An S-Expression inspiried library focused on iterators.
package com.github.basking2.sdsai.itrex.functions.bool;
import com.github.basking2.sdsai.itrex.EvaluationContext;
import com.github.basking2.sdsai.itrex.SExprRuntimeException;
import com.github.basking2.sdsai.itrex.functions.FunctionInterface;
import java.util.Iterator;
/**
* The compare function is used to constuct [eq], [gt], [gte], [lt] and [lte].
*/
public class CompareFunction implements FunctionInterface {
public static enum OP {
EQ {
@Override
public boolean test(int i) {
return i == 0;
}
},
LT {
@Override
public boolean test(int i) {
return i < 0;
}
},
GT {
@Override
public boolean test(int i) {
return i > 0;
}
},
LTE {
@Override
public boolean test(int i) {
return i <= 0;
}
},
GTE {
@Override
public boolean test(int i) {
return i >= 0;
}
};
public abstract boolean test(int i);
}
private final OP operator;
public CompareFunction(final OP operator) {
this.operator = operator;
}
@SuppressWarnings("unchecked")
@Override
public Boolean apply(Iterator> iterator, EvaluationContext evaluationContext) {
if (iterator.hasNext()) {
final Object o = iterator.next();
if ( ! (o instanceof Comparable)) {
throw new SExprRuntimeException("Arguments to compare functions must be Comparable objects.");
}
Comparable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy