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.benchmark.cellprocessor;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.supercsv.benchmark.model.StopTypeAndName;
22  import org.supercsv.cellprocessor.HashMapper;
23  import org.supercsv.cellprocessor.ift.CellProcessor;
24  import org.supercsv.exception.SuperCsvCellProcessorException;
25  import org.supercsv.util.CsvContext;
26  
27  /**
28   * Cell processor to parse a stop type and name.
29   * 
30   * @author James Bassett
31   */
32  public class ParseStopTypeAndName extends HashMapper {
33  
34  	private static final Map<Object, Object> MAP = new HashMap<Object, Object>();
35  	static {
36  		MAP.put("Airport, airport stop", StopTypeAndName.AIRPORT_STOP);
37  		MAP.put("Bus/coach stop", StopTypeAndName.BUS_COACH_STOP);
38  		MAP.put("Ferry", StopTypeAndName.FERRY);
39  		MAP.put("Metro/tram", StopTypeAndName.METRO_TRAM);
40  		MAP.put("Rail", StopTypeAndName.RAIL);
41  		MAP.put("Taxi rank", StopTypeAndName.TAXI_RANK);
42  	}
43  
44  	/**
45  	 * Constructs a new ParseStopTypeAndName processor.
46  	 */
47  	public ParseStopTypeAndName() {
48  		super(MAP);
49  	}
50  
51  	/**
52  	 * Constructs a new ParseStopTypeAndName processor.
53  	 */
54  	public ParseStopTypeAndName(CellProcessor next) {
55  		super(MAP, next);
56  	}
57  
58  	/**
59  	 * {@inheritDoc}
60  	 */
61  	@Override
62  	public Object execute(Object value, CsvContext context) {
63  
64  		validateInputNotNull(value, context);
65  
66  		Object stopTypeAndName = MAP.get(value);
67  		if (stopTypeAndName == null) {
68  			throw new SuperCsvCellProcessorException(String.format(
69  					"Unknown StopTypeAndName: %s", value), context, this);
70  		}
71  		return next.execute(stopTypeAndName, context);
72  	}
73  
74  }