Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
DataCleaner (community edition) Copyright (C) 2014 Neopost - Customer Information Management This copyrighted material is made available to anyone wishing to use, modify, copy, or redistribute it subject to the terms and conditions of the GNU Lesser General Public License, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this distribution; if not, write to: Free Software Foundation, Inc. 51 Franklin Street, Fifth Floor Boston, MA 02110-1301 USA
 package org.datacleaner.beans.transform;
 import java.util.List;
 import java.util.Map;
 @Named("Math formula")
 @Description("Provide a mathematical formula to perform arbitrary calculations.\n"
         + "Formulas support basic operators like plus (+), minus (-), divide (/), multiply (*) and modulus (%).")
 public class MathFormulaTransformer implements Transformer {
     InputColumn<Number>[] _input;
     @StringProperty(emptyString = false, mimeType = "text/groovy")
     String _formula = "(col1 + col2) / col3";
     private ExpressionFactoryImpl _factory;
     private Map<StringList<String>> _columnAliases;
     public OutputColumns getOutputColumns() {
         return new OutputColumns(Number.class"Formula result");
     public void init() {
          = new ExpressionFactoryImpl();
          = new HashMap<StringList<String>>();
         int i = 1;
         for (InputColumn<NumberinputColumn : ) {
             final String name = inputColumn.getName();
             final List<Stringlist = new ArrayList<String>(3);
             final String variableName1 = StringUtils.replaceWhitespaces(name.toLowerCase(), "_");
             final String variableName2 = StringUtils.replaceWhitespaces(name.toLowerCase(), "");
             list.add("col" + i);
     public Number[] transform(InputRow inputRow) {
         final SimpleContext context = new SimpleContext();
         for (InputColumn<NumberinputColumn : ) {
             Number value = inputRow.getValue(inputColumn);
             ValueExpression valueExpression = .createValueExpression(valueNumber.class);
             List<Stringaliases = .get(inputColumn.getName());
             for (String alias : aliases) {
         final String expression = "#{" + .toLowerCase() + "}";
         final ValueExpression valueExpression = .createValueExpression(contextexpressionNumber.class);
        try {
            final Object value = valueExpression.getValue(context);
            assert value instanceof Number;
            if (value instanceof Double) {
                double dbl = ((Doublevalue).doubleValue();
                if (Double.isNaN(dbl) || Double.isInfinite(dbl)) {
                    // we don't want to present infinity and NaN - null is a
                    // better replacement.
                    return new Number[] { null };
            return new Number[] { (Numbervalue };
        } catch (ArithmeticException e) {
            return new Number[] { null };
New to GrepCode? Check out our FAQ X