View Javadoc
1   /*
2    * Copyright 2007 Kasper B. Graversen
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 org.supercsv.cellprocessor.joda;
17  
18  import org.joda.time.Interval;
19  import org.supercsv.cellprocessor.CellProcessorAdaptor;
20  import org.supercsv.cellprocessor.ift.CellProcessor;
21  import org.supercsv.exception.SuperCsvCellProcessorException;
22  import org.supercsv.util.CsvContext;
23  
24  /**
25   * Converts a String (in ISO8601 interval format) to a Joda Interval.
26   * 
27   * @since 2.3.0
28   * @author James Bassett
29   */
30  public class ParseInterval extends CellProcessorAdaptor {
31  
32  	/**
33  	 * Constructs a new <tt>ParseInterval</tt> processor, which parses a String
34  	 * as a Joda Interval.
35  	 */
36  	public ParseInterval() {
37  	}
38  
39  	/**
40  	 * Constructs a new <tt>ParseInterval</tt> processor, which parses a String
41  	 * as a Joda Interval, then calls the next processor in the chain.
42  	 * 
43  	 * @param next
44  	 *            the next processor in the chain
45  	 */
46  	public ParseInterval(final CellProcessor next) {
47  		super(next);
48  	}
49  
50  	/**
51  	 * {@inheritDoc}
52  	 * 
53  	 * @throws SuperCsvCellProcessorException
54  	 *             if value is null or is not a String
55  	 */
56  	public Object execute(final Object value, final CsvContext context) {
57  		validateInputNotNull(value, context);
58  		if (!(value instanceof String)) {
59  			throw new SuperCsvCellProcessorException(String.class, value,
60  					context, this);
61  		}
62  		final Interval result;
63  		try {
64  			result = Interval.parse((String) value);
65  		} catch (IllegalArgumentException e) {
66  			throw new SuperCsvCellProcessorException(
67  					"Failed to parse value as an Interval", context, this, e);
68  		}
69  		return next.execute(result, context);
70  	}
71  
72  }