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.time;
17  
18  import java.time.ZoneId;
19  import java.time.format.TextStyle;
20  import java.util.Locale;
21  import java.util.Objects;
22  
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 ZoneId to a String.
30   * The format is the ID of the timezone, e.g.
31   * ('Europe/Vienna'), as defined by {@link ZoneId#toString()}.
32   *
33   * @author Ludovico Fischer
34   * @since 2.4.0
35   */
36  public class FmtZoneId extends CellProcessorAdaptor {
37  
38  	private final TextStyle textStyle;
39  	private final Locale locale;
40  
41  	/**
42  	 * Constructs a new <tt>FmtZoneId</tt> processor, which formats a
43  	 * ZoneId as a String.
44  	 */
45  	public FmtZoneId() {
46  		this.textStyle = null;
47  		this.locale = null;
48  	}
49  
50  	/**
51  	 * Constructs a new <tt>FmtZoneId</tt> processor, which formats a
52  	 * ZoneId as a String, then calls the next processor in the chain.
53  	 *
54  	 * @param next next processor in the chain
55  	 * @throws NullPointerException if next is null
56  	 */
57  	public FmtZoneId(final CellProcessor next) {
58  		super(next);
59  		this.textStyle = null;
60  		this.locale = null;
61  	}
62  
63  	/**
64  	 * Constructs a new <tt>FmtZoneId</tt> processor, which formats a
65  	 * ZoneId as String, then calls the next processor in the chain.
66  	 *
67  	 * @param textStyle the TextStyle to use for formatting
68  	 * @param locale    the Locale to use for formatting
69  	 * @throws NullPointerException if either textStyle or locale is null
70  	 */
71  	public FmtZoneId(TextStyle textStyle, Locale locale) {
72  		Objects.requireNonNull(textStyle);
73  		Objects.requireNonNull(locale);
74  		this.textStyle = textStyle;
75  		this.locale = locale;
76  	}
77  
78  	/**
79  	 * Constructs a new <tt>FmtZoneId</tt> processor, which formats a
80  	 * ZoneId as String, then calls the next processor in the chain.
81  	 *
82  	 * @param textStyle the TextStyle to use for formatting
83  	 * @param locale    the Locale to use for formatting
84  	 * @param next      next processor in the chain
85  	 * @throws NullPointerException if any argument is null
86  	 */
87  	public FmtZoneId(final TextStyle textStyle, final Locale locale, final CellProcessor next) {
88  		super(next);
89  		Objects.requireNonNull(textStyle);
90  		Objects.requireNonNull(locale);
91  		this.textStyle = textStyle;
92  		this.locale = locale;
93  	}
94  
95  	/**
96  	 * {@inheritDoc}
97  	 *
98  	 * @throws SuperCsvCellProcessorException if value is null or not a ZoneId
99  	 */
100 	public Object execute(final Object value, final CsvContext context) {
101 		validateInputNotNull(value, context);
102 		if( !(value instanceof ZoneId) ) {
103 			throw new SuperCsvCellProcessorException(ZoneId.class, value, context, this);
104 		}
105 		final ZoneId zoneId = (ZoneId) value;
106 		final String result;
107 		if (textStyle != null && locale != null) {
108 			result = zoneId.getDisplayName(textStyle, locale);
109 		} else {
110 			result = zoneId.toString();
111 		}
112 		return next.execute(result, context);
113 	}
114 
115 }