View Javadoc
1   package ca.uhn.fhir.rest.param.binder;
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.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import ca.uhn.fhir.context.ConfigurationException;
30  
31  public class CollectionBinder
32  // implements IParamBinder
33  {
34  
35  	/**
36  	 * @param thePositionDescription Just used in exceptions if theCollectionType is invalid
37  	 */
38  	@SuppressWarnings({ "rawtypes", "cast" })
39  	public static Class<? extends Collection> getInstantiableCollectionType(Class<? extends Collection<?>> theCollectionType, String thePositionDescription) {
40  		if (theCollectionType.equals(List.class) || theCollectionType .equals(ArrayList.class)) {
41  			return (Class<? extends Collection>) ArrayList.class;
42  		} else if (theCollectionType .equals( Set.class )|| theCollectionType .equals( HashSet.class)) {
43  			return (Class<? extends Collection>) HashSet.class;
44  		} else if (theCollectionType.equals(Collection.class)) {
45  			return (Class<? extends Collection>) ArrayList.class;
46  		} else {
47  			throw new ConfigurationException("Unsupported binding collection type '" + theCollectionType.getCanonicalName() + "' for " + thePositionDescription);
48  		}
49  	}
50  
51  	// private Class<?> myCollectionType;
52  	// private IParamBinder myWrap;
53  	//
54  	// public CollectionBinder(IParamBinder theWrap, Class<? extends java.util.Collection<?>> theCollectionType) {
55  	// myWrap = theWrap;
56  	// if (theCollectionType == List.class || theCollectionType == ArrayList.class) {
57  	// myCollectionType = ArrayList.class;
58  	// } else if (theCollectionType == Set.class || theCollectionType == HashSet.class) {
59  	// myCollectionType = HashSet.class;
60  	// } else if (theCollectionType == Collection.class) {
61  	// myCollectionType = ArrayList.class;
62  	// } else {
63  	// throw new ConfigurationException("Unsupported binding collection type: " + theCollectionType.getCanonicalName());
64  	// }
65  	// }
66  
67  	// @Override
68  	// public String encode(Object theString) throws InternalErrorException {
69  	// Collection<?> obj = (Collection<?>) theString;
70  	// StringBuilder b = new StringBuilder();
71  	// for (Object object : obj) {
72  	// String next = myWrap.encode(object);
73  	// if (b.length() > 0) {
74  	// b.append(",");
75  	// }
76  	// b.append(next.replace(",", "\\,"));
77  	// }
78  	// return b.toString();
79  	// }
80  	//
81  	// @SuppressWarnings("unchecked")
82  	// @Override
83  	// public Object parse(String theString) throws InternalErrorException {
84  	// Collection<Object> retVal;
85  	// try {
86  	// retVal = (Collection<Object>) myCollectionType.newInstance();
87  	// } catch (Exception e) {
88  	// throw new InternalErrorException("Failed to instantiate " + myCollectionType, e);
89  	// }
90  	//
91  	// List<String> params = QueryUtil.splitQueryStringByCommasIgnoreEscape(theString);
92  	// for (String string : params) {
93  	// Object nextParsed = myWrap.parse(string);
94  	// retVal.add(nextParsed);
95  	// }
96  	//
97  	// return retVal;
98  	// }
99  
100 }