diff --git a/SdmxBeans/src/main/java/org/sdmxsource/sdmx/sdmxbeans/model/beans/codelist/CodelistBeanImpl.java b/SdmxBeans/src/main/java/org/sdmxsource/sdmx/sdmxbeans/model/beans/codelist/CodelistBeanImpl.java index 18c8729..6e5f276 100644 --- a/SdmxBeans/src/main/java/org/sdmxsource/sdmx/sdmxbeans/model/beans/codelist/CodelistBeanImpl.java +++ b/SdmxBeans/src/main/java/org/sdmxsource/sdmx/sdmxbeans/model/beans/codelist/CodelistBeanImpl.java @@ -54,12 +54,14 @@ public class CodelistBeanImpl extends ItemSchemeBeanImpl implements CodelistBean { private static final long serialVersionUID = 1L; + private final Map codeMap; /////////////////////////////////////////////////////////////////////////////////////////////////// ////////////BUILD FROM ITSELF, CREATES STUB BEAN ////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// private CodelistBeanImpl(CodelistBean bean, URL actualLocation, boolean isServiceUrl) { super(bean, actualLocation, isServiceUrl); + this.codeMap = new HashMap<>(); } /** @@ -72,10 +74,13 @@ private CodelistBeanImpl(CodelistBean bean, URL actualLocation, boolean isServic /////////////////////////////////////////////////////////////////////////////////////////////////// public CodelistBeanImpl(CodelistMutableBean codelist) { super(codelist); + codeMap = new HashMap<>(); try { if (codelist.getItems() != null) { - for (CodeMutableBean code : codelist.getItems()) { - this.items.add(new CodeBeanImpl(this, code)); + for (CodeMutableBean mutableCode : codelist.getItems()) { + var code = new CodeBeanImpl(this, mutableCode); + this.items.add(code); + this.codeMap.put(code.getId(), code); } } } catch (SdmxSemmanticException ex) { @@ -134,10 +139,13 @@ public CodelistBeanImpl(org.sdmx.resources.sdmxml.schemas.v10.xmlbeans.structure bean.getNameList(), createTertiary(bean.isSetIsExternalReference(), bean.getIsExternalReference()), bean.getAnnotations()); + this.codeMap = new HashMap<>(); try { for (org.sdmx.resources.sdmxml.schemas.v10.xmlbeans.structure.CodeType currentCode : bean.getCodeList()) { - items.add(new CodeBeanImpl(this, currentCode)); + var code = new CodeBeanImpl(this, currentCode); + items.add(code); + codeMap.put(code.getId(), code); } } catch (SdmxSemmanticException ex) { throw new SdmxSemmanticException(ex, ExceptionCode.BEAN_STRUCTURE_CONSTRUCTION_ERROR, this.getUrn()); @@ -176,10 +184,13 @@ public CodelistBeanImpl(CodeListType bean) { createTertiary(bean.isSetIsExternalReference(), bean.getIsExternalReference()), bean.getAnnotations()); + this.codeMap = new HashMap<>(); try { for (CodeType currentCode : bean.getCodeList()) { - items.add(new CodeBeanImpl(this, currentCode)); + var code = new CodeBeanImpl(this, currentCode); + items.add(code); + codeMap.put(code.getId(), code); } } catch (SdmxSemmanticException ex) { throw new SdmxSemmanticException(ex, ExceptionCode.BEAN_STRUCTURE_CONSTRUCTION_ERROR, this.getUrn()); @@ -205,10 +216,13 @@ public CodelistBeanImpl(CodeListType bean) { /////////////////////////////////////////////////////////////////////////////////////////////////// public CodelistBeanImpl(CodelistType bean) { super(bean, SDMX_STRUCTURE_TYPE.CODE_LIST); + this.codeMap = new HashMap<>(); try { for (org.sdmx.resources.sdmxml.schemas.v21.structure.CodeType currentCode : bean.getCodeList()) { - items.add(new CodeBeanImpl(this, currentCode)); + var code = new CodeBeanImpl(this, currentCode); + items.add(code); + codeMap.put(code.getId(), code); } } catch (SdmxSemmanticException ex) { throw new SdmxSemmanticException(ex, ExceptionCode.BEAN_STRUCTURE_CONSTRUCTION_ERROR, this.getUrn()); @@ -255,7 +269,7 @@ private void validate() throws ValidationException { urns.add(code.getUrn()); if (ObjectUtil.validString(code.getParentCode())) { - CodeBean parentCode = getCode(items, code.getParentCode()); + CodeBean parentCode = getCode(code.getParentCode()); Set children; if (parentChildMap.containsKey(parentCode)) { children = parentChildMap.get(parentCode); @@ -296,13 +310,12 @@ private void recurseParentMap(Set children, CodeBean parentCode, Map codes, String id) { - for (CodeBean currentCode : codes) { - if (currentCode.getId().equals(id)) { - return currentCode; - } + private CodeBean getCode(String id) { + CodeBean code = getCodeById(id); + if (code == null) { + throw new SdmxSemmanticException(ExceptionCode.CAN_NOT_RESOLVE_PARENT, id); } - throw new SdmxSemmanticException(ExceptionCode.CAN_NOT_RESOLVE_PARENT, id); + return code; } /////////////////////////////////////////////////////////////////////////////////////////////////// @@ -321,11 +334,6 @@ public CodelistMutableBean getMutableInstance() { @Override public CodeBean getCodeById(String id) { - for (CodeBean currentCode : items) { - if (currentCode.getId().equals(id)) { - return currentCode; - } - } - return null; + return codeMap.get(id); } } diff --git a/SdmxBeans/src/test/java/org/sdmxsource/sdmx/sdmxbeans/test/CodeListSuperBeanTest.java b/SdmxBeans/src/test/java/org/sdmxsource/sdmx/sdmxbeans/test/CodeListSuperBeanTest.java index d051bc2..646be89 100644 --- a/SdmxBeans/src/test/java/org/sdmxsource/sdmx/sdmxbeans/test/CodeListSuperBeanTest.java +++ b/SdmxBeans/src/test/java/org/sdmxsource/sdmx/sdmxbeans/test/CodeListSuperBeanTest.java @@ -1,5 +1,12 @@ package org.sdmxsource.sdmx.sdmxbeans.test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.sdmxsource.sdmx.sdmxbeans.data.DataHelper.buildCodelist; + +import java.util.List; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.sdmxsource.sdmx.api.model.beans.codelist.CodeBean; @@ -9,12 +16,6 @@ import org.sdmxsource.sdmx.sdmxbeans.model.mutablesuperbeans.codelist.CodelistMutableSuperBeanImpl; import org.sdmxsource.sdmx.sdmxbeans.model.superbeans.codelist.CodelistSuperBeanImpl; -import java.util.List; - -import static java.util.stream.Collectors.toList; -import static org.junit.jupiter.api.Assertions.*; -import static org.sdmxsource.sdmx.sdmxbeans.data.DataHelper.buildCodelist; - public class CodeListSuperBeanTest { private CodelistBean codelistBean; @@ -22,7 +23,7 @@ public class CodeListSuperBeanTest { @BeforeEach public void setup() { - codelistBean = buildCodelist(10).getImmutableInstance(); + codelistBean = buildCodelist(5_000).getImmutableInstance(); codelistSuperBean = new CodelistSuperBeanImpl(codelistBean); } @@ -40,7 +41,7 @@ public void shouldCheckExceptionForInvalidCodeId() { public void shouldCheckNumberOfCodes() { List codesWithoutParent = codelistBean.getItems().stream() .filter(code -> code.getParentCode() == null) - .collect(toList()); + .toList(); assertEquals(codesWithoutParent.size(), codelistSuperBean.getCodes().size()); }