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.impl;
15  
16  import olg.csv.base.Cell;
17  import olg.csv.bean.ICellProcessor;
18  
19  /**
20   * This class allows to format a column cell from entity properties values.
21   * 
22   * @author Olivier Godineau
23   * 
24   * @param <B>
25   *            the entity type
26   * 
27   */
28  public final class CellProcessor<B> implements Comparable<CellProcessor<B>>, ICellProcessor<B> {
29  	/**
30  	 * column number.
31  	 */
32  	private int rang;
33  
34  	/**
35  	 * column header name.
36  	 */
37  	private String name;
38  
39  	/**
40  	 * formatter to rendering the entity properties as value of a cell.
41  	 */
42  	private PropertyFormatter propertyFormatter;
43  
44  	/**
45  	 * 
46  	 * @param rang
47  	 *            column number
48  	 * @param name
49  	 *            column header name
50  	 * @param propertyFormatter
51  	 *            the formatter to rendering the entity properties as value of
52  	 *            column cell
53  	 */
54  	public CellProcessor(String rang, String name, PropertyFormatter propertyFormatter) {
55  		super();
56  		this.rang = Cell.fromSheetCellNumber(rang);
57  		this.name = name;
58  		this.propertyFormatter = propertyFormatter;
59  	}
60  
61  	/**
62  	 * Returns the column num. Column num begin at 0.
63  	 * 
64  	 * @return the columne num.
65  	 */
66  	public int getRang() {
67  		return rang;
68  	}
69  
70  	/**
71  	 * Get the column header name.
72  	 * 
73  	 * @return the column header name.
74  	 */
75  	public String getName() {
76  		return name;
77  	}
78  
79  	/**
80  	 * Returns the formatter dedicated to rendering the entity properties as
81  	 * value of column cell.
82  	 * 
83  	 * @return the formatter.
84  	 */
85  	public PropertyFormatter getPropertyFormatter() {
86  		return propertyFormatter;
87  	}
88  
89  	/**
90  	 * Sets the column num.
91  	 * 
92  	 * @param rang
93  	 *            the column num.
94  	 */
95  	public void setRang(int rang) {
96  		this.rang = rang;
97  	}
98  
99  	/**
100 	 * Set the column header name.
101 	 * 
102 	 * @param name
103 	 *            the header.
104 	 */
105 	public void setName(String name) {
106 		this.name = name;
107 	}
108 
109 	/**
110 	 * Sets the formatter dedicated to format entity properties as cell value.
111 	 * 
112 	 * @param propertyFormatter
113 	 *            the formatter.
114 	 */
115 	public void setPropertyFormatter(PropertyFormatter propertyFormatter) {
116 		this.propertyFormatter = propertyFormatter;
117 	}
118 	/**
119 	 * {@inheritDoc} Comparison is based on the rang field.
120 	 */
121 	public int compareTo(CellProcessor<B> columnFormatter) {
122 		return this.rang - columnFormatter.rang;
123 	}
124 
125 	@Override
126 	public int hashCode() {
127 		final int prime = 31;
128 		int result = 1;
129 		result = prime * result + ((name == null) ? 0 : name.hashCode());
130 		result = prime * result + ((propertyFormatter == null) ? 0 : propertyFormatter.hashCode());
131 		result = prime * result + rang;
132 		return result;
133 	}
134 
135 	@Override
136 	public boolean equals(Object obj) {
137 		if (this == obj) {
138 			return true;
139 		}
140 		if (obj == null) {
141 			return false;
142 		}
143 		if (getClass() != obj.getClass()) {
144 			return false;
145 		}
146 		CellProcessor<?> other = (CellProcessor<?>) obj;
147 		if (name == null) {
148 			if (other.name != null) {
149 				return false;
150 			}
151 		} else if (!name.equals(other.name)) {
152 			return false;
153 		}
154 		if (propertyFormatter == null) {
155 			if (other.propertyFormatter != null) {
156 				return false;
157 			}
158 		} else if (!propertyFormatter.equals(other.propertyFormatter)) {
159 			return false;
160 		}
161 		if (rang != other.rang) {
162 			return false;
163 		}
164 		return true;
165 	}
166 
167 	/**
168 	 * {@inheritDoc}
169 	 */
170 	public Cell transform(B e) {
171 		return new Cell(this.getRang(), this.getPropertyFormatter().toString(e));
172 	}
173 
174 	/**
175 	 * {@inheritDoc}
176 	 */
177 	public Cell getHeader() {
178 		return new Cell(this.getRang(), this.getName());
179 	}
180 }