Coverage Report - org.apache.camel.impl.converter.ArrayTypeConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayTypeConverter
97% 
100% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.impl.converter;
 18  
 
 19  
 import java.lang.reflect.Array;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Arrays;
 22  
 import java.util.Collection;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.camel.TypeConverter;
 26  
 
 27  
 /**
 28  
  * A type converter which is used to convert to and from array types
 29  
  * particularly for derived types of array component types and dealing with
 30  
  * primitive array types.
 31  
  * 
 32  
  * @version $Revision: $
 33  
  */
 34  351
 public class ArrayTypeConverter implements TypeConverter {
 35  
     public <T> T convertTo(Class<T> type, Object value) {
 36  72
         if (type.isArray()) {
 37  12
             if (value instanceof Collection) {
 38  9
                 Collection collection = (Collection)value;
 39  9
                 Object array = Array.newInstance(type.getComponentType(), collection.size());
 40  9
                 if (array instanceof Object[]) {
 41  6
                     collection.toArray((Object[])array);
 42  6
                 } else {
 43  3
                     int index = 0;
 44  3
                     for (Object element : collection) {
 45  6
                         Array.set(array, index++, element);
 46  6
                     }
 47  
                 }
 48  9
                 return (T)array;
 49  3
             } else if (value != null && value.getClass().isArray()) {
 50  3
                 int size = Array.getLength(value);
 51  3
                 Object answer = Array.newInstance(type.getComponentType(), size);
 52  9
                 for (int i = 0; i < size; i++) {
 53  6
                     Array.set(answer, i, Array.get(value, i));
 54  
                 }
 55  3
                 return (T)answer;
 56  
             }
 57  60
         } else if (Collection.class.isAssignableFrom(type)) {
 58  6
             if (value != null) {
 59  6
                 if (value instanceof Object[]) {
 60  0
                     return (T)Arrays.asList((Object[])value);
 61  6
                 } else if (value.getClass().isArray()) {
 62  3
                     int size = Array.getLength(value);
 63  3
                     List answer = new ArrayList(size);
 64  9
                     for (int i = 0; i < size; i++) {
 65  6
                         answer.add(Array.get(value, i));
 66  
                     }
 67  3
                     return (T)answer;
 68  
                 }
 69  
             }
 70  
         }
 71  57
         return null;
 72  
     }
 73  
 }