]> source.dussan.org Git - poi.git/commitdiff
Converted ConcatEval to singleton
authorJosh Micich <josh@apache.org>
Thu, 23 Jul 2009 21:45:56 +0000 (21:45 +0000)
committerJosh Micich <josh@apache.org>
Thu, 23 Jul 2009 21:45:56 +0000 (21:45 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@797238 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/formula/eval/ConcatEval.java
src/java/org/apache/poi/ss/formula/OperationEvaluatorFactory.java

index 3833f94b9e055dd31bdddc44e203601f3f1be228..b79abe7af659f9cfcff26e0f61f74e7fccf86ad8 100644 (file)
 
 package org.apache.poi.hssf.record.formula.eval;
 
-import org.apache.poi.hssf.record.formula.ConcatPtg;
-
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- *  
  */
 public final class ConcatEval implements OperationEval {
 
-    private ConcatPtg delegate;
-
-    public ConcatEval(ConcatPtg ptg) {
-        delegate = ptg;
-    }
-
-    public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
-       if(args.length != 2) {
-               return ErrorEval.VALUE_INVALID;
-       }
-        StringBuffer sb = new StringBuffer();
-        try {
-                       for (int i = 0; i < 2; i++) { 
-                           
-                           ValueEval ve = OperandResolver.getSingleValue(args[i], srcRow, srcCol);
-                           if (ve instanceof StringValueEval) {
-                               StringValueEval sve = (StringValueEval) ve;
-                               sb.append(sve.getStringValue());
-                           } else if (ve == BlankEval.INSTANCE) {
-                               // do nothing
-                           } else {
-                               throw new RuntimeException("Unexpected value type (" 
-                                               + ve.getClass().getName() + ")");
-                           }
+       public static final OperationEval instance = new ConcatEval();
+
+       private ConcatEval() {
+               // enforce singleton
+       }
+
+       public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
+               if(args.length != 2) {
+                       return ErrorEval.VALUE_INVALID;
+               }
+               StringBuffer sb = new StringBuffer();
+               try {
+                       for (int i = 0; i < 2; i++) {
+
+                               ValueEval ve = OperandResolver.getSingleValue(args[i], srcRow, srcCol);
+                               if (ve instanceof StringValueEval) {
+                                       StringValueEval sve = (StringValueEval) ve;
+                                       sb.append(sve.getStringValue());
+                               } else if (ve == BlankEval.INSTANCE) {
+                                       // do nothing
+                               } else {
+                                       throw new RuntimeException("Unexpected value type ("
+                                                       + ve.getClass().getName() + ")");
+                               }
                        }
                } catch (EvaluationException e) {
                        return e.getErrorEval();
                }
-        
-        return new StringEval(sb.toString());
-    }
 
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
+               return new StringEval(sb.toString());
+       }
+
+       public int getNumberOfOperands() {
+               return 2;
+       }
 }
index 12c7fcda0b6f2a19587a2b023f071988024f6a67..22e1b1cb9ad7020d4e05684e41fe396b90b5c9b1 100755 (executable)
@@ -84,6 +84,7 @@ final class OperationEvaluatorFactory {
                m.put(LessThanPtg.class, LessThanEval.instance);
                m.put(NotEqualPtg.class, NotEqualEval.instance);
 
+               m.put(ConcatPtg.class, ConcatEval.instance);
                m.put(AddPtg.class, AddEval.instance);
                m.put(DividePtg.class, DivideEval.instance);
                m.put(MultiplyPtg.class, MultiplyEval.instance);
@@ -112,16 +113,13 @@ final class OperationEvaluatorFactory {
                if (result != null) {
                        return  result;
                }
-               
+
                if (ptgClass == FuncPtg.class) {
                        return new FuncVarEval((FuncPtg)ptg);
                }
                if (ptgClass == FuncVarPtg.class) {
                        return new FuncVarEval((FuncVarPtg)ptg);
                }
-               if (ptgClass == ConcatPtg.class) {
-                       return new ConcatEval((ConcatPtg)ptg);
-               }
                throw new RuntimeException("Unexpected operation ptg class (" + ptgClass.getName() + ")");
        }
 }