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.

Delta.java 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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.OperationEvaluationContext;
  17. import org.apache.poi.ss.formula.eval.ErrorEval;
  18. import org.apache.poi.ss.formula.eval.EvaluationException;
  19. import org.apache.poi.ss.formula.eval.NumberEval;
  20. import org.apache.poi.ss.formula.eval.OperandResolver;
  21. import org.apache.poi.ss.formula.eval.ValueEval;
  22. /**
  23. * Implementation for Excel DELTA() function.<p>
  24. * <p>
  25. * <b>Syntax</b>:<br> <b>DELTA </b>(<b>number1</b>,<b>number2</b> )<br>
  26. * <p>
  27. * Tests whether two values are equal. Returns 1 if number1 = number2; returns 0 otherwise.
  28. * Use this function to filter a set of values. For example, by summing several DELTA functions
  29. * you calculate the count of equal pairs. This function is also known as the Kronecker Delta function.
  30. *
  31. * <ul>
  32. * <li>If number1 is nonnumeric, DELTA returns the #VALUE! error value.</li>
  33. * <li>If number2 is nonnumeric, DELTA returns the #VALUE! error value.</li>
  34. * </ul>
  35. *
  36. * @author cedric dot walter @ gmail dot com
  37. */
  38. public final class Delta extends Fixed2ArgFunction implements FreeRefFunction {
  39. public static final FreeRefFunction instance = new Delta();
  40. private static final NumberEval ONE = new NumberEval(1);
  41. private static final NumberEval ZERO = new NumberEval(0);
  42. public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg1, ValueEval arg2) {
  43. try {
  44. Double number1 = evaluateValue(arg1, srcRowIndex, srcColumnIndex);
  45. if (number1 == null) {
  46. return ErrorEval.VALUE_INVALID;
  47. }
  48. Double number2 = evaluateValue(arg2, srcRowIndex, srcColumnIndex);
  49. if (number2 == null) {
  50. return ErrorEval.VALUE_INVALID;
  51. }
  52. return (number1.compareTo(number2) == 0) ? ONE : ZERO;
  53. } catch (EvaluationException e) {
  54. return e.getErrorEval();
  55. }
  56. }
  57. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  58. if (args.length == 2) {
  59. return evaluate(ec.getRowIndex(), ec.getColumnIndex(), args[0], args[1]);
  60. }
  61. return ErrorEval.VALUE_INVALID;
  62. }
  63. private static Double evaluateValue(ValueEval arg, int srcRowIndex, int srcColumnIndex) throws EvaluationException {
  64. ValueEval veText = OperandResolver.getSingleValue(arg, srcRowIndex, srcColumnIndex);
  65. String strText1 = OperandResolver.coerceValueToString(veText);
  66. return OperandResolver.parseDouble(strText1);
  67. }
  68. }