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.LongCellProcessor;
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 public class ParseLong extends CellProcessorAdaptor implements StringCellProcessor {
29
30
31
32
33 public ParseLong() {
34 super();
35 }
36
37
38
39
40
41
42
43
44
45
46 public ParseLong(final LongCellProcessor next) {
47 super(next);
48 }
49
50
51
52
53
54
55
56 public Object execute(final Object value, final CsvContext context) {
57 validateInputNotNull(value, context);
58
59 final Long result;
60 if( value instanceof Long ) {
61 result = (Long) value;
62 } else if( value instanceof String ) {
63 try {
64 result = Long.parseLong((String) value);
65 }
66 catch(final NumberFormatException e) {
67 throw new SuperCsvCellProcessorException(String.format("'%s' could not be parsed as an Long", value),
68 context, this, e);
69 }
70 } else {
71 final String actualClassName = value.getClass().getName();
72 throw new SuperCsvCellProcessorException(String.format(
73 "the input value should be of type Long or String but is of type %s", actualClassName), context, this);
74 }
75
76 return next.execute(result, context);
77 }
78 }