PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/libformula-1.1.3/source/org/pentaho/reporting/libraries/formula/operators/DivideOperator.java

#
Java | 77 lines | 33 code | 13 blank | 31 comment | 0 complexity | f880fe6c22c61bb1f49a3eadb88dba14 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it under the
  3. * terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
  4. * Foundation.
  5. *
  6. * You should have received a copy of the GNU Lesser General Public License along with this
  7. * program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  8. * or from the Free Software Foundation, Inc.,
  9. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU Lesser General Public License for more details.
  14. *
  15. * Copyright (c) 2006 - 2009 Pentaho Corporation and Contributors. All rights reserved.
  16. */
  17. package org.pentaho.reporting.libraries.formula.operators;
  18. import java.math.BigDecimal;
  19. import org.pentaho.reporting.libraries.formula.EvaluationException;
  20. import org.pentaho.reporting.libraries.formula.util.NumberUtil;
  21. /**
  22. * A division operation. This operation expects two valid numbers.
  23. *
  24. *
  25. * @author Thomas Morgner
  26. */
  27. public class DivideOperator extends AbstractNumericOperator
  28. {
  29. private static final long serialVersionUID = 3298154333839229191L;
  30. public DivideOperator()
  31. {
  32. }
  33. public Number evaluate(final Number number1, final Number number2) throws EvaluationException
  34. {
  35. final BigDecimal bd1 = NumberUtil.getAsBigDecimal(number1);
  36. final BigDecimal bd2 = NumberUtil.getAsBigDecimal(number2);
  37. return NumberUtil.divide(bd1, bd2);
  38. }
  39. public int getLevel()
  40. {
  41. return 100;
  42. }
  43. public String toString()
  44. {
  45. return "/";
  46. }
  47. public boolean isLeftOperation()
  48. {
  49. return true;
  50. }
  51. /**
  52. * Defines, whether the operation is associative. For associative operations,
  53. * the evaluation order does not matter, if the operation appears more than
  54. * once in an expression, and therefore we can optimize them a lot better than
  55. * non-associative operations (ie. merge constant parts and precompute them
  56. * once).
  57. *
  58. * @return true, if the operation is associative, false otherwise
  59. */
  60. public boolean isAssociative()
  61. {
  62. return false;
  63. }
  64. }