Converted ConcatEval to singleton

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@797238 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-07-23 21:45:56 +00:00
parent 15e434d684
commit e6d41ed590
2 changed files with 30 additions and 35 deletions

View File

@ -17,47 +17,44 @@
package org.apache.poi.hssf.record.formula.eval; package org.apache.poi.hssf.record.formula.eval;
import org.apache.poi.hssf.record.formula.ConcatPtg;
/** /**
* @author Amol S. Deshmukh < amolweb at ya hoo dot com > * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
*
*/ */
public final class ConcatEval implements OperationEval { public final class ConcatEval implements OperationEval {
private ConcatPtg delegate; public static final OperationEval instance = new ConcatEval();
public ConcatEval(ConcatPtg ptg) { private ConcatEval() {
delegate = ptg; // enforce singleton
} }
public Eval evaluate(Eval[] args, int srcRow, short srcCol) { public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
if(args.length != 2) { if(args.length != 2) {
return ErrorEval.VALUE_INVALID; return ErrorEval.VALUE_INVALID;
} }
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
try { try {
for (int i = 0; i < 2; i++) { for (int i = 0; i < 2; i++) {
ValueEval ve = OperandResolver.getSingleValue(args[i], srcRow, srcCol); ValueEval ve = OperandResolver.getSingleValue(args[i], srcRow, srcCol);
if (ve instanceof StringValueEval) { if (ve instanceof StringValueEval) {
StringValueEval sve = (StringValueEval) ve; StringValueEval sve = (StringValueEval) ve;
sb.append(sve.getStringValue()); sb.append(sve.getStringValue());
} else if (ve == BlankEval.INSTANCE) { } else if (ve == BlankEval.INSTANCE) {
// do nothing // do nothing
} else { } else {
throw new RuntimeException("Unexpected value type (" throw new RuntimeException("Unexpected value type ("
+ ve.getClass().getName() + ")"); + ve.getClass().getName() + ")");
} }
} }
} catch (EvaluationException e) { } catch (EvaluationException e) {
return e.getErrorEval(); return e.getErrorEval();
} }
return new StringEval(sb.toString());
}
public int getNumberOfOperands() { return new StringEval(sb.toString());
return delegate.getNumberOfOperands(); }
}
public int getNumberOfOperands() {
return 2;
}
} }

View File

@ -84,6 +84,7 @@ final class OperationEvaluatorFactory {
m.put(LessThanPtg.class, LessThanEval.instance); m.put(LessThanPtg.class, LessThanEval.instance);
m.put(NotEqualPtg.class, NotEqualEval.instance); m.put(NotEqualPtg.class, NotEqualEval.instance);
m.put(ConcatPtg.class, ConcatEval.instance);
m.put(AddPtg.class, AddEval.instance); m.put(AddPtg.class, AddEval.instance);
m.put(DividePtg.class, DivideEval.instance); m.put(DividePtg.class, DivideEval.instance);
m.put(MultiplyPtg.class, MultiplyEval.instance); m.put(MultiplyPtg.class, MultiplyEval.instance);
@ -112,16 +113,13 @@ final class OperationEvaluatorFactory {
if (result != null) { if (result != null) {
return result; return result;
} }
if (ptgClass == FuncPtg.class) { if (ptgClass == FuncPtg.class) {
return new FuncVarEval((FuncPtg)ptg); return new FuncVarEval((FuncPtg)ptg);
} }
if (ptgClass == FuncVarPtg.class) { if (ptgClass == FuncVarPtg.class) {
return new FuncVarEval((FuncVarPtg)ptg); return new FuncVarEval((FuncVarPtg)ptg);
} }
if (ptgClass == ConcatPtg.class) {
return new ConcatEval((ConcatPtg)ptg);
}
throw new RuntimeException("Unexpected operation ptg class (" + ptgClass.getName() + ")"); throw new RuntimeException("Unexpected operation ptg class (" + ptgClass.getName() + ")");
} }
} }