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