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 format a stop type and name for writing.
29   * 
30   * @author James Bassett
31   */
32  public class FormatStopTypeAndName extends HashMapper {
33  	
34  	private static final Map<Object, Object> MAP = new HashMap<Object, Object>();
35  	static {
36  		MAP.put(StopTypeAndName.AIRPORT_STOP, "Airport, airport stop");
37  		MAP.put(StopTypeAndName.BUS_COACH_STOP, "Bus/coach stop");
38  		MAP.put(StopTypeAndName.FERRY, "Ferry");
39  		MAP.put(StopTypeAndName.METRO_TRAM, "Metro/tram" );
40  		MAP.put(StopTypeAndName.RAIL, "Rail");
41  		MAP.put(StopTypeAndName.TAXI_RANK, "Taxi rank");
42  	}
43  	
44  	/**
45  	 * Constructs a new FormatStopTypeAndName processor.
46  	 */
47  	public FormatStopTypeAndName() {
48  		super(MAP);
49  	}
50  	
51  	/**
52  	 * Constructs a new FormatStopTypeAndName processor.
53  	 */
54  	public FormatStopTypeAndName(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  		String stopTypeAndName = (String) MAP.get(value);
67  		if( stopTypeAndName == null ) {
68  			throw new SuperCsvCellProcessorException(String.format("Unknown StopTypeAndName: %s", value), context, this);
69  		}
70  		return next.execute(stopTypeAndName, context);
71  	}
72  	
73  }