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;
17  
18  import java.util.Map;
19  
20  import org.supercsv.cellprocessor.ift.BoolCellProcessor;
21  import org.supercsv.cellprocessor.ift.CellProcessor;
22  import org.supercsv.cellprocessor.ift.DateCellProcessor;
23  import org.supercsv.cellprocessor.ift.DoubleCellProcessor;
24  import org.supercsv.cellprocessor.ift.LongCellProcessor;
25  import org.supercsv.cellprocessor.ift.StringCellProcessor;
26  import org.supercsv.exception.SuperCsvCellProcessorException;
27  import org.supercsv.util.CsvContext;
28  
29  /**
30   * Maps from one object to another, by looking up a <tt>Map</tt> with the input as the key, and returning its
31   * corresponding value.
32   * 
33   * @since 1.50
34   * @author Dominique De Vito
35   * @author James Bassett
36   */
37  public class HashMapper extends CellProcessorAdaptor implements BoolCellProcessor, DateCellProcessor,
38  	DoubleCellProcessor, LongCellProcessor, StringCellProcessor {
39  	
40  	private final Map<Object, Object> mapping;
41  	private final Object defaultValue;
42  	
43  	/**
44  	 * Constructs a new <tt>HashMapper</tt> processor, which maps from one object to another, by looking up a
45  	 * <tt>Map</tt> with the input as the key, and returning its corresponding value. If no mapping is found, then
46  	 * <tt>null</tt> is returned.
47  	 * 
48  	 * @param mapping
49  	 *            the Map
50  	 * @throws NullPointerException
51  	 *             if mapping is null
52  	 * @throws IllegalArgumentException
53  	 *             if mapping is empty
54  	 */
55  	public HashMapper(final Map<Object, Object> mapping) {
56  		this(mapping, (Object) null);
57  	}
58  	
59  	/**
60  	 * Constructs a new <tt>HashMapper</tt> processor, which maps from one object to another, by looking up a
61  	 * <tt>Map</tt> with the input as the key, and returning its corresponding value. If no mapping is found, then the
62  	 * supplied default value is returned.
63  	 * 
64  	 * @param mapping
65  	 *            the Map
66  	 * @param defaultValue
67  	 *            the value to return if no mapping is found
68  	 * @throws NullPointerException
69  	 *             if mapping is null
70  	 * @throws IllegalArgumentException
71  	 *             if mapping is empty
72  	 */
73  	public HashMapper(final Map<Object, Object> mapping, final Object defaultValue) {
74  		super();
75  		checkPreconditions(mapping);
76  		this.mapping = mapping;
77  		this.defaultValue = defaultValue;
78  		
79  	}
80  	
81  	/**
82  	 * Constructs a new <tt>HashMapper</tt> processor, which maps from one object to another, by looking up a
83  	 * <tt>Map</tt> with the input as the key, and returning its corresponding value. If no mapping is found, then
84  	 * <tt>null</tt> is returned. Regardless of whether a mapping is found, the next processor in the chain will be
85  	 * called.
86  	 * 
87  	 * @param mapping
88  	 *            the Map
89  	 * @param next
90  	 *            the next processor in the chain
91  	 * @throws NullPointerException
92  	 *             if mapping or next is null
93  	 * @throws IllegalArgumentException
94  	 *             if mapping is empty
95  	 */
96  	public HashMapper(final Map<Object, Object> mapping, final CellProcessor next) {
97  		this(mapping, null, next);
98  	}
99  	
100 	/**
101 	 * Constructs a new <tt>HashMapper</tt> processor, which maps from one object to another, by looking up a
102 	 * <tt>Map</tt> with the input as the key, and returning its corresponding value. If no mapping is found, then the
103 	 * supplied default value is returned. Regardless of whether a mapping is found, the next processor in the chain
104 	 * will be called.
105 	 * 
106 	 * @param mapping
107 	 *            the Map
108 	 * @param defaultValue
109 	 *            the value to return if no mapping is found
110 	 * @param next
111 	 *            the next processor in the chain
112 	 * @throws NullPointerException
113 	 *             if mapping or next is null
114 	 * @throws IllegalArgumentException
115 	 *             if mapping is empty
116 	 */
117 	public HashMapper(final Map<Object, Object> mapping, final Object defaultValue, final CellProcessor next) {
118 		super(next);
119 		checkPreconditions(mapping);
120 		this.mapping = mapping;
121 		this.defaultValue = defaultValue;
122 	}
123 	
124 	/**
125 	 * Checks the preconditions for creating a new HashMapper processor.
126 	 * 
127 	 * @param mapping
128 	 *            the Map
129 	 * @throws NullPointerException
130 	 *             if mapping is null
131 	 * @throws IllegalArgumentException
132 	 *             if mapping is empty
133 	 */
134 	private static void checkPreconditions(final Map<Object, Object> mapping) {
135 		if( mapping == null ) {
136 			throw new NullPointerException("mapping should not be null");
137 		} else if( mapping.isEmpty() ) {
138 			throw new IllegalArgumentException("mapping should not be empty");
139 		}
140 	}
141 	
142 	/**
143 	 * {@inheritDoc}
144 	 * 
145 	 * @throws SuperCsvCellProcessorException
146 	 *             if value is null
147 	 */
148 	public Object execute(final Object value, final CsvContext context) {
149 		validateInputNotNull(value, context);
150 		
151 		Object result = mapping.get(value);
152 		if( result == null ) {
153 			result = defaultValue;
154 		}
155 		
156 		return next.execute(result, context);
157 	}
158 }