You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OperationEvaluatorFactory.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.formula;
  16. import java.util.HashMap;
  17. import java.util.Map;
  18. import org.apache.poi.hssf.record.formula.AbstractFunctionPtg;
  19. import org.apache.poi.hssf.record.formula.AddPtg;
  20. import org.apache.poi.hssf.record.formula.ConcatPtg;
  21. import org.apache.poi.hssf.record.formula.DividePtg;
  22. import org.apache.poi.hssf.record.formula.EqualPtg;
  23. import org.apache.poi.hssf.record.formula.GreaterEqualPtg;
  24. import org.apache.poi.hssf.record.formula.GreaterThanPtg;
  25. import org.apache.poi.hssf.record.formula.LessEqualPtg;
  26. import org.apache.poi.hssf.record.formula.LessThanPtg;
  27. import org.apache.poi.hssf.record.formula.MultiplyPtg;
  28. import org.apache.poi.hssf.record.formula.NotEqualPtg;
  29. import org.apache.poi.hssf.record.formula.OperationPtg;
  30. import org.apache.poi.hssf.record.formula.PercentPtg;
  31. import org.apache.poi.hssf.record.formula.PowerPtg;
  32. import org.apache.poi.hssf.record.formula.Ptg;
  33. import org.apache.poi.hssf.record.formula.RangePtg;
  34. import org.apache.poi.hssf.record.formula.SubtractPtg;
  35. import org.apache.poi.hssf.record.formula.UnaryMinusPtg;
  36. import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
  37. import org.apache.poi.hssf.record.formula.eval.ConcatEval;
  38. import org.apache.poi.hssf.record.formula.eval.FunctionEval;
  39. import org.apache.poi.hssf.record.formula.eval.OperationEval;
  40. import org.apache.poi.hssf.record.formula.eval.PercentEval;
  41. import org.apache.poi.hssf.record.formula.eval.RangeEval;
  42. import org.apache.poi.hssf.record.formula.eval.RelationalOperationEval;
  43. import org.apache.poi.hssf.record.formula.eval.TwoOperandNumericOperation;
  44. import org.apache.poi.hssf.record.formula.eval.UnaryMinusEval;
  45. import org.apache.poi.hssf.record.formula.eval.UnaryPlusEval;
  46. import org.apache.poi.hssf.record.formula.eval.ValueEval;
  47. import org.apache.poi.hssf.record.formula.functions.Function;
  48. /**
  49. * This class creates <tt>OperationEval</tt> instances to help evaluate <tt>OperationPtg</tt>
  50. * formula tokens.
  51. *
  52. * @author Josh Micich
  53. */
  54. final class OperationEvaluatorFactory {
  55. private static final Map<Class<? extends Ptg>, OperationEval> _instancesByPtgClass = initialiseInstancesMap();
  56. private OperationEvaluatorFactory() {
  57. // no instances of this class
  58. }
  59. private static Map<Class<? extends Ptg>, OperationEval> initialiseInstancesMap() {
  60. Map<Class<? extends Ptg>, OperationEval> m = new HashMap<Class<? extends Ptg>, OperationEval>(32);
  61. put(m, 2, EqualPtg.class, RelationalOperationEval.EqualEval);
  62. put(m, 2, GreaterEqualPtg.class, RelationalOperationEval.GreaterEqualEval);
  63. put(m, 2, GreaterThanPtg.class, RelationalOperationEval.GreaterThanEval);
  64. put(m, 2, LessEqualPtg.class, RelationalOperationEval.LessEqualEval);
  65. put(m, 2, LessThanPtg.class, RelationalOperationEval.LessThanEval);
  66. put(m, 2, NotEqualPtg.class, RelationalOperationEval.NotEqualEval);
  67. put(m, 2, ConcatPtg.class, ConcatEval.instance);
  68. put(m, 2, AddPtg.class, TwoOperandNumericOperation.AddEval);
  69. put(m, 2, DividePtg.class, TwoOperandNumericOperation.DivideEval);
  70. put(m, 2, MultiplyPtg.class, TwoOperandNumericOperation.MultiplyEval);
  71. put(m, 1, PercentPtg.class, PercentEval.instance);
  72. put(m, 2, PowerPtg.class, TwoOperandNumericOperation.PowerEval);
  73. put(m, 2, SubtractPtg.class, TwoOperandNumericOperation.SubtractEval);
  74. put(m, 1, UnaryMinusPtg.class, UnaryMinusEval.instance);
  75. put(m, 1, UnaryPlusPtg.class, UnaryPlusEval.instance);
  76. put(m, 2, RangePtg.class, RangeEval.instance);
  77. return m;
  78. }
  79. private static void put(Map<Class<? extends Ptg>, OperationEval> m, int argCount,
  80. Class<? extends Ptg> ptgClass, Function instance) {
  81. m.put(ptgClass, new OperationFunctionEval(instance, argCount));
  82. }
  83. /**
  84. * Simple adapter from {@link OperationEval} to {@link Function}
  85. */
  86. private static final class OperationFunctionEval implements OperationEval {
  87. private final Function _function;
  88. private final int _numberOfOperands;
  89. public OperationFunctionEval(Function function, int argCount) {
  90. _function = function;
  91. _numberOfOperands = argCount;
  92. }
  93. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  94. return _function.evaluate(args, ec.getRowIndex(), (short) ec.getColumnIndex());
  95. }
  96. public int getNumberOfOperands() {
  97. return _numberOfOperands;
  98. }
  99. }
  100. /**
  101. * returns the OperationEval concrete impl instance corresponding
  102. * to the supplied operationPtg
  103. */
  104. public static OperationEval create(OperationPtg ptg) {
  105. if(ptg == null) {
  106. throw new IllegalArgumentException("ptg must not be null");
  107. }
  108. OperationEval result;
  109. Class<? extends OperationPtg> ptgClass = ptg.getClass();
  110. result = _instancesByPtgClass.get(ptgClass);
  111. if (result != null) {
  112. return result;
  113. }
  114. if (ptg instanceof AbstractFunctionPtg) {
  115. return new FunctionEval((AbstractFunctionPtg)ptg);
  116. }
  117. throw new RuntimeException("Unexpected operation ptg class (" + ptgClass.getName() + ")");
  118. }
  119. }