View Javadoc
1   package ca.uhn.fhir.context;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 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.ArrayList;
25  import java.util.Collections;
26  import java.util.Comparator;
27  import java.util.List;
28  import java.util.Map;
29  
30  import ca.uhn.fhir.model.api.IDatatype;
31  import ca.uhn.fhir.model.api.IElement;
32  import ca.uhn.fhir.model.api.IResource;
33  import ca.uhn.fhir.model.api.annotation.Child;
34  import ca.uhn.fhir.model.api.annotation.Description;
35  import ca.uhn.fhir.model.primitive.XhtmlDt;
36  
37  public class RuntimeChildAny extends RuntimeChildChoiceDefinition {
38  
39  	public RuntimeChildAny(Field theField, String theElementName, Child theChildAnnotation, Description theDescriptionAnnotation) {
40  		super(theField, theElementName, theChildAnnotation, theDescriptionAnnotation);
41  	}
42  
43  	@Override
44  	void sealAndInitialize(Map<Class<? extends IElement>, BaseRuntimeElementDefinition<?>> theClassToElementDefinitions) {
45  		List<Class<? extends IElement>> choiceTypes = new ArrayList<Class<? extends IElement>>();
46  		
47  		for (Class<? extends IElement> next : theClassToElementDefinitions.keySet()) {
48  			if (next.equals(XhtmlDt.class)) {
49  				continue;
50  			}
51  			if (IResource.class.isAssignableFrom(next) || IDatatype.class.isAssignableFrom(next)) {
52  				choiceTypes.add(next);
53  			}
54  		}
55  		Collections.sort(choiceTypes,new Comparator<Class<?>>(){
56  			@Override
57  			public int compare(Class<?> theO1, Class<?> theO2) {
58  				boolean o1res = IResource.class.isAssignableFrom(theO1);
59  				boolean o2res = IResource.class.isAssignableFrom(theO2);
60  				if (o1res && o2res) {
61  					return theO1.getSimpleName().compareTo(theO2.getSimpleName());
62  				} else if (o1res) {
63  					return -1;
64  				} else if (o1res == false && o2res == false) {
65  					return 0;
66  				}else {
67  					return 1;
68  				}
69  			}});
70  		
71  		setChoiceTypes(choiceTypes);
72  		
73  		super.sealAndInitialize(theClassToElementDefinitions);
74  	}
75  
76  	
77  	
78  }