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