/*
* This file is part of JCF.
* JCF is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JCF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with JCF. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.bitfish.jcf.common.expression;
import java.text.ParseException;
import org.apache.commons.lang.StringUtils;
import eu.bitfish.jcf.common.expression.operand.AbstractOperand;
import eu.bitfish.jcf.common.util.DateUtil;
import eu.bitfish.jcf.processor.exception.FilterException;
/**
* Simple expression which only contains two operands and an operator.
*
* @author Michael Sieber
*
*/
public class SimpleExpression extends AbstractExpression {
private final AbstractOperand _first;
private final String _operator;
private final AbstractOperand _second;
/**
* Create a new {@link SimpleExpression}.
*
* @param first
* The first operand
* @param operator
* The operator
* @param second
* The second operand
*/
public SimpleExpression(AbstractOperand first, String operator,
AbstractOperand second) {
_first = first;
_operator = operator;
_second = second;
}
@Override
public boolean evaluate(Object obj) throws FilterException {
final String firstValue = _first.getValue(obj);
final String secondValue = _second.getValue(obj);
switch (_operator) {
case "!=":
return !firstValue.equals(secondValue);
case "=":
return firstValue.equals(secondValue);
case "not like":
return !firstValue.contains(secondValue);
case "like":
return firstValue.contains(secondValue);
case ">":
if (StringUtils.isNumeric(firstValue)
&& StringUtils.isNumeric(secondValue)) {
return Double.parseDouble(firstValue) > Double
.parseDouble(secondValue);
} else if (DateUtil.isDate(firstValue)
&& DateUtil.isDate(secondValue)) {
try {
return DateUtil.getDate(firstValue).compareTo(
DateUtil.getDate(secondValue)) == 1;
} catch (ParseException e) {
throw new FilterException("Unable to parse date ("
+ firstValue + ", " + secondValue + ")");
}
}
throw new FilterException("Operator > for value " + firstValue
+ " and value " + secondValue + " is not applicable.");
case ">=":
if (StringUtils.isNumeric(firstValue)
&& StringUtils.isNumeric(secondValue)) {
return Double.parseDouble(firstValue) > Double
.parseDouble(secondValue);
} else if (DateUtil.isDate(firstValue)
&& DateUtil.isDate(secondValue)) {
try {
return DateUtil.getDate(firstValue).compareTo(
DateUtil.getDate(secondValue)) >= 0;
} catch (ParseException e) {
throw new FilterException("Unable to parse date ("
+ firstValue + ", " + secondValue + ")");
}
}
throw new FilterException("Operator >= for value " + firstValue
+ " and value " + secondValue + " is not applicable.");
case "<":
if (StringUtils.isNumeric(firstValue)
&& StringUtils.isNumeric(secondValue)) {
return Double.parseDouble(firstValue) < Double
.parseDouble(secondValue);
} else if (DateUtil.isDate(firstValue)
&& DateUtil.isDate(secondValue)) {
try {
return DateUtil.getDate(firstValue).compareTo(
DateUtil.getDate(secondValue)) == -1;
} catch (ParseException e) {
throw new FilterException("Unable to parse date ("
+ firstValue + ", " + secondValue + ")");
}
}
throw new FilterException("Operator < for value " + firstValue
+ " and value " + secondValue + " is not applicable.");
case "<=":
if (StringUtils.isNumeric(firstValue)
&& StringUtils.isNumeric(secondValue)) {
return Double.parseDouble(firstValue) < Double
.parseDouble(secondValue);
} else if (DateUtil.isDate(firstValue)
&& DateUtil.isDate(secondValue)) {
try {
return DateUtil.getDate(firstValue).compareTo(
DateUtil.getDate(secondValue)) <= 0;
} catch (ParseException e) {
throw new FilterException("Unable to parse date ("
+ firstValue + ", " + secondValue + ")");
}
}
throw new FilterException("Operator <= for value " + firstValue
+ " and value " + secondValue + " is not applicable.");
}
throw new FilterException("Unkown operator " + _operator);
}
}