Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RelationalOperationEval.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.CacheAreaEval;
  17. import org.apache.poi.ss.formula.functions.ArrayFunction;
  18. import org.apache.poi.ss.formula.functions.Fixed2ArgFunction;
  19. import org.apache.poi.ss.formula.functions.Function;
  20. import org.apache.poi.ss.util.NumberComparer;
  21. /**
  22. * Base class for all comparison operator evaluators
  23. *
  24. * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  25. */
  26. public abstract class RelationalOperationEval extends Fixed2ArgFunction implements ArrayFunction {
  27. /**
  28. * Converts a standard compare result (-1, 0, 1) to <code>true</code> or <code>false</code>
  29. * according to subclass' comparison type.
  30. */
  31. protected abstract boolean convertComparisonResult(int cmpResult);
  32. /**
  33. * This is a description of how the relational operators apply in MS Excel.
  34. * Use this as a guideline when testing/implementing the evaluate methods
  35. * for the relational operators Evals.
  36. *
  37. * <pre>
  38. * Bool.TRUE > any number.
  39. * Bool > any string. ALWAYS
  40. * Bool.TRUE > Bool.FALSE
  41. * Bool.FALSE == Blank
  42. *
  43. * Strings are never converted to numbers or booleans
  44. * String > any number. ALWAYS
  45. * Non-empty String > Blank
  46. * Empty String == Blank
  47. * String are sorted dictionary wise
  48. *
  49. * Blank > Negative numbers
  50. * Blank == 0
  51. * Blank < Positive numbers
  52. * </pre>
  53. */
  54. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  55. ValueEval vA;
  56. ValueEval vB;
  57. try {
  58. vA = OperandResolver.getSingleValue(arg0, srcRowIndex, srcColumnIndex);
  59. vB = OperandResolver.getSingleValue(arg1, srcRowIndex, srcColumnIndex);
  60. } catch (EvaluationException e) {
  61. return e.getErrorEval();
  62. }
  63. int cmpResult = doCompare(vA, vB);
  64. boolean result = convertComparisonResult(cmpResult);
  65. return BoolEval.valueOf(result);
  66. }
  67. public ValueEval evaluateArray(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
  68. ValueEval arg0 = args[0];
  69. ValueEval arg1 = args[1];
  70. int w1, w2, h1, h2;
  71. int a1FirstCol = 0, a1FirstRow = 0;
  72. if (arg0 instanceof AreaEval) {
  73. AreaEval ae = (AreaEval)arg0;
  74. w1 = ae.getWidth();
  75. h1 = ae.getHeight();
  76. a1FirstCol = ae.getFirstColumn();
  77. a1FirstRow = ae.getFirstRow();
  78. } else if (arg0 instanceof RefEval){
  79. RefEval ref = (RefEval)arg0;
  80. w1 = 1;
  81. h1 = 1;
  82. a1FirstCol = ref.getColumn();
  83. a1FirstRow = ref.getRow();
  84. } else {
  85. w1 = 1;
  86. h1 = 1;
  87. }
  88. int a2FirstCol = 0, a2FirstRow = 0;
  89. if (arg1 instanceof AreaEval) {
  90. AreaEval ae = (AreaEval)arg1;
  91. w2 = ae.getWidth();
  92. h2 = ae.getHeight();
  93. a2FirstCol = ae.getFirstColumn();
  94. a2FirstRow = ae.getFirstRow();
  95. } else if (arg1 instanceof RefEval){
  96. RefEval ref = (RefEval)arg1;
  97. w2 = 1;
  98. h2 = 1;
  99. a2FirstCol = ref.getColumn();
  100. a2FirstRow = ref.getRow();
  101. } else {
  102. w2 = 1;
  103. h2 = 1;
  104. }
  105. int width = Math.max(w1, w2);
  106. int height = Math.max(h1, h2);
  107. ValueEval[] vals = new ValueEval[height * width];
  108. int idx = 0;
  109. for(int i = 0; i < height; i++){
  110. for(int j = 0; j < width; j++){
  111. ValueEval vA;
  112. try {
  113. vA = OperandResolver.getSingleValue(arg0, a1FirstRow + i, a1FirstCol + j);
  114. } catch (EvaluationException e) {
  115. vA = e.getErrorEval();
  116. }
  117. ValueEval vB;
  118. try {
  119. vB = OperandResolver.getSingleValue(arg1, a2FirstRow + i, a2FirstCol + j);
  120. } catch (EvaluationException e) {
  121. vB = e.getErrorEval();
  122. }
  123. if(vA instanceof ErrorEval){
  124. vals[idx++] = vA;
  125. } else if (vB instanceof ErrorEval) {
  126. vals[idx++] = vB;
  127. } else {
  128. int cmpResult = doCompare(vA, vB);
  129. boolean result = convertComparisonResult(cmpResult);
  130. vals[idx++] = BoolEval.valueOf(result);
  131. }
  132. }
  133. }
  134. if (vals.length == 1) {
  135. return vals[0];
  136. }
  137. return new CacheAreaEval(srcRowIndex, srcColumnIndex, srcRowIndex + height - 1, srcColumnIndex + width - 1, vals);
  138. }
  139. private static int doCompare(ValueEval va, ValueEval vb) {
  140. // special cases when one operand is blank
  141. if (va == BlankEval.instance) {
  142. return compareBlank(vb);
  143. }
  144. if (vb == BlankEval.instance) {
  145. return -compareBlank(va);
  146. }
  147. if (va instanceof BoolEval) {
  148. if (vb instanceof BoolEval) {
  149. BoolEval bA = (BoolEval) va;
  150. BoolEval bB = (BoolEval) vb;
  151. if (bA.getBooleanValue() == bB.getBooleanValue()) {
  152. return 0;
  153. }
  154. return bA.getBooleanValue() ? 1 : -1;
  155. }
  156. return 1;
  157. }
  158. if (vb instanceof BoolEval) {
  159. return -1;
  160. }
  161. if (va instanceof StringEval) {
  162. if (vb instanceof StringEval) {
  163. StringEval sA = (StringEval) va;
  164. StringEval sB = (StringEval) vb;
  165. return sA.getStringValue().compareToIgnoreCase(sB.getStringValue());
  166. }
  167. return 1;
  168. }
  169. if (vb instanceof StringEval) {
  170. return -1;
  171. }
  172. if (va instanceof NumberEval) {
  173. if (vb instanceof NumberEval) {
  174. NumberEval nA = (NumberEval) va;
  175. NumberEval nB = (NumberEval) vb;
  176. return NumberComparer.compare(nA.getNumberValue(), nB.getNumberValue());
  177. }
  178. }
  179. throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), ("
  180. + vb.getClass().getName() + ")");
  181. }
  182. private static int compareBlank(ValueEval v) {
  183. if (v == BlankEval.instance) {
  184. return 0;
  185. }
  186. if (v instanceof BoolEval) {
  187. BoolEval boolEval = (BoolEval) v;
  188. return boolEval.getBooleanValue() ? -1 : 0;
  189. }
  190. if (v instanceof NumberEval) {
  191. NumberEval ne = (NumberEval) v;
  192. return NumberComparer.compare(0.0, ne.getNumberValue());
  193. }
  194. if (v instanceof StringEval) {
  195. StringEval se = (StringEval) v;
  196. return se.getStringValue().length() < 1 ? 0 : -1;
  197. }
  198. throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")");
  199. }
  200. public static final Function EqualEval = new RelationalOperationEval() {
  201. protected boolean convertComparisonResult(int cmpResult) {
  202. return cmpResult == 0;
  203. }
  204. };
  205. public static final Function GreaterEqualEval = new RelationalOperationEval() {
  206. protected boolean convertComparisonResult(int cmpResult) {
  207. return cmpResult >= 0;
  208. }
  209. };
  210. public static final Function GreaterThanEval = new RelationalOperationEval() {
  211. protected boolean convertComparisonResult(int cmpResult) {
  212. return cmpResult > 0;
  213. }
  214. };
  215. public static final Function LessEqualEval = new RelationalOperationEval() {
  216. protected boolean convertComparisonResult(int cmpResult) {
  217. return cmpResult <= 0;
  218. }
  219. };
  220. public static final Function LessThanEval = new RelationalOperationEval() {
  221. protected boolean convertComparisonResult(int cmpResult) {
  222. return cmpResult < 0;
  223. }
  224. };
  225. public static final Function NotEqualEval = new RelationalOperationEval() {
  226. protected boolean convertComparisonResult(int cmpResult) {
  227. return cmpResult != 0;
  228. }
  229. };
  230. }