
org.datacleaner.result.AbstractCrosstabResultReducer Maven / Gradle / Ivy
/**
* DataCleaner (community edition)
* Copyright (C) 2014 Free Software Foundation, Inc.
*
* 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.result;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.metamodel.util.NumberComparator;
import org.datacleaner.api.AnalyzerResult;
import org.datacleaner.api.AnalyzerResultReducer;
/**
* Abstract reducer class for {@link CrosstabResult}s that are two dimensional
* and the dimensions are the same on all slave results. This scenario is quite
* common since a lot of analyzers produce crosstabs with measures on one
* dimension and column names on another.
*/
public abstract class AbstractCrosstabResultReducer implements AnalyzerResultReducer {
/**
* Helper method to get a sum of values (values will be checked whether they
* are integers only, or else a double will be returned).
*
* @param slaveValues
* @return
*/
protected static Number sum(final List> slaveValues) {
for (final Object slaveValue : slaveValues) {
if (slaveValue != null) {
final Class extends Object> cls = slaveValue.getClass();
if (!(cls == Integer.class || cls == Short.class || cls == Byte.class)) {
return sumAsDouble(slaveValues);
}
}
}
return sumAsInteger(slaveValues);
}
/**
* Helper method to get a sum of values (sum will be calculated as an
* integer)
*
* @param slaveValues
* @return
*/
protected static Integer sumAsInteger(final List> slaveValues) {
int sum = 0;
for (final Object slaveValue : slaveValues) {
final Number value = (Number) slaveValue;
if (value != null) {
sum += value.intValue();
}
}
return sum;
}
/**
* Helper method to get a sum of values (sum will be calculated as a double)
*
* @param slaveValues
* @return
*/
protected static Double sumAsDouble(final List> slaveValues) {
double sum = 0;
for (final Object slaveValue : slaveValues) {
final Number value = (Number) slaveValue;
if (value != null) {
sum += value.doubleValue();
}
}
return sum;
}
/**
* Helper method to get the maximum of all values (must be numbers)
*
* @param slaveValues
* @return
*/
protected static Number maximum(final List> slaveValues) {
Number max = null;
for (final Object slaveValue : slaveValues) {
if (max == null) {
max = (Number) slaveValue;
} else {
final Comparable
© 2015 - 2025 Weber Informatics LLC | Privacy Policy