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.Period;
19 import org.joda.time.format.ISOPeriodFormat;
20 import org.joda.time.format.PeriodFormat;
21 import org.joda.time.format.PeriodFormatter;
22 import org.joda.time.format.PeriodFormatterBuilder;
23 import org.supercsv.cellprocessor.CellProcessorAdaptor;
24 import org.supercsv.cellprocessor.ift.CellProcessor;
25 import org.supercsv.exception.SuperCsvCellProcessorException;
26 import org.supercsv.util.CsvContext;
27
28 /**
29 * Converts a Joda Period to a String.
30 *
31 * <p>
32 * For constructors using PeriodFormatter, refer to the following Joda classes:
33 * <ul>
34 * <li>{@link PeriodFormat} - formats by pattern and style</li>
35 * <li>{@link ISOPeriodFormat} - ISO8601 formats</li>
36 * <li>{@link PeriodFormatterBuilder} - complex formats created via method calls
37 * </li>
38 * </ul>
39 * <p>
40 * By default, converts to a String in the ISO8601 duration format.
41 * <p>
42 * For example, "PT6H3M7S" represents 6 hours, 3 minutes, 7 seconds.
43 * <p>
44 *
45 * @since 2.3.0
46 * @author James Bassett
47 */
48 public class FmtPeriod extends CellProcessorAdaptor {
49
50 private final PeriodFormatter formatter;
51
52 /**
53 * Constructs a new <tt>FmtPeriod</tt> processor, which formats a Joda
54 * Period as a String.
55 */
56 public FmtPeriod() {
57 formatter = null;
58 }
59
60 /**
61 * Constructs a new <tt>FmtPeriod</tt> processor, which formats a Joda
62 * Period as a String, then calls the next processor in the chain.
63 *
64 * @param next
65 * the next processor in the chain
66 * @throws NullPointerException
67 * if formatter or next is null
68 */
69 public FmtPeriod(final CellProcessor next) {
70 super(next);
71 this.formatter = null;
72 }
73
74 /**
75 * Constructs a new <tt>FmtPeriod</tt> processor, which formats a Joda
76 * Period as a String using the supplied formatter.
77 *
78 * @param formatter
79 * the formatter to use
80 * @throws NullPointerException
81 * if formatter is null
82 */
83 public FmtPeriod(final PeriodFormatter formatter) {
84 checkPreconditions(formatter);
85 this.formatter = formatter;
86 }
87
88 /**
89 * Constructs a new <tt>FmtPeriod</tt> processor, which formats a Joda
90 * Period as a String using the supplied formatter, then calls the next
91 * processor in the chain.
92 *
93 * @param formatter
94 * the formatter to use
95 * @param next
96 * the next processor in the chain
97 * @throws NullPointerException
98 * if formatter or next is null
99 */
100 public FmtPeriod(final PeriodFormatter formatter, final CellProcessor next) {
101 super(next);
102 checkPreconditions(formatter);
103 this.formatter = formatter;
104 }
105
106 /**
107 * Checks the preconditions for creating a new FmtPeriod processor.
108 *
109 * @param formatter
110 * the formatter
111 * @throws NullPointerException
112 * if formatter is null
113 */
114 private static void checkPreconditions(final PeriodFormatter formatter) {
115 if (formatter == null) {
116 throw new NullPointerException("formatter should not be null");
117 }
118 }
119
120 /**
121 * {@inheritDoc}
122 *
123 * @throws SuperCsvCellProcessorException
124 * if value is null or not a Period
125 */
126 public Object execute(final Object value, final CsvContext context) {
127 validateInputNotNull(value, context);
128 if (!(value instanceof Period)) {
129 throw new SuperCsvCellProcessorException(Period.class, value,
130 context, this);
131 }
132 final Period period = (Period) value;
133 final String result;
134 if (formatter != null) {
135 result = period.toString(formatter);
136 } else {
137 result = period.toString();
138 }
139
140 return next.execute(result, context);
141 }
142
143 }