選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IntersectionEval.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Fixed2ArgFunction;
  17. import org.apache.poi.ss.formula.functions.Function;
  18. public final class IntersectionEval extends Fixed2ArgFunction {
  19. public static final Function instance = new IntersectionEval();
  20. private IntersectionEval() {
  21. // enforces singleton
  22. }
  23. @Override
  24. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) {
  25. try {
  26. AreaEval reA = evaluateRef(arg0);
  27. AreaEval reB = evaluateRef(arg1);
  28. AreaEval result = resolveRange(reA, reB);
  29. if (result == null) {
  30. return ErrorEval.NULL_INTERSECTION;
  31. }
  32. return result;
  33. } catch (EvaluationException e) {
  34. return e.getErrorEval();
  35. }
  36. }
  37. /**
  38. * @return simple rectangular {@link AreaEval} which represents the intersection of areas
  39. * {@code aeA} and {@code aeB}. If the two areas do not intersect, the result is {@code null}.
  40. */
  41. private static AreaEval resolveRange(AreaEval aeA, AreaEval aeB) {
  42. int aeAfr = aeA.getFirstRow();
  43. int aeAfc = aeA.getFirstColumn();
  44. int aeBlc = aeB.getLastColumn();
  45. if (aeAfc > aeBlc) {
  46. return null;
  47. }
  48. int aeBfc = aeB.getFirstColumn();
  49. if (aeBfc > aeA.getLastColumn()) {
  50. return null;
  51. }
  52. int aeBlr = aeB.getLastRow();
  53. if (aeAfr > aeBlr) {
  54. return null;
  55. }
  56. int aeBfr = aeB.getFirstRow();
  57. int aeAlr = aeA.getLastRow();
  58. if (aeBfr > aeAlr) {
  59. return null;
  60. }
  61. int top = Math.max(aeAfr, aeBfr);
  62. int bottom = Math.min(aeAlr, aeBlr);
  63. int left = Math.max(aeAfc, aeBfc);
  64. int right = Math.min(aeA.getLastColumn(), aeBlc);
  65. return aeA.offset(top-aeAfr, bottom-aeAfr, left-aeAfc, right-aeAfc);
  66. }
  67. private static AreaEval evaluateRef(ValueEval arg) throws EvaluationException {
  68. if (arg instanceof AreaEval) {
  69. return (AreaEval) arg;
  70. }
  71. if (arg instanceof RefEval) {
  72. return ((RefEval) arg).offset(0, 0, 0, 0);
  73. }
  74. if (arg instanceof ErrorEval) {
  75. throw new EvaluationException((ErrorEval)arg);
  76. }
  77. throw new IllegalArgumentException("Unexpected ref arg class (" + arg.getClass().getName() + ")");
  78. }
  79. }