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.

BooleanFunction.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.functions;
  16. import org.apache.poi.ss.formula.TwoDEval;
  17. import org.apache.poi.ss.formula.eval.BlankEval;
  18. import org.apache.poi.ss.formula.eval.BoolEval;
  19. import org.apache.poi.ss.formula.eval.ErrorEval;
  20. import org.apache.poi.ss.formula.eval.EvaluationException;
  21. import org.apache.poi.ss.formula.eval.MissingArgEval;
  22. import org.apache.poi.ss.formula.eval.OperandResolver;
  23. import org.apache.poi.ss.formula.eval.RefEval;
  24. import org.apache.poi.ss.formula.eval.ValueEval;
  25. /**
  26. * Here are the general rules concerning Boolean functions:
  27. * <ol>
  28. * <li> Blanks are ignored (not either true or false) </li>
  29. * <li> Strings are ignored if part of an area ref or cell ref, otherwise they must be 'true' or 'false'</li>
  30. * <li> Numbers: 0 is false. Any other number is TRUE </li>
  31. * <li> Areas: *all* cells in area are evaluated according to the above rules</li>
  32. * </ol>
  33. */
  34. public abstract class BooleanFunction implements Function,ArrayFunction {
  35. public final ValueEval evaluate(ValueEval[] args, int srcRow, int srcCol) {
  36. if (args.length < 1) {
  37. return ErrorEval.VALUE_INVALID;
  38. }
  39. boolean boolResult;
  40. try {
  41. boolResult = calculate(args);
  42. } catch (EvaluationException e) {
  43. return e.getErrorEval();
  44. }
  45. return BoolEval.valueOf(boolResult);
  46. }
  47. private boolean calculate(ValueEval[] args) throws EvaluationException {
  48. boolean result = getInitialResultValue();
  49. boolean atLeastOneNonBlank = false;
  50. /*
  51. * Note: no short-circuit boolean loop exit because any ErrorEvals will override the result
  52. */
  53. for (final ValueEval arg : args) {
  54. Boolean tempVe;
  55. if (arg instanceof TwoDEval) {
  56. TwoDEval ae = (TwoDEval) arg;
  57. int height = ae.getHeight();
  58. int width = ae.getWidth();
  59. for (int rrIx=0; rrIx<height; rrIx++) {
  60. for (int rcIx=0; rcIx<width; rcIx++) {
  61. ValueEval ve = ae.getValue(rrIx, rcIx);
  62. tempVe = OperandResolver.coerceValueToBoolean(ve, true);
  63. if (tempVe != null) {
  64. result = partialEvaluate(result, tempVe);
  65. atLeastOneNonBlank = true;
  66. }
  67. }
  68. }
  69. continue;
  70. }
  71. if (arg instanceof RefEval) {
  72. RefEval re = (RefEval) arg;
  73. final int firstSheetIndex = re.getFirstSheetIndex();
  74. final int lastSheetIndex = re.getLastSheetIndex();
  75. for (int sIx = firstSheetIndex; sIx <= lastSheetIndex; sIx++) {
  76. ValueEval ve = re.getInnerValueEval(sIx);
  77. tempVe = OperandResolver.coerceValueToBoolean(ve, true);
  78. if (tempVe != null) {
  79. result = partialEvaluate(result, tempVe);
  80. atLeastOneNonBlank = true;
  81. }
  82. }
  83. continue;
  84. }
  85. if (arg == MissingArgEval.instance || arg == BlankEval.instance) {
  86. tempVe = false; // missing parameters are treated as FALSE
  87. } else {
  88. tempVe = OperandResolver.coerceValueToBoolean(arg, false);
  89. }
  90. if (tempVe != null) {
  91. result = partialEvaluate(result, tempVe);
  92. atLeastOneNonBlank = true;
  93. }
  94. }
  95. if (!atLeastOneNonBlank) {
  96. throw new EvaluationException(ErrorEval.VALUE_INVALID);
  97. }
  98. return result;
  99. }
  100. protected abstract boolean getInitialResultValue();
  101. protected abstract boolean partialEvaluate(boolean cumulativeResult, boolean currentValue);
  102. public static final Function AND = new BooleanFunction() {
  103. protected boolean getInitialResultValue() {
  104. return true;
  105. }
  106. protected boolean partialEvaluate(boolean cumulativeResult, boolean currentValue) {
  107. return cumulativeResult && currentValue;
  108. }
  109. };
  110. public static final Function OR = new BooleanFunction() {
  111. protected boolean getInitialResultValue() {
  112. return false;
  113. }
  114. protected boolean partialEvaluate(boolean cumulativeResult, boolean currentValue) {
  115. return cumulativeResult || currentValue;
  116. }
  117. };
  118. public static final Function FALSE = BooleanFunction::evaluateFalse;
  119. public static final Function TRUE = BooleanFunction::evaluateTrue;
  120. public static final Function NOT = BooleanFunction::evaluateNot;
  121. @Override
  122. public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  123. return evaluate(args, srcRowIndex, srcColumnIndex);
  124. }
  125. private static ValueEval evaluateFalse(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  126. return args.length != 0 ? ErrorEval.VALUE_INVALID : BoolEval.FALSE;
  127. }
  128. private static ValueEval evaluateTrue(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  129. return args.length != 0 ? ErrorEval.VALUE_INVALID : BoolEval.TRUE;
  130. }
  131. private static ValueEval evaluateNot(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  132. if (args.length != 1) {
  133. return ErrorEval.VALUE_INVALID;
  134. }
  135. java.util.function.Function<ValueEval, ValueEval> notInner = (va) -> {
  136. try {
  137. ValueEval ve = OperandResolver.getSingleValue(va, srcRowIndex, srcColumnIndex);
  138. Boolean b = OperandResolver.coerceValueToBoolean(ve, false);
  139. boolean boolArgVal = b != null && b;
  140. return BoolEval.valueOf(!boolArgVal);
  141. } catch (EvaluationException e) {
  142. return e.getErrorEval();
  143. }
  144. };
  145. return ArrayFunction._evaluateOneArrayArg(args[0], srcRowIndex, srcColumnIndex, notInner);
  146. }
  147. }