Source for org.jfree.formula.operators.AbstractCompareOperator

   1: package org.jfree.formula.operators;
   2: 
   3: import org.jfree.formula.lvalues.TypeValuePair;
   4: import org.jfree.formula.FormulaContext;
   5: import org.jfree.formula.EvaluationException;
   6: import org.jfree.formula.LibFormulaErrorValue;
   7: import org.jfree.formula.typing.TypeRegistry;
   8: import org.jfree.formula.typing.Type;
   9: import org.jfree.formula.typing.ExtendedComparator;
  10: import org.jfree.formula.typing.coretypes.LogicalType;
  11: 
  12: /**
  13:  * Creation-Date: 06.06.2007, 18:52:25
  14:  *
  15:  * @author Thomas Morgner
  16:  */
  17: public abstract class AbstractCompareOperator implements InfixOperator
  18: {
  19:   private static final TypeValuePair RETURN_TRUE = new TypeValuePair(LogicalType.TYPE, Boolean.TRUE);
  20:   private static final TypeValuePair RETURN_FALSE = new TypeValuePair(LogicalType.TYPE, Boolean.FALSE);
  21: 
  22:   public AbstractCompareOperator()
  23:   {
  24:   }
  25: 
  26:   public final TypeValuePair evaluate(final FormulaContext context,
  27:                                       final TypeValuePair value1,
  28:                                       final TypeValuePair value2)
  29:       throws EvaluationException
  30:   {
  31:     final TypeRegistry typeRegistry = context.getTypeRegistry();
  32:     final Type type1 = value1.getType();
  33:     final Type type2 = value2.getType();
  34:     final Object value1Raw = value1.getValue();
  35:     final Object value2Raw = value2.getValue();
  36:     if (value1Raw == null || value2Raw == null)
  37:     {
  38:       throw new EvaluationException(LibFormulaErrorValue.ERROR_NA_VALUE);
  39:     }
  40: 
  41:     final ExtendedComparator comparator = typeRegistry.getComparator(type1, type2);
  42:     final Integer result = comparator.compare (type1, value1Raw, type2, value2Raw);
  43:     if (result == null)
  44:     {
  45:       throw new EvaluationException
  46:           (LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  47:     }
  48: 
  49:     if (evaluate(result.intValue()))
  50:     {
  51:       return RETURN_TRUE;
  52:     }
  53:     return RETURN_FALSE;
  54:   }
  55: 
  56:   protected abstract boolean evaluate(int compareResult);
  57: }