1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.supercsv.cellprocessor;
17
18 import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
19 import org.supercsv.cellprocessor.ift.StringCellProcessor;
20 import org.supercsv.exception.SuperCsvCellProcessorException;
21 import org.supercsv.util.CsvContext;
22
23
24
25
26
27
28
29 public class ParseChar extends CellProcessorAdaptor implements StringCellProcessor {
30
31
32
33
34 public ParseChar() {
35 super();
36 }
37
38
39
40
41
42
43
44
45
46
47 public ParseChar(final DoubleCellProcessor next) {
48 super(next);
49 }
50
51
52
53
54
55
56
57 public Object execute(final Object value, final CsvContext context) {
58 validateInputNotNull(value, context);
59
60 final Character result;
61 if( value instanceof Character ) {
62 result = (Character) value;
63 } else if( value instanceof String ) {
64 final String stringValue = (String) value;
65 if( stringValue.length() == 1 ) {
66 result = Character.valueOf(stringValue.charAt(0));
67 } else {
68 throw new SuperCsvCellProcessorException(String.format(
69 "'%s' cannot be parsed as a char as it is a String longer than 1 character", stringValue), context,
70 this);
71 }
72 } else {
73 final String actualClassName = value.getClass().getName();
74 throw new SuperCsvCellProcessorException(String.format(
75 "the input value should be of type Character or String but is of type %s", actualClassName), context,
76 this);
77 }
78
79 return next.execute(result, context);
80 }
81 }