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