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.Iterator;
25  import java.util.List;
26  
27  /***
28   * @author Ronald Blaschke
29   */
30  public class ListConverter extends AbstractConverter {
31      private final Type sourceCType, destinationCType;
32      private final MetaConverter cConverter;
33  
34      public ListConverter(final Type sourceCType, final Type destinationCType, final MetaConverter cConverter) {
35          this.sourceCType = sourceCType;
36          this.destinationCType = destinationCType;
37          this.cConverter = cConverter;
38      }
39  
40      public Type getSourceType() {
41          return Types.findPTypeByType(Types.findTypeByClass(List.class), sourceCType);
42      }
43  
44      public Type getDestinationType() {
45          return Types.findPTypeByType(Types.findTypeByClass(ArrayList.class), destinationCType);
46      }
47  
48      public int getWeight() {
49          return 100;
50      }
51  
52      public Object convert(final Object obj) throws Exception {
53          final List in = (List) obj;
54  
55          final List out = new ArrayList(in.size());
56          for (final Iterator i = in.iterator(); i.hasNext();) {
57              out.add(cConverter.getConverter().convert(i.next()));
58          }
59  
60          return out;
61      }
62  }