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