001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.lang.reflect.Field;
024import java.util.Collections;
025import java.util.List;
026import java.util.Map;
027import java.util.Set;
028
029import org.hl7.fhir.instance.model.api.IBase;
030import org.hl7.fhir.instance.model.api.IBaseResource;
031
032import ca.uhn.fhir.model.api.annotation.Child;
033import ca.uhn.fhir.model.api.annotation.Description;
034import ca.uhn.fhir.model.base.composite.BaseContainedDt;
035
036public class RuntimeChildContainedResources extends BaseRuntimeDeclaredChildDefinition {
037
038        private BaseRuntimeElementDefinition<?> myElem;
039
040        RuntimeChildContainedResources(Field theField, Child theChildAnnotation, Description theDescriptionAnnotation, String theElementName) throws ConfigurationException {
041                super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
042        }
043
044        @Override
045        public int getMax() {
046                return Child.MAX_UNLIMITED;
047        }
048
049        @Override
050        public int getMin() {
051                return 0;
052        }
053
054        @Override
055        public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
056                assert theName.equals(getElementName());
057                return myElem;
058        }
059
060        @Override
061        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType) {
062                return myElem;
063        }
064
065        @Override
066        public String getChildNameByDatatype(Class<? extends IBase> theType) {
067                return getElementName();
068        }
069
070        @Override
071        public Set<String> getValidChildNames() {
072                return Collections.singleton(getElementName());
073        }
074
075        @Override
076        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
077                Class<?> actualType = theContext.getVersion().getContainedType();
078                if (BaseContainedDt.class.isAssignableFrom(actualType)) {
079                        @SuppressWarnings("unchecked")
080                        Class<? extends BaseContainedDt> type = (Class<? extends BaseContainedDt>) actualType;
081                        myElem = new RuntimeElemContainedResources(type, false);
082                } else if (List.class.isAssignableFrom(actualType)) {
083                        myElem = new RuntimeElemContainedResourceList(IBaseResource.class, false);
084                } else {
085                        throw new ConfigurationException("Fhir Version definition returned invalid contained type: " + actualType);
086                }
087        }
088
089}