Fix saving routine for descriptions
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / descriptiveDataSet / matrix / QuantitativeDataStatistics.java
1 /**
2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.editor.descriptiveDataSet.matrix;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14
15 import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
16
17 import eu.etaxonomy.cdm.model.description.QuantitativeData;
18 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
19
20 /**
21 * @author pplitzner
22 * @since Jul 18, 2018
23 *
24 */
25 public class QuantitativeDataStatistics {
26
27 private List<DescriptiveStatistics> statistics = new ArrayList<>();
28
29 public void addQuantitativeData(QuantitativeData quantitativeData){
30 double[] exactValues = quantitativeData.getStatisticalValues().stream()
31 .filter(value -> value.getType().equals(StatisticalMeasure.EXACT_VALUE()))
32 .mapToDouble(value -> value.getValue()).toArray();
33 //if there are exact values we use those
34 if (exactValues.length > 0) {
35 statistics.add(new DescriptiveStatistics(exactValues));
36 }
37 //otherwise we use the aggregated values (min, max, etc.)
38 else{
39 DescriptiveStatistics aggregatedStatistics = new DescriptiveStatistics();
40 if(quantitativeData.getMin()!=null){
41 aggregatedStatistics.addValue(quantitativeData.getMin().doubleValue());
42 }
43 if(quantitativeData.getMax()!=null){
44 aggregatedStatistics.addValue(quantitativeData.getMax().doubleValue());
45 }
46 statistics.add(aggregatedStatistics);
47 }
48 Collections.sort(statistics, (s1, s2)->Double.compare(s1.getMean(), s2.getMean()));
49 }
50
51 public List<DescriptiveStatistics> getStatistics() {
52 return statistics;
53 }
54
55 }