List<ResourceReferenceDt> allElements = myContext.newTerser().getAllPopulatedChildElementsOfType(theResource, ResourceReferenceDt.class);
Set<String> allIds = new HashSet<String>();
for (ResourceReferenceDt next : allElements) {
IResource resource = next.getResource();
if (resource != null) {
if (resource.getId().isEmpty()) {
resource.setId(new IdDt(myNextContainedId++));
// resource.setId(new IdDt(UUID.randomUUID().toString()));
}
if (!allIds.contains(resource.getId().getValue())) {
theTarget.getContained().getContainedResources().add(resource);
allIds.add(resource.getId().getValue());
}
next.setReference("#" + resource.getId().getValue());
containResourcesForEncoding(resource, theTarget);
}
}