View Javadoc

1   /*
2    * Copyright 2004 Ronald Blaschke.
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.rblasch.convert.converters.collections;
17  
18  import org.rblasch.convert.MetaConverter;
19  import org.rblasch.convert.converters.AbstractConverter;
20  import org.rblasch.convert.type.Type;
21  import org.rblasch.convert.type.Types;
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /***
27   * @author Ronald Blaschke
28   */
29  public class ArrayToArrayListConverter extends AbstractConverter {
30      private final Type sourceCType, destinationCType;
31      private final MetaConverter cConverter;
32  
33      public ArrayToArrayListConverter(final Type sourceCType, final Type destinationCType, final MetaConverter cConverter) {
34          this.sourceCType = sourceCType;
35          this.destinationCType = destinationCType;
36          this.cConverter = cConverter;
37      }
38  
39      public Type getSourceType() {
40          return Types.findPTypeByType(Types.primitiveArrayType(), sourceCType);
41      }
42  
43      public Type getDestinationType() {
44          return Types.findPTypeByType(Types.findTypeByClass(ArrayList.class), destinationCType);
45      }
46  
47      public int getWeight() {
48          return 100;
49      }
50  
51      public Object convert(final Object obj) throws Exception {
52          final Object[] array = (Object[]) obj;
53          final List l = new ArrayList(array.length);
54          for (int i = 0; i < array.length; ++i) {
55              l.add(cConverter.getConverter().convert(array[i]));
56          }
57          return l;
58      }
59  }