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 org.supercsv.benchmark.model.StopType;
19  import org.supercsv.cellprocessor.CellProcessorAdaptor;
20  import org.supercsv.cellprocessor.ift.CellProcessor;
21  import org.supercsv.exception.SuperCsvCellProcessorException;
22  import org.supercsv.util.CsvContext;
23  
24  /**
25   * Cell processor to parse a stop type.
26   * 
27   * @author James Bassett
28   */
29  public class ParseStopType extends CellProcessorAdaptor {
30  	
31  	/**
32  	 * Constructs a new ParseStopType processor.
33  	 */
34  	public ParseStopType() {
35  		super();
36  	}
37  	
38  	/**
39  	 * Constructs a new ParseStopType processor.
40  	 */
41  	public ParseStopType(CellProcessor next) {
42  		super(next);
43  	}
44  	
45  	/**
46  	 * {@inheritDoc}
47  	 */
48  	public Object execute(Object value, CsvContext context) {
49  		
50  		validateInputNotNull(value, context);
51  		
52  		for( StopType stopType : StopType.values() ) {
53  			if( stopType.name().equals(value.toString()) ) {
54  				return next.execute(stopType, context);
55  			}
56  		}
57  		
58  		throw new SuperCsvCellProcessorException(String.format("Unknown StopType: %s", value), context, this);
59  	}
60  	
61  }