View Javadoc
1   package org.supercsv.cellprocessor.joda;
2   
3   import org.joda.time.format.DateTimeFormatter;
4   import org.supercsv.cellprocessor.CellProcessorAdaptor;
5   import org.supercsv.cellprocessor.ift.CellProcessor;
6   import org.supercsv.cellprocessor.ift.StringCellProcessor;
7   import org.supercsv.exception.SuperCsvCellProcessorException;
8   import org.supercsv.util.CsvContext;
9   
10  /**
11   * Abstract base class for cell processors converting Strings to Joda types.
12   * 
13   * @since 2.3.0
14   * @author James Bassett
15   * @param <T>
16   *            the Joda type that the processor returns
17   */
18  public abstract class AbstractJodaParsingProcessor<T> extends
19  		CellProcessorAdaptor implements StringCellProcessor {
20  
21  	private final DateTimeFormatter formatter;
22  
23  	/**
24  	 * Constructs a new <tt>AbstractJodaParsingProcessor</tt> processor, which
25  	 * parses a String as a Joda type.
26  	 */
27  	public AbstractJodaParsingProcessor() {
28  		this.formatter = null;
29  	}
30  
31  	/**
32  	 * Constructs a new <tt>AbstractJodaParsingProcessor</tt> processor, which
33  	 * parses a String as a Joda type, then calls the next processor in the
34  	 * chain.
35  	 * 
36  	 * @param next
37  	 *            the next processor in the chain
38  	 * @throws NullPointerException
39  	 *             if next is null
40  	 */
41  	public AbstractJodaParsingProcessor(final CellProcessor next) {
42  		super(next);
43  		this.formatter = null;
44  	}
45  
46  	/**
47  	 * Constructs a new <tt>AbstractJodaParsingProcessor</tt> processor, which
48  	 * parses a String as a Joda type using the supplied formatter.
49  	 * 
50  	 * @param formatter
51  	 *            the formatter used for parsing
52  	 * @throws NullPointerException
53  	 *             if formatter is null
54  	 */
55  	public AbstractJodaParsingProcessor(final DateTimeFormatter formatter) {
56  		checkPreconditions(formatter);
57  		this.formatter = formatter;
58  	}
59  
60  	/**
61  	 * Constructs a new <tt>AbstractJodaParsingProcessor</tt> processor, which
62  	 * parses a String as a Joda type using the supplied formatter, then calls
63  	 * the next processor in the chain.
64  	 * 
65  	 * @param formatter
66  	 *            the formatter used for parsing
67  	 * @param next
68  	 *            the next processor in the chain
69  	 * @throws NullPointerException
70  	 *             if formatter or next is null
71  	 */
72  	public AbstractJodaParsingProcessor(final DateTimeFormatter formatter,
73  			final CellProcessor next) {
74  		super(next);
75  		checkPreconditions(formatter);
76  		this.formatter = formatter;
77  	}
78  
79  	/**
80  	 * Checks the preconditions for creating a new AbstractJodaParsingProcessor
81  	 * processor.
82  	 * 
83  	 * @param formatter
84  	 *            the formatter
85  	 * @throws NullPointerException
86  	 *             if formatter is null
87  	 */
88  	private static void checkPreconditions(final DateTimeFormatter formatter) {
89  		if (formatter == null) {
90  			throw new NullPointerException("formatter should not be null");
91  		}
92  	}
93  
94  	/**
95  	 * {@inheritDoc}
96  	 * 
97  	 * @throws SuperCsvCellProcessorException
98  	 *             if value is null or is not a String
99  	 */
100 	public Object execute(final Object value, final CsvContext context) {
101 		validateInputNotNull(value, context);
102 		if (!(value instanceof String)) {
103 			throw new SuperCsvCellProcessorException(String.class, value,
104 					context, this);
105 		}
106 
107 		final String string = (String) value;
108 		final T result;
109 		try {
110 			if (formatter != null) {
111 				result = parse(string, formatter);
112 			} else {
113 				result = parse(string);
114 			}
115 		} catch (IllegalArgumentException e) {
116 			throw new SuperCsvCellProcessorException("Failed to parse value",
117 					context, this, e);
118 		}
119 
120 		return next.execute(result, context);
121 	}
122 
123 	/**
124 	 * Parses the String into the appropriate Joda type.
125 	 * 
126 	 * @param string
127 	 *            the string to parse
128 	 * @return the Joda type
129 	 * @throws IllegalArgumentException
130 	 *             if the string can't be parsed
131 	 */
132 	protected abstract T parse(final String string);
133 
134 	/**
135 	 * Parses the String into the appropriate Joda type, using the supplied
136 	 * formatter.
137 	 * 
138 	 * @param string
139 	 *            the string to parse
140 	 * @param formatter
141 	 *            the formatter to use
142 	 * @return the Joda type
143 	 * @throws IllegalArgumentException
144 	 *             if the string can't be parsed
145 	 */
146 	protected abstract T parse(final String string,
147 			final DateTimeFormatter formatter);
148 
149 }