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.DateTimeException;
19  import java.time.ZoneId;
20  import java.util.Map;
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 String to a ZoneId.
30   *
31   * @author Ludovico Fischer
32   * @since 2.4.0
33   */
34  public class ParseZoneId extends CellProcessorAdaptor {
35  
36  	private final Map<String, String> aliasMap;
37  
38  	/**
39  	 * Constructs a new <tt>ParseZoneId</tt> processor, which parses a
40  	 * String recognized by {@link ZoneId#of(String)} as a ZoneId.
41  	 */
42  	public ParseZoneId() {
43  		this.aliasMap = null;
44  	}
45  
46  	/**
47  	 * Constructs a new <tt>ParseZoneId</tt> processor, which parses a
48  	 * String as a ZoneId, then calls the next processor in the
49  	 * chain.
50  	 *
51  	 * @param next the next processor in the chain
52  	 * @see ParseZoneId()
53  	 */
54  	public ParseZoneId(final CellProcessor next) {
55  		super(next);
56  		this.aliasMap = null;
57  	}
58  
59  	/**
60  	 * Constructs a new <tt>ParseZoneId</tt> processor, which parses a
61  	 * String as a ZoneId using the supplied Zone ID mappings.
62  	 *
63  	 * @param aliasMap a Map from custom zone IDs to canonical representations
64  	 * @see ZoneId#of(String, Map)
65  	 */
66  	public ParseZoneId(final Map<String, String> aliasMap) {
67  		Objects.requireNonNull(aliasMap);
68  		this.aliasMap = aliasMap;
69  	}
70  
71  	/**
72  	 * Constructs a new <tt>ParseZoneId</tt> processor, which parses a
73  	 * String as a ZoneId using the supplied Zone ID mappings, then calls the next processor in the
74  	 * chain.
75  	 *
76  	 * @param aliasMap a Map from custom zone IDs to canonical representations
77  	 * @param next     the next processor in the chain
78  	 * @see ZoneId#of(String, Map)
79  	 */
80  	public ParseZoneId(final Map<String, String> aliasMap, final CellProcessor next) {
81  		super(next);
82  		Objects.requireNonNull(aliasMap);
83  		this.aliasMap = aliasMap;
84  	}
85  
86  	/**
87  	 * {@inheritDoc}
88  	 *
89  	 * @throws SuperCsvCellProcessorException if value is null or is not a String
90  	 */
91  	public Object execute(final Object value, final CsvContext context) {
92  		validateInputNotNull(value, context);
93  		if( !(value instanceof String) ) {
94  			throw new SuperCsvCellProcessorException(String.class, value, context, this);
95  		}
96  		final ZoneId result;
97  		try {
98  			if( aliasMap != null ) {
99  				result = ZoneId.of((String) value, aliasMap);
100 			} else {
101 				result = ZoneId.of((String) value);
102 			}
103 		}
104 		catch(DateTimeException e) {
105 			throw new SuperCsvCellProcessorException("Failed to parse value as a ZoneId", context, this, e);
106 		}
107 		return next.execute(result, context);
108 
109 	}
110 
111 }