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 Joda Interval to a String in ISO8601 interval format.
26 *
27 * @since 2.3.0
28 * @author James Bassett
29 */
30 public class FmtInterval extends CellProcessorAdaptor {
31
32 /**
33 * Constructs a new <tt>FmtInterval</tt> processor, which formats a Joda
34 * Interval as a String.
35 */
36 public FmtInterval() {
37 }
38
39 /**
40 * Constructs a new <tt>FmtInterval</tt> processor, which formats a Joda
41 * Interval as a String, then calls the next processor in the chain.
42 *
43 * @param next
44 * next processor in the chain
45 * @throws NullPointerException
46 * if next is null
47 */
48 public FmtInterval(final CellProcessor next) {
49 super(next);
50 }
51
52 /**
53 * {@inheritDoc}
54 *
55 * @throws SuperCsvCellProcessorException
56 * if value is null or not a Interval
57 */
58 public Object execute(final Object value, final CsvContext context) {
59 validateInputNotNull(value, context);
60 if (!(value instanceof Interval)) {
61 throw new SuperCsvCellProcessorException(Interval.class, value,
62 context, this);
63 }
64 final Interval interval = (Interval) value;
65 final String result = interval.toString();
66 return next.execute(result, context);
67 }
68
69 }