001package ca.uhn.fhir.context;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
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.Map;
026import java.util.Set;
027
028import org.hl7.fhir.instance.model.api.IBase;
029
030import ca.uhn.fhir.model.api.annotation.Child;
031import ca.uhn.fhir.model.api.annotation.Description;
032
033public class RuntimeChildResourceBlockDefinition extends BaseRuntimeDeclaredChildDefinition {
034
035        private RuntimeResourceBlockDefinition myElementDef;
036        private Class<? extends IBase> myResourceBlockType;
037
038        public RuntimeChildResourceBlockDefinition(Field theField, Child theChildAnnotation, Description theDescriptionAnnotation, String theElementName, Class<? extends IBase> theResourceBlockType) throws ConfigurationException {
039                super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
040                myResourceBlockType = theResourceBlockType;
041        }
042
043        @Override
044        public RuntimeResourceBlockDefinition getChildByName(String theName) {
045                if (getElementName().equals(theName)) {
046                        return myElementDef;
047                }else {
048                        return null;
049                }
050        }
051
052        @Override
053        public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
054                if (myResourceBlockType.equals(theDatatype)) {
055                        return getElementName();
056                }
057                return null;
058        }
059
060        @Override
061        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
062                if (myResourceBlockType.equals(theDatatype)) {
063                        return myElementDef;
064                }
065                return null;
066        }
067
068        @Override
069        public Set<String> getValidChildNames() {
070                return Collections.singleton(getElementName());
071        }
072
073        @Override
074        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
075                myElementDef = (RuntimeResourceBlockDefinition) theClassToElementDefinitions.get(myResourceBlockType);
076        }
077
078}