001package ca.uhn.fhir.context; 002 003/* 004 * #%L 005 * HAPI FHIR - Core Library 006 * %% 007 * Copyright (C) 2014 - 2019 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.annotation.Child; 032import ca.uhn.fhir.model.api.annotation.Description; 033 034public abstract class BaseRuntimeChildDatatypeDefinition extends BaseRuntimeDeclaredChildDefinition { 035 036 private Class<? extends IBase> myDatatype; 037 038 private BaseRuntimeElementDefinition<?> myElementDefinition; 039 040 public BaseRuntimeChildDatatypeDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, Class<? extends IBase> theDatatype) { 041 super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName); 042 // should use RuntimeChildAny 043 assert Modifier.isInterface(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here"; 044 assert Modifier.isAbstract(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here"; 045 myDatatype = theDatatype; 046 } 047 048 /** 049 * If this child has a bound type, this method will return the Enum type that 050 * it is bound to. Otherwise, will return <code>null</code>. 051 */ 052 public Class<? extends Enum<?>> getBoundEnumType() { 053 return null; 054 } 055 056 @Override 057 public BaseRuntimeElementDefinition<?> getChildByName(String theName) { 058 if (getElementName().equals(theName)) { 059 return myElementDefinition; 060 } 061 return null; 062 } 063 064 @Override 065 public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { 066 Class<?> nextType = theDatatype; 067 while (nextType.equals(Object.class) == false) { 068 if (myDatatype.equals(nextType)) { 069 return myElementDefinition; 070 } 071 nextType = nextType.getSuperclass(); 072 } 073 return null; 074 } 075 076 @Override 077 public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { 078 Class<?> nextType = theDatatype; 079 while (nextType.equals(Object.class) == false) { 080 if (myDatatype.equals(nextType)) { 081 return getElementName(); 082 } 083 nextType = nextType.getSuperclass(); 084 } 085 return null; 086 } 087 088 public Class<? extends IBase> getDatatype() { 089 return myDatatype; 090 } 091 092 @Override 093 public Set<String> getValidChildNames() { 094 return Collections.singleton(getElementName()); 095 } 096 097 @Override 098 void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { 099 myElementDefinition = theClassToElementDefinitions.get(getDatatype()); 100 if (myElementDefinition == null) { 101 myElementDefinition = theContext.getElementDefinition(getDatatype()); 102 } 103 assert myElementDefinition != null : "Unknown type: " + getDatatype(); 104 } 105 106 @Override 107 public String toString() { 108 return getClass().getSimpleName() + "[" + getElementName() + "]"; 109 } 110 111}