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.lang.reflect.Modifier;
025import java.util.Collections;
026import java.util.Map;
027import java.util.Set;
028
029import org.hl7.fhir.instance.model.api.IBase;
030
031import ca.uhn.fhir.model.api.ICodeEnum;
032import ca.uhn.fhir.model.api.annotation.Child;
033import ca.uhn.fhir.model.api.annotation.Description;
034
035public abstract class BaseRuntimeChildDatatypeDefinition extends BaseRuntimeDeclaredChildDefinition {
036
037        private Class<? extends ICodeEnum> myCodeType;
038        private Class<? extends IBase> myDatatype;
039
040        private BaseRuntimeElementDefinition<?> myElementDefinition;
041
042        public BaseRuntimeChildDatatypeDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, Class<? extends IBase> theDatatype) {
043                super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
044                // should use RuntimeChildAny
045                assert Modifier.isInterface(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here";
046                assert Modifier.isAbstract(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here";
047                myDatatype = theDatatype;
048        }
049
050        @Override
051        public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
052                Class<?> nextType = theDatatype;
053                while (nextType.equals(Object.class) == false) {
054                        if (myDatatype.equals(nextType)) {
055                                return getElementName();
056                        }
057                        nextType = nextType.getSuperclass();
058                }
059                return null;
060        }
061
062        @Override
063        public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
064                Class<?> nextType = theDatatype;
065                while (nextType.equals(Object.class) == false) {
066                        if (myDatatype.equals(nextType)) {
067                                return myElementDefinition;
068                        }
069                        nextType = nextType.getSuperclass();
070                }
071                return null;
072        }
073
074        @Override
075        public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
076                if (getElementName().equals(theName)) {
077                        return myElementDefinition;
078                }
079                return null;
080        }
081
082        public Class<? extends ICodeEnum> getCodeType() {
083                return myCodeType;
084        }
085
086        public Class<? extends IBase> getDatatype() {
087                return myDatatype;
088        }
089
090        @Override
091        public Set<String> getValidChildNames() {
092                return Collections.singleton(getElementName());
093        }
094
095        @Override
096        void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
097                myElementDefinition = theClassToElementDefinitions.get(getDatatype());
098                assert myElementDefinition != null : "Unknown type: " + getDatatype();
099        }
100
101        public void setCodeType(Class<? extends ICodeEnum> theType) {
102                if (myElementDefinition != null) {
103                        throw new IllegalStateException("Can not set code type at runtime");
104                }
105                myCodeType = theType;
106        }
107
108        @Override
109        public String toString() {
110                return getClass().getSimpleName() + "[" + getElementName() + "]";
111        }
112
113}