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 java.lang.reflect.Field; 023import java.lang.reflect.Modifier; 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; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035public abstract class BaseRuntimeChildDatatypeDefinition extends BaseRuntimeDeclaredChildDefinition { 036 Logger ourLog = LoggerFactory.getLogger(BaseRuntimeChildDatatypeDefinition.class); 037 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 /** 051 * If this child has a bound type, this method will return the Enum type that 052 * it is bound to. Otherwise, will return <code>null</code>. 053 */ 054 public Class<? extends Enum<?>> getBoundEnumType() { 055 return null; 056 } 057 058 @Override 059 public BaseRuntimeElementDefinition<?> getChildByName(String theName) { 060 if (getElementName().equals(theName)) { 061 return myElementDefinition; 062 } 063 return null; 064 } 065 066 @Override 067 public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) { 068 Class<?> nextType = theDatatype; 069 while (nextType.equals(Object.class) == false) { 070 if (myDatatype.equals(nextType)) { 071 return myElementDefinition; 072 } 073 nextType = nextType.getSuperclass(); 074 } 075 return null; 076 } 077 078 @Override 079 public String getChildNameByDatatype(Class<? extends IBase> theDatatype) { 080 Class<?> nextType = theDatatype; 081 while (nextType.equals(Object.class) == false) { 082 if (myDatatype.equals(nextType)) { 083 return getElementName(); 084 } 085 nextType = nextType.getSuperclass(); 086 } 087 return null; 088 } 089 090 public Class<? extends IBase> getDatatype() { 091 return myDatatype; 092 } 093 094 @Override 095 public Set<String> getValidChildNames() { 096 return Collections.singleton(getElementName()); 097 } 098 099 @Override 100 void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) { 101 myElementDefinition = theClassToElementDefinitions.get(getDatatype()); 102 if (myElementDefinition == null) { 103 myElementDefinition = theContext.getElementDefinition(getDatatype()); 104 } 105 assert myElementDefinition != null : "Unknown type: " + getDatatype(); 106 } 107 108 @Override 109 public String toString() { 110 return getClass().getSimpleName() + "[" + getElementName() + "]"; 111 } 112 113}