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