com.github.TKnudsen.ComplexDataObject.model.processors.complexDataObject.NumericAttributeMerger Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.model.processors.complexDataObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.TKnudsen.ComplexDataObject.data.complexDataObject.ComplexDataContainer;
import com.github.TKnudsen.ComplexDataObject.data.complexDataObject.ComplexDataObject;
import com.github.TKnudsen.ComplexDataObject.model.tools.MathFunctions;
/**
*
* Merges values from a list of given numerical attributes.
*
*
*
* Copyright: Copyright (c) 2017-2019
*
*
* @author Juergen Bernard
* @version 1.01
*/
public class NumericAttributeMerger implements IComplexDataObjectProcessor {
private final List attributes;
private final String targetAttribute;
public NumericAttributeMerger(List attributes, String targetAttribute) {
this.attributes = attributes;
this.targetAttribute = targetAttribute;
}
@Override
public void process(List data) {
Map> valuesMap = new HashMap<>();
// collect values
for (ComplexDataObject cdo : data) {
if (cdo == null)
continue;
List values = new ArrayList<>();
for (String attribute : attributes) {
Object o = cdo.getAttribute(attribute);
if (o == null)
continue;
if (!(o instanceof Number))
continue;
Number value = (Number) o;
if (Double.isNaN(value.doubleValue()))
continue;
values.add(value.doubleValue());
}
valuesMap.put(cdo, values);
}
// aggregate and assign to new attribute
for (ComplexDataObject cdo : data) {
if (cdo == null)
continue;
List list = valuesMap.get(cdo);
if (list == null || list.isEmpty())
cdo.add(targetAttribute, Double.NaN);
else
cdo.add(targetAttribute, MathFunctions.getMean(list));
}
}
@Override
public void process(ComplexDataContainer container) {
for (ComplexDataObject complexDataObject : container)
process(Arrays.asList(complexDataObject));
}
@Override
public DataProcessingCategory getPreprocessingCategory() {
return DataProcessingCategory.SECONDARY_DATA_PROVIDER;
}
}