fix #10420 order exact values in QuantitativeDataFormatter
authorAndreas Müller <a.mueller@bgbm.org>
Fri, 20 Oct 2023 12:58:29 +0000 (14:58 +0200)
committerAndreas Müller <a.mueller@bgbm.org>
Fri, 20 Oct 2023 12:58:29 +0000 (14:58 +0200)
cdmlib-model/src/main/java/eu/etaxonomy/cdm/format/description/QuantitativeDataFormatter.java
cdmlib-model/src/test/java/eu/etaxonomy/cdm/format/description/QuantitativeDataFormatterTest.java

index c0a0d17f9b14e732504a945156709412a3367842..a548bae2e1d7802373183399183915126ce1733c 100644 (file)
@@ -9,6 +9,8 @@
 package eu.etaxonomy.cdm.format.description;
 
 import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import org.apache.commons.lang3.StringUtils;
@@ -34,6 +36,7 @@ public class QuantitativeDataFormatter
     static final String minSep = UTF8.NARROW_NO_BREAK + sepDash;
     static final String maxSep = sepDash + UTF8.NARROW_NO_BREAK;
     public static final String lowerUpperSep = UTF8.NARROW_NO_BREAK + sepDash + UTF8.NARROW_NO_BREAK;
+    private static final String EXACT_VALUE_SEP = ";";
 
     static final String modifierSep = UTF8.NARROW_NO_BREAK.toString();
 
@@ -101,9 +104,11 @@ public class QuantitativeDataFormatter
             minMax = "<" + upper;
         }
         String exactValueStr = "";
-        for(BigDecimal exactValue : quantData.getExactValues()){
+        List<BigDecimal> exactValues = new ArrayList<>(quantData.getExactValues());
+        Collections.sort(exactValues);
+        for(BigDecimal exactValue : exactValues){
             if (exactValue != null){
-                exactValueStr = CdmUtils.concat(";", exactValueStr, String.valueOf(exactValue));
+                exactValueStr = CdmUtils.concat(EXACT_VALUE_SEP, exactValueStr, String.valueOf(exactValue));
             }
         }
         if (isNotBlank(minMax)){
index 4bfd182440701832bdd5ecf71d9e3ad3c2069ab1..a1eeaa7f9898d40377ccaa252b21d77188ce8044 100644 (file)
@@ -159,4 +159,25 @@ public class QuantitativeDataFormatterTest extends TermTestBase {
         text = formatter.format(quantData, formatKey);
         Assert.assertEquals("data unavailable (0.1"+lowerUpperSep+"1.3 m [n=2])", text);
     }
+
+    @Test
+    public void testOrdering() {
+        QuantitativeData quantData = QuantitativeData.NewInstance(Feature.CHROMOSOME_NUMBER());
+        FormatKey[] formatKey = null;
+        QuantitativeDataFormatter formatter = new QuantitativeDataFormatter(quantData, formatKey);
+
+        StatisticalMeasurementValue val1 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.EXACT_VALUE(), new BigDecimal("2.1"));
+        StatisticalMeasurementValue val2 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.EXACT_VALUE(), new BigDecimal("1.3"));
+        StatisticalMeasurementValue val3 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.EXACT_VALUE(), new BigDecimal("5"));
+        StatisticalMeasurementValue val4 = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.EXACT_VALUE(), new BigDecimal("4"));
+        quantData.addStatisticalValue(val1);
+        quantData.addStatisticalValue(val2);
+        quantData.addStatisticalValue(val3);
+        quantData.addStatisticalValue(val4);
+        MeasurementUnit unit = MeasurementUnit.METER();
+        quantData.setUnit(unit);
+
+        String text = formatter.format(quantData, formatKey);
+        Assert.assertEquals("1.3;2.1;4;5 m", text);
+    }
 }
\ No newline at end of file