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 org.apache.camel.TypeConverter;
 20  
 
 21  
 import java.lang.reflect.Array;
 22  
 import java.util.ArrayList;
 23  
 import java.util.Arrays;
 24  
 import java.util.Collection;
 25  
 import java.util.List;
 26  
 
 27  
 /**
 28  
  * A type converter which is used to convert to and from array types particularly for derived types of array
 29  
  * component types and dealing with primitive array types.
 30  
  *
 31  
  * @version $Revision: $
 32  
  */
 33  58
 public class ArrayTypeConverter implements TypeConverter {
 34  
     public <T> T convertTo(Class<T> type, Object value) {
 35  14
         if (type.isArray()) {
 36  4
             if (value instanceof Collection) {
 37  3
                 Collection collection = (Collection) value;
 38  3
                 Object array = Array.newInstance(type.getComponentType(), collection.size());
 39  3
                 if (array instanceof Object[]) {
 40  2
                     collection.toArray((Object[]) array);
 41  2
                 }
 42  
                 else {
 43  1
                     int index = 0;
 44  1
                     for (Object element : collection) {
 45  2
                         Array.set(array, index++, element);
 46  2
                     }
 47  
                 }
 48  3
                 return (T) array;
 49  
             }
 50  1
             else if (value != null && value.getClass().isArray()) {
 51  1
                 int size = Array.getLength(value);
 52  1
                 Object answer = Array.newInstance(type.getComponentType(), size);
 53  3
                 for (int i = 0; i < size; i++) {
 54  2
                     Array.set(answer, i, Array.get(value, i));
 55  
                 }
 56  1
                 return (T) answer;
 57  
             }
 58  
         }
 59  10
         else if (Collection.class.isAssignableFrom(type)) {
 60  1
             if (value != null) {
 61  1
                 if (value instanceof Object[]) {
 62  0
                     return (T) Arrays.asList((Object[]) value);
 63  
                 }
 64  1
                 else if (value.getClass().isArray()) {
 65  1
                     int size = Array.getLength(value);
 66  1
                     List answer = new ArrayList(size);
 67  3
                     for (int i = 0; i < size; i++) {
 68  2
                         answer.add(Array.get(value, i));
 69  
                     }
 70  1
                     return (T) answer;
 71  
                 }
 72  
             }
 73  
         }
 74  9
         return null;
 75  
     }
 76  
 }