View Javadoc
1   /*
2    * Copyright 2016 Andrew Rucker Jones.
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 com.opencsv.bean.mocks;
17  
18  import com.opencsv.bean.AbstractBeanField;
19  import com.opencsv.exceptions.CsvDataTypeMismatchException;
20  
21  import java.math.BigDecimal;
22  import java.math.BigInteger;
23  
24  /**
25   * A basic custom mapper that maps all primitives and wrapped primitives.
26   * Always returns the same values, regardless of the input.
27   *
28   * @param <T> Type of the bean
29   * @param <I> Type of the index into a multivalued field
30   * @author Andrew Rucker Jones
31   */
32  public class CustomTestMapper<T, I> extends AbstractBeanField<T, I> {
33      @Override
34      protected Object convert(String value)
35              throws CsvDataTypeMismatchException {
36          Class<?> fieldType = field.getType();
37          if (fieldType.equals(Boolean.TYPE) || fieldType.equals(Boolean.class))
38              return Boolean.TRUE;
39          if (fieldType.equals(Byte.TYPE) || fieldType.equals(Byte.class))
40              return Byte.MAX_VALUE;
41          if (fieldType.equals(Double.TYPE) || fieldType.equals(Double.class))
42              return Double.MAX_VALUE;
43          if (fieldType.equals(Float.TYPE) || fieldType.equals(Float.class))
44              return Float.MAX_VALUE;
45          if (fieldType.equals(Integer.TYPE) || fieldType.equals(Integer.class))
46              return Integer.MAX_VALUE;
47          if (fieldType.equals(Long.TYPE) || fieldType.equals(Long.class))
48              return Long.MAX_VALUE;
49          if (fieldType.equals(Short.TYPE) || fieldType.equals(Short.class))
50              return Short.MAX_VALUE;
51          if (fieldType.equals(Character.TYPE) || fieldType.equals(Character.class))
52              return Character.MAX_VALUE;
53          if (fieldType.equals(BigDecimal.class))
54              return BigDecimal.TEN;
55          if (fieldType.equals(BigInteger.class))
56              return BigInteger.TEN;
57          if (fieldType.isAssignableFrom(String.class))
58              return "inside custom converter";
59          throw new CsvDataTypeMismatchException(value, fieldType, String.format(
60                  "Unable to set field value for field '%s' with value '%s' "
61                          + "- type is unsupported. Use primitive, boxed "
62                          + "primitive, BigDecimal, BigInteger and String types only.",
63                  fieldType, value));
64      }
65  }