001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.rest.param.binder; 021 022import ca.uhn.fhir.context.ConfigurationException; 023import ca.uhn.fhir.i18n.Msg; 024 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.HashSet; 028import java.util.List; 029import java.util.Set; 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(Msg.code(1956) + "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(Msg.code(1957) + "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(Msg.code(1958) + "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}