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.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import org.hl7.fhir.instance.model.api.IBase;
30  import org.hl7.fhir.instance.model.api.IBaseResource;
31  
32  import ca.uhn.fhir.model.api.annotation.Child;
33  import ca.uhn.fhir.model.api.annotation.Description;
34  import ca.uhn.fhir.model.base.composite.BaseContainedDt;
35  
36  public class RuntimeChildContainedResources extends BaseRuntimeDeclaredChildDefinition {
37  
38  	private BaseRuntimeElementDefinition<?> myElem;
39  
40  	RuntimeChildContainedResources(Field theField, Child theChildAnnotation, Description theDescriptionAnnotation, String theElementName) throws ConfigurationException {
41  		super(theField, theChildAnnotation, theDescriptionAnnotation, theElementName);
42  	}
43  
44  	@Override
45  	public int getMax() {
46  		return Child.MAX_UNLIMITED;
47  	}
48  
49  	@Override
50  	public int getMin() {
51  		return 0;
52  	}
53  
54  	@Override
55  	public BaseRuntimeElementDefinition<?> getChildByName(String theName) {
56  		assert theName.equals(getElementName());
57  		return myElem;
58  	}
59  
60  	@Override
61  	public BaseRuntimeElementDefinition<?> getChildElementDefinitionByDatatype(Class<? extends IBase> theType) {
62  		return myElem;
63  	}
64  
65  	@Override
66  	public String getChildNameByDatatype(Class<? extends IBase> theType) {
67  		return getElementName();
68  	}
69  
70  	@Override
71  	public Set<String> getValidChildNames() {
72  		return Collections.singleton(getElementName());
73  	}
74  
75  	@Override
76  	void sealAndInitialize(FhirContext theContext, Map<Class<? extends IBase>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
77  		Class<?> actualType = theContext.getVersion().getContainedType();
78  		if (BaseContainedDt.class.isAssignableFrom(actualType)) {
79  			@SuppressWarnings("unchecked")
80  			Class<? extends BaseContainedDt> type = (Class<? extends BaseContainedDt>) actualType;
81  			myElem = new RuntimeElemContainedResources(type, false);
82  		} else if (List.class.isAssignableFrom(actualType)) {
83  			myElem = new RuntimeElemContainedResourceList(IBaseResource.class, false);
84  		} else {
85  			throw new ConfigurationException("Fhir Version definition returned invalid contained type: " + actualType);
86  		}
87  	}
88  
89  }