Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
public class CodelistBeanImpl extends ItemSchemeBeanImpl<CodeBean> implements CodelistBean {
private static final long serialVersionUID = 1L;

private final Map<String, CodeBean> codeMap;

///////////////////////////////////////////////////////////////////////////////////////////////////
////////////BUILD FROM ITSELF, CREATES STUB BEAN //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
private CodelistBeanImpl(CodelistBean bean, URL actualLocation, boolean isServiceUrl) {
super(bean, actualLocation, isServiceUrl);
this.codeMap = new HashMap<>();
}

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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<CodeBean> children;
if (parentChildMap.containsKey(parentCode)) {
children = parentChildMap.get(parentCode);
Expand Down Expand Up @@ -296,13 +310,12 @@ private void recurseParentMap(Set<CodeBean> children, CodeBean parentCode, Map<C
}
}

private CodeBean getCode(List<CodeBean> 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;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,20 +16,14 @@
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;
private CodelistSuperBean codelistSuperBean;

@BeforeEach
public void setup() {
codelistBean = buildCodelist(10).getImmutableInstance();
codelistBean = buildCodelist(5_000).getImmutableInstance();
codelistSuperBean = new CodelistSuperBeanImpl(codelistBean);
}

Expand All @@ -40,7 +41,7 @@ public void shouldCheckExceptionForInvalidCodeId() {
public void shouldCheckNumberOfCodes() {
List<CodeBean> codesWithoutParent = codelistBean.getItems().stream()
.filter(code -> code.getParentCode() == null)
.collect(toList());
.toList();

assertEquals(codesWithoutParent.size(), codelistSuperBean.getCodes().size());
}
Expand Down
Loading