KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > anupam > csv > mapping > CSVFieldMapping


1 /*
2  * CSVFieldMapping.java
3  *
4  * Copyright (C) 2005 Anupam Sengupta (anupamsg@users.sourceforge.net)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Version: $Revision: 1.3 $
21  */

22 package net.sf.anupam.csv.mapping;
23
24 import net.sf.anupam.csv.formatters.CSVFieldFormatter;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.lang.builder.EqualsBuilder;
28 import org.apache.commons.lang.builder.ToStringBuilder;
29 import org.apache.commons.lang.builder.HashCodeBuilder;
30 import org.apache.commons.lang.builder.CompareToBuilder;
31
32 /**
33  * Represents a single CSV field to Java Bean attribute mapping. The mapping can
34  * be for basic data types, or point to other referenced CSV bean mappings for
35  * representing nested beans.
36  *
37  * @author Anupam Sengupta
38  * @version $Revision: 1.3 $
39  * @see CSVBeanMapping
40  * @since 1.5
41  */

42 public class CSVFieldMapping implements Comparable JavaDoc<CSVFieldMapping> {
43
44     /**
45      * user defined name of CSV field being mapped.
46      */

47     private String JavaDoc fieldName;
48
49     /**
50      * Fully qualified class name of the field being mapped.
51      */

52     private String JavaDoc fieldType;
53
54     /**
55      * The CSV field position (starting at 0).
56      */

57     private int fieldPosition;
58
59     /**
60      * Name of the bean's attribute.
61      */

62     private String JavaDoc attributeName;
63
64     /**
65      * The CSV field formatter to use for this field.
66      */

67     private CSVFieldFormatter formatter;
68
69     /**
70      * Declarative name of the CSV field formatter to use.
71      */

72     private String JavaDoc reformatterName;
73
74     /**
75      * Declarative bean name of the being being referenced by this field
76      * mapping.
77      */

78     private String JavaDoc beanReferenceName;
79
80     /**
81      * The CSV bean mapping referenced by this field mapping.
82      */

83     private CSVBeanMapping beanReference;
84
85     /**
86      * Constructor for CSVFieldMapping.
87      */

88     public CSVFieldMapping() {
89         super();
90     }
91
92     /**
93      * Returns the hash code for this field mapping. The hash code is based on the
94      * field name and the field position.
95      *
96      * @return the hash code
97      * @see Object#hashCode()
98      */

99     @Override JavaDoc
100     public int hashCode() {
101         return new HashCodeBuilder().append(fieldName).append(fieldPosition)
102                 .toHashCode();
103     }
104
105     /**
106      * Compares this field mapping with another for equality. Field mappings are compared
107      * for the name and the field position.
108      *
109      * @param other the other field mapping to compare against
110      * @return <code>true</code> if equal, <code>false</code> otherwise
111      * @see Object#equals(Object)
112      */

113     @Override JavaDoc
114     public boolean equals(final Object JavaDoc other) {
115         if (this == other) {
116             return true;
117         }
118         if (!(other instanceof CSVFieldMapping)) {
119             return false;
120         }
121         final CSVFieldMapping castOther = (CSVFieldMapping) other;
122         return new EqualsBuilder().append(fieldName, castOther.fieldName)
123                 .append(fieldPosition, castOther.fieldPosition).isEquals();
124     }
125
126     /**
127      * Dumps the contents of this field mapping as a string. This is meant for
128      * <strong>debugging</strong> only.
129      *
130      * @return the string representation
131      * @see Object#toString()
132      */

133     @Override JavaDoc
134     public String JavaDoc toString() {
135         final ToStringBuilder strBuilder = new ToStringBuilder(this);
136         strBuilder.append("fieldName", fieldName)
137                 .append("fieldType", fieldType).append("fieldPosition",
138                 fieldPosition).append("attributeName", attributeName)
139                 .append("reformatterName", reformatterName);
140
141         strBuilder.append("FormatterClass", (formatter != null ? formatter
142                 .getClass() : "None"));
143         strBuilder.append("beanReferenceName", beanReferenceName);
144         strBuilder
145                 .append("Bean Reference Class",
146                         (beanReference != null) ? beanReference.getBeanClass()
147                                 : "None");
148         return strBuilder.toString();
149     }
150
151     /**
152      * Compares this field mapping to another mapping. The comparision is based on
153      * the field position.
154      *
155      * @param other the other field mapping to compare to
156      * @return <code>0</code> if the two field mappings are equal, <code>-1</code> if this
157      * field mapping position is less than the other's, and <code>+1</code> if this field mapping
158      * position is higher than the others.
159      * @see Comparable#compareTo
160      */

161     public int compareTo(final CSVFieldMapping other) {
162         if (this.equals(other)) {
163             return 0;
164         } else {
165             return new CompareToBuilder().append(this.getFieldName(), other.getFieldName())
166                     .append(this.getFieldPosition(), other.getFieldPosition()).toComparison();
167
168
169         }
170
171     }
172
173     /**
174      * Returns the mapped POJO bean's attribute name corresponding to this field.
175      *
176      * @return Returns the mapped POJO attribute name
177      */

178     public String JavaDoc getAttributeName() {
179         return this.attributeName;
180     }
181
182     /**
183      * Sets the mapped POJO's attribute name corresponding to this field. The name has to <strong>exactly</strong>
184      * match the attribute name (including the case).
185      *
186      * @param attributeName The mapped POJO' attribute name
187      */

188     public void setAttributeName(final String JavaDoc attributeName) {
189         this.attributeName = StringUtils.trim(attributeName);
190     }
191
192     /**
193      * Returns the user defined name of this field.
194      *
195      * @return Returns the name of this field.
196      */

197     public String JavaDoc getFieldName() {
198         return this.fieldName;
199     }
200
201     /**
202      * Sets the user defined name of this field. This need not be same
203      * as the CSV field name (if defined on the CSV header row).
204      *
205      * @param fieldName The name of this field
206      */

207     public void setFieldName(final String JavaDoc fieldName) {
208         this.fieldName = StringUtils.trim(fieldName);
209     }
210
211     /**
212      * Returns this field's position in the CSV line. Field positions
213      * start at 0.
214      *
215      * @return Returns the field's position
216      */

217     public int getFieldPosition() {
218         return this.fieldPosition;
219     }
220
221     /**
222      * Sets this field's position in the CSV line. Field positions start
223      * at 0.
224      *
225      * @param fieldPosition The field's position in the CSV line
226      */

227     public void setFieldPosition(final int fieldPosition) {
228         this.fieldPosition = fieldPosition;
229     }
230
231     /**
232      * Returns the fully qualified Java type name of this field.
233      *
234      * @return Returns the Java type name of this field
235      */

236     public String JavaDoc getFieldType() {
237         return this.fieldType;
238     }
239
240     /**
241      * Sets the fully qualified Java type name of this field.
242      *
243      * @param fieldType The Java type name of this field
244      */

245     public void setFieldType(final String JavaDoc fieldType) {
246         this.fieldType = StringUtils.trim(fieldType);
247     }
248
249     /**
250      * Returns the CSV formatter attached to this field.
251      *
252      * @return Returns the formatter
253      */

254     public CSVFieldFormatter getFormatter() {
255         return this.formatter;
256     }
257
258     /**
259      * Sets the formatter attached to this field.
260      *
261      * @param formatter The formatter to set
262      */

263     public void setFormatter(final CSVFieldFormatter formatter) {
264         this.formatter = formatter;
265     }
266
267     /**
268      * Returns the declarative name of the formatter attached to this field.
269      *
270      * @return Returns the declarative formatter name
271      */

272     public String JavaDoc getReformatterName() {
273         return this.reformatterName;
274     }
275
276     /**
277      * Sets the declarative name of the formatter attached to this field.
278      *
279      * @param reformatterName The declarative formatter name to set
280      */

281     public void setReformatterName(final String JavaDoc reformatterName) {
282         this.reformatterName = reformatterName;
283     }
284
285     /**
286      * Returns the declarative name of the referenced bean mapping for this field, or
287      * <code>null</code> if no bean mapping if referenced by this field.
288      *
289      * @return Returns name of the referenced bean mapping
290      */

291     public String JavaDoc getBeanReferenceName() {
292         return this.beanReferenceName;
293     }
294
295     /**
296      * Sets the declarative name of a referenced bean mapping for this
297      * field.
298      *
299      * @param beanReferenceName The declarative name of the referenced bean
300      */

301     public void setBeanReferenceName(final String JavaDoc beanReferenceName) {
302         this.beanReferenceName = beanReferenceName;
303     }
304
305     /**
306      * Sets the referenced bean mapping for this field.
307      *
308      * @param beanReference The bean mapping reference to set
309      */

310     public void setBeanReference(final CSVBeanMapping beanReference) {
311         this.beanReference = beanReference;
312     }
313
314     /**
315      * Returns the referenced bean mapping, if one is present. Returns
316      * <code>null</code> if this field does not have any bean reference.
317      *
318      * @return Returns the bean mapping reference
319      */

320     public CSVBeanMapping getBeanReference() {
321         return this.beanReference;
322     }
323
324 }
325
Popular Tags