hapifhir#1699 set index when adding children

instead of calling numberChildren() recursively whenever an element is
added.
This commit is contained in:
mrunibe 2024-11-02 18:02:11 +01:00
parent f909a0c228
commit 8d780e0784
2 changed files with 33 additions and 13 deletions

View File

@ -475,7 +475,7 @@ public class Element extends Base implements NamedItem {
} else {
Element ne = new Element(child).setFormat(format);
children.add(ne);
numberChildren();
ne.index = children.getSizeByName(ne.getListName()) - 1;
childForValue = ne;
break;
}
@ -563,6 +563,7 @@ public class Element extends Base implements NamedItem {
} else {
Element ne = new Element(child).setFormat(format);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
}
}
@ -572,6 +573,7 @@ public class Element extends Base implements NamedItem {
if (p.getName().equals(name)) {
Element ne = new Element(name, p).setFormat(format);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
} else if (p.getDefinition().isChoice() && name.startsWith(p.getName().replace("[x]", ""))) {
String type = name.substring(p.getName().length()-3);
@ -581,6 +583,7 @@ public class Element extends Base implements NamedItem {
Element ne = new Element(name, p).setFormat(format);
ne.setType(type);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
}
@ -604,6 +607,7 @@ public class Element extends Base implements NamedItem {
if (p.getName().equals(name)) {
Element ne = new Element(name, p).setFormat(format);
children.add(ne);
ne.index = children.getSizeByName(ne.getListName()) - 1;
return ne;
}
}
@ -950,8 +954,6 @@ public class Element extends Base implements NamedItem {
child.sort();
if (child.isEmpty())
remove.add(child);
else
child.numberChildren();
}
children.removeAll(remove);
children.sort(new ElementSortComparator(this, this.property));

View File

@ -53,12 +53,12 @@ public class NamedItemList<T extends org.hl7.fhir.utilities.NamedItemList.NamedI
@Override
public boolean add(T e) {
map = null;
addToMap(e);
return list.add(e);
}
public void add(int index, T e) {
addToMap(e);
list.add(index, e);
map = null;
}
@Override
@ -74,7 +74,9 @@ public class NamedItemList<T extends org.hl7.fhir.utilities.NamedItemList.NamedI
@Override
public boolean addAll(Collection<? extends T> c) {
map = null;
for(T e : c) {
addToMap(e);
}
return list.addAll(c);
}
@ -115,6 +117,14 @@ public class NamedItemList<T extends org.hl7.fhir.utilities.NamedItemList.NamedI
}
return res;
}
public int getSizeByName(String name) {
if (map == null) {
buildMap();
}
List<T> l = map.get(name);
return l == null ? 0 : l.size();
}
public T get(int c) {
return list.get(c);
@ -123,15 +133,23 @@ public class NamedItemList<T extends org.hl7.fhir.utilities.NamedItemList.NamedI
private void buildMap() {
map = new HashMap<>();
for (T child : list) {
String n = child.getListName();
List<T> l = map.get(n);
if (l == null) {
l = new ArrayList<>();
map.put(n,l);
}
l.add(child);
addToMap(child);
}
}
private void addToMap(T child) {
if (map == null) {
// map will be re-built anyway in next call to getByName
return;
}
String n = child.getListName();
List<T> l = map.get(n);
if (l == null) {
l = new ArrayList<>();
map.put(n,l);
}
l.add(child);
}
public void sort(Comparator<? super T> sorter) {
Collections.sort(list, sorter);