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