View Javadoc
1   /*
2    * Copyright 2012 Olivier Godineau
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at http://www.apache.org/licenses/LICENSE-2.0
7    * 
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11   * License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package olg.csv.bean.getter.impl;
15  
16  import olg.csv.base.Cell;
17  import olg.csv.base.Row;
18  import olg.csv.bean.getter.AbstractGetter;
19  import olg.csv.bean.getter.GetterException;
20  
21  /**
22   * Default getter based on extract cell from a row.
23   * 
24   */
25  public final class DefaultGetter extends AbstractGetter {
26  
27  	/**
28  	 * the cell num.
29  	 */
30  	private int rang;
31  
32  	/**
33  	 * Returns the rank of the target cell.
34  	 * 
35  	 * @return the number of the cell
36  	 */
37  	public int getRang() {
38  		return rang;
39  	}
40  
41  	/**
42  	 * Sets the rank of the target cell.
43  	 * 
44  	 * @param rang
45  	 *            the cell num. Must not be greater or equals 0.
46  	 */
47  	public void setRang(int rang) {
48  		if (rang < 0) {
49  			throw new IllegalArgumentException("Rang only greater or equals  0");
50  		}
51  		this.rang = rang;
52  	}
53  
54  	/**
55  	 * Extracts the target cell value from the given row.
56  	 * 
57  	 * @param line
58  	 *            the row from which get the string.
59  	 * @return the string.
60  	 */
61  	@Override
62  	protected String doGet(Row line) {
63  		String retour = null;
64  		if (line != null) {
65  			if (line.getSize() > rang) {
66  				Cell cell = line.getCell(rang);
67  				retour = (cell == null ? null : cell.getValue());
68  			} else {
69  				throw new GetterException(String.format("rang[%s] too high in the row %s", rang, line));
70  			}
71  		}
72  		return retour;
73  	}
74  
75  	/**
76  	 * Constructor with cell rank in argument.
77  	 * 
78  	 * @param rang
79  	 *            a non negative integer or a string in Sheet cell number format
80  	 *            (as C, AB ,... )
81  	 */
82  	public DefaultGetter(String rang) {
83  		super();
84  		this.rang = Cell.fromSheetCellNumber(rang);
85  
86  	}
87  }