001package ca.uhn.fhir.rest.param.binder; 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.util.ArrayList; 024import java.util.Collection; 025import java.util.HashSet; 026import java.util.List; 027import java.util.Set; 028 029import ca.uhn.fhir.context.ConfigurationException; 030 031public class CollectionBinder 032// implements IParamBinder 033{ 034 035 /** 036 * @param thePositionDescription Just used in exceptions if theCollectionType is invalid 037 */ 038 @SuppressWarnings({ "rawtypes", "cast" }) 039 public static Class<? extends Collection> getInstantiableCollectionType(Class<? extends Collection<?>> theCollectionType, String thePositionDescription) { 040 if (theCollectionType.equals(List.class) || theCollectionType .equals(ArrayList.class)) { 041 return (Class<? extends Collection>) ArrayList.class; 042 } else if (theCollectionType .equals( Set.class )|| theCollectionType .equals( HashSet.class)) { 043 return (Class<? extends Collection>) HashSet.class; 044 } else if (theCollectionType.equals(Collection.class)) { 045 return (Class<? extends Collection>) ArrayList.class; 046 } else { 047 throw new ConfigurationException("Unsupported binding collection type '" + theCollectionType.getCanonicalName() + "' for " + thePositionDescription); 048 } 049 } 050 051 // private Class<?> myCollectionType; 052 // private IParamBinder myWrap; 053 // 054 // public CollectionBinder(IParamBinder theWrap, Class<? extends java.util.Collection<?>> theCollectionType) { 055 // myWrap = theWrap; 056 // if (theCollectionType == List.class || theCollectionType == ArrayList.class) { 057 // myCollectionType = ArrayList.class; 058 // } else if (theCollectionType == Set.class || theCollectionType == HashSet.class) { 059 // myCollectionType = HashSet.class; 060 // } else if (theCollectionType == Collection.class) { 061 // myCollectionType = ArrayList.class; 062 // } else { 063 // throw new ConfigurationException("Unsupported binding collection type: " + theCollectionType.getCanonicalName()); 064 // } 065 // } 066 067 // @Override 068 // public String encode(Object theString) throws InternalErrorException { 069 // Collection<?> obj = (Collection<?>) theString; 070 // StringBuilder b = new StringBuilder(); 071 // for (Object object : obj) { 072 // String next = myWrap.encode(object); 073 // if (b.length() > 0) { 074 // b.append(","); 075 // } 076 // b.append(next.replace(",", "\\,")); 077 // } 078 // return b.toString(); 079 // } 080 // 081 // @SuppressWarnings("unchecked") 082 // @Override 083 // public Object parse(String theString) throws InternalErrorException { 084 // Collection<Object> retVal; 085 // try { 086 // retVal = (Collection<Object>) myCollectionType.newInstance(); 087 // } catch (Exception e) { 088 // throw new InternalErrorException("Failed to instantiate " + myCollectionType, e); 089 // } 090 // 091 // List<String> params = QueryUtil.splitQueryStringByCommasIgnoreEscape(theString); 092 // for (String string : params) { 093 // Object nextParsed = myWrap.parse(string); 094 // retVal.add(nextParsed); 095 // } 096 // 097 // return retVal; 098 // } 099 100}