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.

RangeEval.java 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 RangeEval extends Fixed2ArgFunction {
  19. public static final Function instance = new RangeEval();
  20. private RangeEval() {
  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. return resolveRange(reA, reB);
  29. } catch (EvaluationException e) {
  30. return e.getErrorEval();
  31. }
  32. }
  33. /**
  34. * @return simple rectangular {@link AreaEval} which fully encloses both areas
  35. * {@code aeA} and {@code aeB}
  36. */
  37. private static AreaEval resolveRange(AreaEval aeA, AreaEval aeB) {
  38. int aeAfr = aeA.getFirstRow();
  39. int aeAfc = aeA.getFirstColumn();
  40. int top = Math.min(aeAfr, aeB.getFirstRow());
  41. int bottom = Math.max(aeA.getLastRow(), aeB.getLastRow());
  42. int left = Math.min(aeAfc, aeB.getFirstColumn());
  43. int right = Math.max(aeA.getLastColumn(), aeB.getLastColumn());
  44. return aeA.offset(top-aeAfr, bottom-aeAfr, left-aeAfc, right-aeAfc);
  45. }
  46. private static AreaEval evaluateRef(ValueEval arg) throws EvaluationException {
  47. if (arg instanceof AreaEval) {
  48. return (AreaEval) arg;
  49. }
  50. if (arg instanceof RefEval) {
  51. return ((RefEval) arg).offset(0, 0, 0, 0);
  52. }
  53. if (arg instanceof ErrorEval) {
  54. throw new EvaluationException((ErrorEval)arg);
  55. }
  56. throw new IllegalArgumentException("Unexpected ref arg class (" + arg.getClass().getName() + ")");
  57. }
  58. }