| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ArrayConverter |
|
| 2.6666666666666665;2.667 |
| 1 | /* | |
| 2 | * Copyright 2003-2004 The Apache Software Foundation | |
| 3 | * | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | package org.apache.commons.convert1.array; | |
| 17 | ||
| 18 | import java.lang.reflect.Array; | |
| 19 | import java.util.List; | |
| 20 | import java.util.Iterator; | |
| 21 | ||
| 22 | import org.apache.commons.convert1.ConversionException; | |
| 23 | import org.apache.commons.convert1.Converter; | |
| 24 | ||
| 25 | import org.apache.commons.convert1.ConvertRegistry; | |
| 26 | ||
| 27 | ||
| 28 | /** | |
| 29 | * <p>Standard {@link Converter} implementation that converts an incoming | |
| 30 | * comma delimited String into a known Java class via another Converter. | |
| 31 | * | |
| 32 | * @author Henri Yandell | |
| 33 | * @version $Id: ArrayConverter.java 155441 2005-02-26 13:19:22Z dirkv $ | |
| 34 | * @since 0.1 | |
| 35 | */ | |
| 36 | public class ArrayConverter implements Converter { | |
| 37 | ||
| 38 | private ConvertRegistry registry; | |
| 39 | private Converter converter; | |
| 40 | ||
| 41 | /** | |
| 42 | * Handles multiple types of sub-type by reusing a | |
| 43 | * particular registry for the sub-elements. | |
| 44 | */ | |
| 45 | 0 | public ArrayConverter(ConvertRegistry registry) { |
| 46 | 0 | this.registry = registry; |
| 47 | 0 | } |
| 48 | ||
| 49 | /** | |
| 50 | * Handles only one type of element converter. | |
| 51 | */ | |
| 52 | 0 | public ArrayConverter(Converter converter) { |
| 53 | 0 | this.converter = converter; |
| 54 | 0 | } |
| 55 | ||
| 56 | /** | |
| 57 | * Convert the specified input object into an output object of the | |
| 58 | * specified type. | |
| 59 | * | |
| 60 | * @param type Data type to which this value should be converted | |
| 61 | * @param value The input value to be converted | |
| 62 | * | |
| 63 | * @exception ConversionException if conversion cannot be performed | |
| 64 | * successfully | |
| 65 | */ | |
| 66 | public Object convert(Class toClass, Object value) { | |
| 67 | ||
| 68 | 0 | if (value == null) { |
| 69 | 0 | return null; // ignore defaults for the moment |
| 70 | } | |
| 71 | ||
| 72 | // parse the comma delimitation. | |
| 73 | //H? can we abstract this somehow? | |
| 74 | // An ElementProvider? | |
| 75 | 0 | List elements = StringArrayParser.parseElements(value.toString()); |
| 76 | ||
| 77 | // assume that toClass is an array?? | |
| 78 | 0 | if( toClass.isArray() ) { |
| 79 | 0 | toClass = toClass.getComponentType(); |
| 80 | } | |
| 81 | 0 | Object array = Array.newInstance( toClass, elements.size() ); |
| 82 | ||
| 83 | 0 | if(registry != null) { |
| 84 | 0 | converter = this.registry.lookup( String.class, toClass ); |
| 85 | } | |
| 86 | ||
| 87 | 0 | Iterator itr = elements.iterator(); |
| 88 | 0 | int idx = 0; |
| 89 | 0 | while(itr.hasNext()) { |
| 90 | 0 | Array.set( array, idx, converter.convert( toClass, itr.next() ) ); |
| 91 | 0 | idx++; |
| 92 | } | |
| 93 | ||
| 94 | 0 | return array; |
| 95 | } | |
| 96 | ||
| 97 | } |