Source for org.jfree.formula.operators.EqualOperator

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: EqualOperator.java,v 1.7 2007/06/06 17:07:52 taqua Exp $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.operators;
  32: 
  33: import org.jfree.formula.EvaluationException;
  34: import org.jfree.formula.FormulaContext;
  35: import org.jfree.formula.LibFormulaErrorValue;
  36: import org.jfree.formula.typing.ExtendedComparator;
  37: import org.jfree.formula.typing.TypeRegistry;
  38: import org.jfree.formula.typing.Type;
  39: import org.jfree.formula.typing.coretypes.LogicalType;
  40: import org.jfree.formula.lvalues.TypeValuePair;
  41: 
  42: /**
  43:  * Creation-Date: 31.10.2006, 16:34:11
  44:  *
  45:  * @author Thomas Morgner
  46:  */
  47: public class EqualOperator implements InfixOperator
  48: {
  49:   private static final TypeValuePair RETURN_TRUE = new TypeValuePair(LogicalType.TYPE, Boolean.TRUE);
  50:   private static final TypeValuePair RETURN_FALSE = new TypeValuePair(LogicalType.TYPE, Boolean.FALSE);
  51: 
  52:   public EqualOperator()
  53:   {
  54:   }
  55: 
  56:   public TypeValuePair evaluate(final FormulaContext context,
  57:                                 final TypeValuePair value1,
  58:                                 final TypeValuePair value2)
  59:       throws EvaluationException
  60:   {
  61:     final TypeRegistry typeRegistry = context.getTypeRegistry();
  62:     final Object value1Raw = value1.getValue();
  63:     final Object value2Raw = value2.getValue();
  64:     if (value1Raw == null || value2Raw == null)
  65:     {
  66:       throw new EvaluationException(LibFormulaErrorValue.ERROR_NA_VALUE);
  67:     }
  68: 
  69:     final Type type1 = value1.getType();
  70:     final Type type2 = value2.getType();
  71:     final ExtendedComparator comparator = typeRegistry.getComparator(type1, type2);
  72:     final boolean result = comparator.isEqual (type1, value1Raw, type2, value2Raw);
  73:     if (result)
  74:     {
  75:       return RETURN_TRUE;
  76:     }
  77:     else
  78:     {
  79:       return RETURN_FALSE;
  80:     }
  81:   }
  82: 
  83:   public int getLevel()
  84:   {
  85:     return 400;
  86:   }
  87: 
  88: 
  89:   public String toString()
  90:   {
  91:     return "=";
  92:   }
  93: 
  94:   public boolean isLeftOperation()
  95:   {
  96:     return true;
  97:   }
  98: 
  99:   /**
 100:    * Defines, whether the operation is associative. For associative operations,
 101:    * the evaluation order does not matter, if the operation appears more than
 102:    * once in an expression, and therefore we can optimize them a lot better than
 103:    * non-associative operations (ie. merge constant parts and precompute them
 104:    * once).
 105:    *
 106:    * @return true, if the operation is associative, false otherwise
 107:    */
 108:   public boolean isAssociative()
 109:   {
 110:     return false;
 111:   }
 112: 
 113: }