Stuart McCulloch | 669423b | 2012-06-26 16:34:24 +0000 | [diff] [blame^] | 1 | package aQute.bnd.build.model.conversions; |
| 2 | |
| 3 | import java.util.*; |
| 4 | |
| 5 | import aQute.lib.osgi.*; |
| 6 | import aQute.libg.qtokens.*; |
| 7 | |
| 8 | public class SimpleListConverter<R> implements Converter<List<R>,String> { |
| 9 | |
| 10 | private Converter< ? extends R, ? super String> itemConverter; |
| 11 | |
| 12 | public static <R> Converter<List<R>,String> create(Converter<R, ? super String> itemConverter) { |
| 13 | return new SimpleListConverter<R>(itemConverter); |
| 14 | } |
| 15 | |
| 16 | public static Converter<List<String>,String> create() { |
| 17 | return new SimpleListConverter<String>(new NoopConverter<String>()); |
| 18 | } |
| 19 | |
| 20 | private SimpleListConverter(Converter< ? extends R, ? super String> itemConverter) { |
| 21 | this.itemConverter = itemConverter; |
| 22 | } |
| 23 | |
| 24 | public List<R> convert(String input) throws IllegalArgumentException { |
| 25 | List<R> result = new ArrayList<R>(); |
| 26 | |
| 27 | if (Constants.EMPTY_HEADER.equalsIgnoreCase(input.trim())) |
| 28 | return result; |
| 29 | |
| 30 | QuotedTokenizer qt = new QuotedTokenizer(input, ","); |
| 31 | String token = qt.nextToken(); |
| 32 | |
| 33 | while (token != null) { |
| 34 | result.add(itemConverter.convert(token.trim())); |
| 35 | token = qt.nextToken(); |
| 36 | } |
| 37 | |
| 38 | return result; |
| 39 | } |
| 40 | |
| 41 | } |