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.

RelationalOperationEval.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.eval;
  16. import org.apache.poi.ss.formula.functions.ArrayFunction;
  17. import org.apache.poi.ss.formula.functions.Fixed2ArgFunction;
  18. import org.apache.poi.ss.formula.functions.Function;
  19. import org.apache.poi.ss.util.NumberComparer;
  20. /**
  21. * Base class for all comparison operator evaluators
  22. */
  23. public abstract class RelationalOperationEval extends Fixed2ArgFunction implements ArrayFunction {
  24. /**
  25. * Converts a standard compare result (-1, 0, 1) to <code>true</code> or <code>false</code>
  26. * according to subclass' comparison type.
  27. */
  28. protected abstract boolean convertComparisonResult(int cmpResult);
  29. /**
  30. * This is a description of how the relational operators apply in MS Excel.
  31. * Use this as a guideline when testing/implementing the evaluate methods
  32. * for the relational operators Evals.
  33. *
  34. * <pre>
  35. * Bool.TRUE &gt; any number.
  36. * Bool &gt; any string. ALWAYS
  37. * Bool.TRUE &gt; Bool.FALSE
  38. * Bool.FALSE == Blank
  39. *
  40. * Strings are never converted to numbers or booleans
  41. * String &gt; any number. ALWAYS
  42. * Non-empty String &gt; Blank
  43. * Empty String == Blank
  44. * String are sorted dictionary wise
  45. *
  46. * Blank &gt; Negative numbers
  47. * Blank == 0
  48. * Blank &lt; Positive numbers
  49. * </pre>
  50. */
  51. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  52. ValueEval vA;
  53. ValueEval vB;
  54. try {
  55. vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  56. vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex);
  57. } catch (EvaluationException e) {
  58. return e.getErrorEval();
  59. }
  60. int cmpResult = doCompare(vA, vB);
  61. boolean result = convertComparisonResult(cmpResult);
  62. return BoolEval.valueOf(result);
  63. }
  64. @Override
  65. public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  66. ValueEval arg0 = args[0];
  67. ValueEval arg1 = args[1];
  68. return evaluateTwoArrayArgs(arg0, arg1, srcRowIndex, srcColumnIndex, (vA, vB) -> {
  69. int cmpResult = doCompare(vA, vB);
  70. boolean result = convertComparisonResult(cmpResult);
  71. return BoolEval.valueOf(result);
  72. });
  73. }
  74. private static int doCompare(ValueEval va, ValueEval vb) {
  75. // special cases when one operand is blank or missing
  76. if (va == BlankEval.instance || va instanceof MissingArgEval) {
  77. return compareBlank(vb);
  78. }
  79. if (vb == BlankEval.instance || vb instanceof MissingArgEval) {
  80. return -compareBlank(va);
  81. }
  82. if (va instanceof BoolEval) {
  83. if (vb instanceof BoolEval) {
  84. BoolEval bA = (BoolEval) va;
  85. BoolEval bB = (BoolEval) vb;
  86. if (bA.getBooleanValue() == bB.getBooleanValue()) {
  87. return 0;
  88. }
  89. return bA.getBooleanValue() ? 1 : -1;
  90. }
  91. return 1;
  92. }
  93. if (vb instanceof BoolEval) {
  94. return -1;
  95. }
  96. if (va instanceof StringEval) {
  97. if (vb instanceof StringEval) {
  98. StringEval sA = (StringEval) va;
  99. StringEval sB = (StringEval) vb;
  100. return sA.getStringValue().compareToIgnoreCase(sB.getStringValue());
  101. }
  102. return 1;
  103. }
  104. if (vb instanceof StringEval) {
  105. return -1;
  106. }
  107. if (va instanceof NumberEval) {
  108. if (vb instanceof NumberEval) {
  109. NumberEval nA = (NumberEval) va;
  110. NumberEval nB = (NumberEval) vb;
  111. return NumberComparer.compare(nA.getNumberValue(), nB.getNumberValue());
  112. }
  113. }
  114. throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), ("
  115. + vb.getClass().getName() + ")");
  116. }
  117. private static int compareBlank(ValueEval v) {
  118. if (v == BlankEval.instance || v instanceof MissingArgEval) {
  119. return 0;
  120. }
  121. if (v instanceof BoolEval) {
  122. BoolEval boolEval = (BoolEval) v;
  123. return boolEval.getBooleanValue() ? -1 : 0;
  124. }
  125. if (v instanceof NumberEval) {
  126. NumberEval ne = (NumberEval) v;
  127. return NumberComparer.compare(0.0, ne.getNumberValue());
  128. }
  129. if (v instanceof StringEval) {
  130. StringEval se = (StringEval) v;
  131. return se.getStringValue().length() < 1 ? 0 : -1;
  132. }
  133. throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")");
  134. }
  135. public static final Function EqualEval = new RelationalOperationEval() {
  136. protected boolean convertComparisonResult(int cmpResult) {
  137. return cmpResult == 0;
  138. }
  139. };
  140. public static final Function GreaterEqualEval = new RelationalOperationEval() {
  141. protected boolean convertComparisonResult(int cmpResult) {
  142. return cmpResult >= 0;
  143. }
  144. };
  145. public static final Function GreaterThanEval = new RelationalOperationEval() {
  146. protected boolean convertComparisonResult(int cmpResult) {
  147. return cmpResult > 0;
  148. }
  149. };
  150. public static final Function LessEqualEval = new RelationalOperationEval() {
  151. protected boolean convertComparisonResult(int cmpResult) {
  152. return cmpResult <= 0;
  153. }
  154. };
  155. public static final Function LessThanEval = new RelationalOperationEval() {
  156. protected boolean convertComparisonResult(int cmpResult) {
  157. return cmpResult < 0;
  158. }
  159. };
  160. public static final Function NotEqualEval = new RelationalOperationEval() {
  161. protected boolean convertComparisonResult(int cmpResult) {
  162. return cmpResult != 0;
  163. }
  164. };
  165. }