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.ConversionFailedException;
19  import org.rblasch.convert.MetaConverter;
20  import org.rblasch.convert.assertion.Assertion;
21  import org.rblasch.convert.converters.AbstractConverter;
22  import org.rblasch.convert.type.ClassType;
23  import org.rblasch.convert.type.PrimitiveType;
24  import org.rblasch.convert.type.Type;
25  import org.rblasch.convert.type.Types;
26  
27  import java.lang.reflect.Array;
28  
29  /***
30   * @author Ronald Blaschke
31   */
32  public class ArrayConverter extends AbstractConverter {
33      private final Type sourceCType, destinationCType;
34      private final MetaConverter cConverter;
35  
36      public ArrayConverter(final Type sourceCType, final Type destinationCType, final MetaConverter cConverter) {
37          this.sourceCType = sourceCType;
38          this.destinationCType = destinationCType;
39          this.cConverter = cConverter;
40      }
41  
42      public Type getSourceType() {
43          return Types.findPTypeByType(Types.findTypeByName("array"), sourceCType);
44      }
45  
46      public Type getDestinationType() {
47          return Types.findPTypeByType(Types.findTypeByName("array"), destinationCType);
48      }
49  
50      public int getWeight() {
51          return 100;
52      }
53  
54      public Object convert(final Object obj) throws Exception {
55          Assertion.ensure(sourceCType instanceof ClassType
56          || sourceCType instanceof PrimitiveType, "source component must be class or primitive");
57          Assertion.ensure(destinationCType instanceof ClassType
58          || destinationCType instanceof PrimitiveType, "destination component must be class or primitive");
59  
60          final Object in = obj;
61          final Class outComponentClass;
62          if (destinationCType instanceof ClassType) {
63              outComponentClass = ((ClassType) destinationCType).getTheClass();
64          } else if (destinationCType instanceof PrimitiveType) {
65              outComponentClass = ((PrimitiveType) destinationCType).getTheClass();
66          } else {
67              throw new ConversionFailedException("Illegal source component type " + sourceCType);
68          }
69  
70          final Object out = Array.newInstance(outComponentClass, Array.getLength(in));
71          for (int i = 0; i < Array.getLength(in); ++i) {
72              Array.set(out, i, cConverter.getConverter().convert(Array.get(in, i)));
73          }
74  
75          return out;
76      }
77  }