Source for org.jfree.formula.operators.ConcatOperator

   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: ConcatOperator.java 2887 2007-06-06 17:07:52Z taqua $
  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.lvalues.TypeValuePair;
  37: import org.jfree.formula.typing.TypeRegistry;
  38: import org.jfree.formula.typing.coretypes.TextType;
  39: 
  40: /**
  41:  * Concats two strings operator.
  42:  *
  43:  * @author Thomas Morgner
  44:  */
  45: public class ConcatOperator implements InfixOperator
  46: {
  47:   public ConcatOperator()
  48:   {
  49:   }
  50: 
  51:   public TypeValuePair evaluate(final FormulaContext context,
  52:                                 final TypeValuePair value1,
  53:                                 final TypeValuePair value2)
  54:       throws EvaluationException
  55:   {
  56:     final TypeRegistry typeRegistry = context.getTypeRegistry();
  57: 
  58:     // Error or empty string, that's the question ..
  59:     final Object raw1 = value1.getValue();
  60:     final Object raw2 = value2.getValue();
  61:     if (raw1 == null || raw2 == null)
  62:     {
  63:       throw new EvaluationException(LibFormulaErrorValue.ERROR_NA_VALUE);
  64:     }
  65: 
  66:     final String text1 = typeRegistry.convertToText(value1.getType(), raw1);
  67:     final String text2 = typeRegistry.convertToText(value2.getType(), raw2);
  68:     if (text1 == null && text2 == null)
  69:     {
  70:       throw new EvaluationException
  71:           (LibFormulaErrorValue.ERROR_INVALID_ARGUMENT_VALUE);
  72:     }
  73:     if (text1 == null)
  74:     {
  75:       return new TypeValuePair(TextType.TYPE, text2);
  76:     }
  77:     if (text2 == null)
  78:     {
  79:       return new TypeValuePair(TextType.TYPE, text1);
  80:     }
  81: 
  82:     return new TypeValuePair(TextType.TYPE, text1 + text2);
  83:   }
  84: 
  85:   public int getLevel()
  86:   {
  87:     return 300;
  88:   }
  89: 
  90: 
  91:   public String toString()
  92:   {
  93:     return "&";
  94:   }
  95: 
  96:   public boolean isLeftOperation()
  97:   {
  98:     return true;
  99:   }
 100: 
 101:   /**
 102:    * Defines, whether the operation is associative. For associative operations,
 103:    * the evaluation order does not matter, if the operation appears more than
 104:    * once in an expression, and therefore we can optimize them a lot better than
 105:    * non-associative operations (ie. merge constant parts and precompute them
 106:    * once).
 107:    *
 108:    * @return true, if the operation is associative, false otherwise
 109:    */
 110:   public boolean isAssociative()
 111:   {
 112:     return false;
 113:   }
 114: 
 115: }