diff --git a/api/src/main/java/org/openmrs/ConceptNumeric.java b/api/src/main/java/org/openmrs/ConceptNumeric.java index d4ec34ae46b..ae84af36d8d 100644 --- a/api/src/main/java/org/openmrs/ConceptNumeric.java +++ b/api/src/main/java/org/openmrs/ConceptNumeric.java @@ -10,6 +10,7 @@ package org.openmrs; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.Set; import java.util.TreeSet; @@ -58,7 +59,7 @@ public class ConceptNumeric extends Concept { /** default constructor */ public ConceptNumeric() { - referenceRanges = new HashSet<>(); + referenceRanges = new LinkedHashSet<>(); } /** @@ -132,7 +133,7 @@ public ConceptNumeric(Concept c) { this.units = ""; this.allowDecimal = false; - referenceRanges = new HashSet<>(); + referenceRanges = new LinkedHashSet<>(); } // Property accessors diff --git a/api/src/test/java/org/openmrs/ConceptNumericTest.java b/api/src/test/java/org/openmrs/ConceptNumericTest.java index 9a2f053f998..f901c8b4688 100644 --- a/api/src/test/java/org/openmrs/ConceptNumericTest.java +++ b/api/src/test/java/org/openmrs/ConceptNumericTest.java @@ -19,6 +19,9 @@ import org.openmrs.api.context.Context; import org.openmrs.test.jupiter.BaseContextSensitiveTest; +import java.util.ArrayList; +import java.util.List; + /** * Tests the {@link ConceptNumeric} object */ @@ -148,4 +151,33 @@ public void shouldRemoveReferenceRangeFromConceptNumeric() { Context.getConceptService().saveConcept(cn); assertEquals(1, Context.getConceptService().getConceptNumeric(22).getReferenceRanges().size()); } + + @Test + public void shouldMaintainInsertionOrderOfReferenceRangesWithConstructor() { + Concept concept = new Concept(); + concept.setConceptId(1); + + ConceptNumeric conceptNumeric = new ConceptNumeric(concept); + + ConceptReferenceRange referenceRange1 = new ConceptReferenceRange(); + referenceRange1.setId(1); + ConceptReferenceRange referenceRange2 = new ConceptReferenceRange(); + referenceRange2.setId(2); + ConceptReferenceRange referenceRange3 = new ConceptReferenceRange(); + referenceRange3.setId(3); + ConceptReferenceRange referenceRange4 = new ConceptReferenceRange(); + referenceRange4.setId(4); + + conceptNumeric.addReferenceRange(referenceRange1); + conceptNumeric.addReferenceRange(referenceRange2); + conceptNumeric.addReferenceRange(referenceRange3); + conceptNumeric.addReferenceRange(referenceRange4); + + List referenceRangeList = new ArrayList<>(conceptNumeric.getReferenceRanges()); + + assertEquals(1, referenceRangeList.get(0).getId().intValue()); + assertEquals(2, referenceRangeList.get(1).getId().intValue()); + assertEquals(3, referenceRangeList.get(2).getId().intValue()); + assertEquals(4, referenceRangeList.get(3).getId().intValue()); + } }