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.

LogicalFunction.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.eval.*;
  17. /**
  18. * Implementation of the various ISxxx Logical Functions, which
  19. * take a single expression argument, and return True or False.
  20. */
  21. public abstract class LogicalFunction extends Fixed1ArgFunction {
  22. @SuppressWarnings("unused")
  23. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
  24. ValueEval ve;
  25. try {
  26. ve = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  27. } catch (EvaluationException e) {
  28. // Note - it is more usual to propagate error codes straight to the result like this:
  29. // but logical functions behave a little differently
  30. // return e.getErrorEval();
  31. // this will usually cause a 'FALSE' result except for ISNONTEXT()
  32. ve = e.getErrorEval();
  33. }
  34. return BoolEval.valueOf(evaluate(ve));
  35. }
  36. /**
  37. * @param arg any {@link ValueEval}, potentially {@link BlankEval} or {@link ErrorEval}.
  38. */
  39. protected abstract boolean evaluate(ValueEval arg);
  40. public static final Function ISLOGICAL = new LogicalFunction() {
  41. protected boolean evaluate(ValueEval arg) {
  42. return arg instanceof BoolEval;
  43. }
  44. };
  45. public static final Function ISNONTEXT = new LogicalFunction() {
  46. protected boolean evaluate(ValueEval arg) {
  47. return !(arg instanceof StringEval);
  48. }
  49. };
  50. public static final Function ISNUMBER = new LogicalFunction() {
  51. protected boolean evaluate(ValueEval arg) {
  52. return arg instanceof NumberEval;
  53. }
  54. };
  55. public static final Function ISTEXT = new LogicalFunction() {
  56. protected boolean evaluate(ValueEval arg) {
  57. return arg instanceof StringEval;
  58. }
  59. };
  60. public static final Function ISBLANK = new LogicalFunction() {
  61. protected boolean evaluate(ValueEval arg) {
  62. return arg instanceof BlankEval;
  63. }
  64. };
  65. public static final Function ISERROR = new LogicalFunction() {
  66. protected boolean evaluate(ValueEval arg) {
  67. return arg instanceof ErrorEval;
  68. }
  69. };
  70. /**
  71. * Implementation of Excel <tt>ISERR()</tt> function.<p/>
  72. *
  73. * <b>Syntax</b>:<br/>
  74. * <b>ISERR</b>(<b>value</b>)<p/>
  75. *
  76. * <b>value</b> The value to be tested<p/>
  77. *
  78. * Returns the logical value <tt>TRUE</tt> if value refers to any error value except
  79. * <tt>'#N/A'</tt>; otherwise, it returns <tt>FALSE</tt>.
  80. */
  81. public static final Function ISERR = new LogicalFunction() {
  82. @Override
  83. protected boolean evaluate(ValueEval arg) {
  84. if (arg instanceof ErrorEval) {
  85. return arg != ErrorEval.NA;
  86. }
  87. return false;
  88. }
  89. };
  90. /**
  91. * Implementation for Excel ISNA() function.<p/>
  92. *
  93. * <b>Syntax</b>:<br/>
  94. * <b>ISNA</b>(<b>value</b>)<p/>
  95. *
  96. * <b>value</b> The value to be tested<br/>
  97. * <br/>
  98. * Returns <tt>TRUE</tt> if the specified value is '#N/A', <tt>FALSE</tt> otherwise.
  99. */
  100. public static final Function ISNA = new LogicalFunction() {
  101. protected boolean evaluate(ValueEval arg) {
  102. return arg == ErrorEval.NA;
  103. }
  104. };
  105. public static final Function ISREF = new Fixed1ArgFunction() {
  106. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0) {
  107. if (arg0 instanceof RefEval || arg0 instanceof AreaEval || arg0 instanceof RefListEval) {
  108. return BoolEval.TRUE;
  109. }
  110. return BoolEval.FALSE;
  111. }
  112. };
  113. }