View Javadoc
1   package ca.uhn.fhir.context;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.lang.reflect.Field;
24  import java.lang.reflect.Modifier;
25  import java.util.Collections;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.hl7.fhir.instance.model.api.IBase;
30  
31  import ca.uhn.fhir.model.api.annotation.Child;
32  import ca.uhn.fhir.model.api.annotation.Description;
33  
34  public abstract class BaseRuntimeChildDatatypeDefinition extends BaseRuntimeDeclaredChildDefinition {
35  
36  	private Class<? extends IBase> myDatatype;
37  
38  	private BaseRuntimeElementDefinition<?> myElementDefinition;
39  
40  	public BaseRuntimeChildDatatypeDefinition(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation, Class<? extends IBase> theDatatype) {
41  		super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
42  		// should use RuntimeChildAny
43  		assert Modifier.isInterface(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here";
44  		assert Modifier.isAbstract(theDatatype.getModifiers()) == false : "Type of " + theDatatype + " shouldn't be here";
45  		myDatatype = theDatatype;
46  	}
47  
48  	/**
49  	 * If this child has a bound type, this method will return the Enum type that
50  	 * it is bound to. Otherwise, will return <code>null</code>.
51  	 */
52  	public Class<? extends Enum<?>> getBoundEnumType() {
53  		return null;
54  	}
55  
56  	@Override
57  	public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
58  		if (getElementName().equals(theName)) {
59  			return myElementDefinition;
60  		}
61  		return null;
62  	}
63  
64  	@Override
65  	public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theDatatype) {
66  		Class<?> nextType = theDatatype;
67  		while (nextType.equals(Object.class) == false) {
68  			if (myDatatype.equals(nextType)) {
69  				return myElementDefinition;
70  			}
71  			nextType = nextType.getSuperclass();
72  		}
73  		return null;
74  	}
75  
76  	@Override
77  	public String getChildNameByDatatype(Class<? extends IBase> theDatatype) {
78  		Class<?> nextType = theDatatype;
79  		while (nextType.equals(Object.class) == false) {
80  			if (myDatatype.equals(nextType)) {
81  				return getElementName();
82  			}
83  			nextType = nextType.getSuperclass();
84  		}
85  		return null;
86  	}
87  
88  	public Class<? extends IBase> getDatatype() {
89  		return myDatatype;
90  	}
91  
92  	@Override
93  	public Set<String> getValidChildNames() {
94  		return Collections.singleton(getElementName());
95  	}
96  
97  	@Override
98  	void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
99  		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 }