PageRenderTime 355ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/libformula-1.1.3/source/org/pentaho/reporting/libraries/formula/function/information/IsErrFunction.java

#
Java | 96 lines | 65 code | 10 blank | 21 comment | 8 complexity | f219a2cc9dee676c056833c9be17ea27 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.function.information;
  18. import org.pentaho.reporting.libraries.formula.ErrorValue;
  19. import org.pentaho.reporting.libraries.formula.EvaluationException;
  20. import org.pentaho.reporting.libraries.formula.FormulaContext;
  21. import org.pentaho.reporting.libraries.formula.LibFormulaErrorValue;
  22. import org.pentaho.reporting.libraries.formula.function.Function;
  23. import org.pentaho.reporting.libraries.formula.function.ParameterCallback;
  24. import org.pentaho.reporting.libraries.formula.lvalues.TypeValuePair;
  25. import org.pentaho.reporting.libraries.formula.typing.Type;
  26. import org.pentaho.reporting.libraries.formula.typing.coretypes.ErrorType;
  27. import org.pentaho.reporting.libraries.formula.typing.coretypes.LogicalType;
  28. import org.apache.commons.logging.Log;
  29. import org.apache.commons.logging.LogFactory;
  30. /**
  31. * This function returns true if the parameter is of error and not of error type NA.
  32. *
  33. * @author Cedric Pronzato
  34. */
  35. public class IsErrFunction implements Function
  36. {
  37. private static final Log logger = LogFactory.getLog(IsErrFunction.class);
  38. private static final TypeValuePair RETURN_TRUE = new TypeValuePair(LogicalType.TYPE, Boolean.TRUE);
  39. private static final TypeValuePair RETURN_FALSE = new TypeValuePair(LogicalType.TYPE, Boolean.FALSE);
  40. private static final long serialVersionUID = 6749192734608313367L;
  41. public IsErrFunction()
  42. {
  43. }
  44. public TypeValuePair evaluate(final FormulaContext context,
  45. final ParameterCallback parameters) throws EvaluationException
  46. {
  47. if (parameters.getParameterCount() != 1)
  48. {
  49. throw EvaluationException.getInstance(LibFormulaErrorValue.ERROR_ARGUMENTS_VALUE);
  50. }
  51. try
  52. {
  53. final Type type = parameters.getType(0);
  54. final Object value = parameters.getValue(0);
  55. if (ErrorType.TYPE.equals(type) && value instanceof ErrorValue)
  56. {
  57. logger.warn ("Passing errors around is deprecated. Throw exceptions instead.");
  58. final ErrorValue na = (ErrorValue) value;
  59. if (na.getErrorCode() == LibFormulaErrorValue.ERROR_NA)
  60. {
  61. return RETURN_FALSE;
  62. }
  63. else
  64. {
  65. return RETURN_TRUE;
  66. }
  67. }
  68. }
  69. catch (EvaluationException e)
  70. {
  71. if (e.getErrorValue().getErrorCode() == LibFormulaErrorValue.ERROR_NA)
  72. {
  73. return RETURN_FALSE;
  74. }
  75. else
  76. {
  77. return RETURN_TRUE;
  78. }
  79. }
  80. return RETURN_FALSE;
  81. }
  82. public String getCanonicalName()
  83. {
  84. return "ISERR";
  85. }
  86. }