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  /**
22   * This class converts to {@link com.opencsv.bean.mocks.ComplexClassForCustomAnnotation}.
23   *
24   * @author Andrew Rucker Jones
25   */
26  public class ConverterComplexClassForCustomAnnotation<T, I> extends AbstractBeanField<T, I> {
27  
28      @Override
29      protected Object convert(String value) {
30          ComplexClassForCustomAnnotation o;
31          String[] sa = value.split("\\.", 3);
32          if (sa[2].contains("derived")) {
33              o = new ComplexDerivedClassForCustomAnnotation();
34              ((ComplexDerivedClassForCustomAnnotation) o).f = (float) 1.0;
35          } else if (sa[2].contains("badtype")) {
36              return "Mismatched data type";
37          } else {
38              o = new ComplexClassForCustomAnnotation();
39          }
40          o.i = Integer.parseInt(sa[0]);
41          o.c = sa[1].charAt(0);
42          o.s = sa[2];
43          return o;
44      }
45      
46      @Override
47      protected String convertToWrite(Object o) throws CsvDataTypeMismatchException {
48          if(!(o instanceof ComplexClassForCustomAnnotation)) {
49              throw new CsvDataTypeMismatchException();
50          }
51          ComplexClassForCustomAnnotation c = (ComplexClassForCustomAnnotation)o;
52          StringBuilder sb = new StringBuilder();
53          sb.append(c.i).append('.');
54          sb.append(c.c).append('.');
55          sb.append(c.s);
56          return sb.toString();
57      }
58  }