KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CSVBeanMapping.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.5 $
21  */

22
23 package net.sf.anupam.csv.mapping;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.commons.lang.builder.ToStringBuilder;
27 import org.apache.commons.lang.builder.EqualsBuilder;
28 import org.apache.commons.lang.builder.HashCodeBuilder;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
33
34 /**
35  * Represents the CSV to Java Bean mapping for a single Java bean. Instances of this class store the bean name, the
36  * fully qualified class name, CSV header indicator, as well as individual CSV field mappings.
37  * <p/>
38  * <p/>
39  * The bean name (which is user defined) acts as the name of the mapping as well, and is used for looking up or
40  * accessing the specific {@link net.sf.anupam.csv.CSVParser parsers} for this mapping. </p>
41  *
42  * @author Anupam Sengupta
43  * @version $Revision: 1.5 $
44  * @see CSVFieldMapping
45  * @since 1.5
46  */

47 public class CSVBeanMapping implements Iterable JavaDoc<CSVFieldMapping> {
48
49     /**
50      * Name of the Java bean being mapped. This is a user defined name, and need not be same as the bean's class name.
51      */

52     private String JavaDoc beanName;
53
54     /**
55      * Fully qualified class name of the Java bean being mapped.
56      */

57     private String JavaDoc beanClass;
58
59     /**
60      * Indicates whether the source CSV has a header row.
61      */

62     private boolean csvHeaderPresent;
63
64     /**
65      * List of mapped CSV fields for this bean mapping.
66      */

67     private SortedSet JavaDoc<CSVFieldMapping> fields;
68
69     /**
70      * The highest field position number.
71      */

72     private int maxFieldPosition;
73
74     /**
75      * Constructor for CSVBeanMapping.
76      */

77     public CSVBeanMapping() {
78         super();
79         fields = new TreeSet JavaDoc<CSVFieldMapping>();
80     }
81
82     /**
83      * Returns the hash code for this bean mapping. The hashcode is based on the bean name.
84      *
85      * @return the hash code
86      */

87     @Override JavaDoc
88     public int hashCode() {
89         return new HashCodeBuilder().append(this.getBeanName())
90                 .toHashCode();
91
92     }
93
94     /**
95      * Compares this bean mapping to another for equality. The comparision is based on the declarative bean name.
96      *
97      * @param other the other bean mapping to compare against
98      * @return <code>true</code> if equal, <code>false</code> otherwise
99      */

100     @Override JavaDoc
101     public boolean equals(final Object JavaDoc other) {
102         if (this == other) {
103             return true;
104         }
105
106         if (!(other instanceof CSVBeanMapping)) {
107             return false;
108         }
109         final CSVBeanMapping otherInstance = (CSVBeanMapping) other;
110         return new EqualsBuilder().append(this.getBeanName(), otherInstance.getBeanName())
111                 .isEquals();
112
113     }
114
115     /**
116      * Dumps content of the bean mapping. This is meant for <strong>debugging</strong> only.
117      *
118      * @return a string representation of this bean mapping
119      * @see Object#toString()
120      */

121     @Override JavaDoc
122     public String JavaDoc toString() {
123         final ToStringBuilder strBuilder = new ToStringBuilder(this).append(
124                 "beanName", beanName)
125                 .append("beanClass", beanClass);
126
127         strBuilder.append("Number of Fields", fields.size());
128         strBuilder.append("Max Field Position", getMaxFieldPosition());
129         strBuilder.append("CSV Header Present", isCsvHeaderPresent());
130         return strBuilder.toString();
131     }
132
133     /**
134      * Provides an iterator over the CSV field mappings present in this bean mapping.
135      *
136      * @return returns an iterator over the field mappins present in this bean mapping
137      * @see Iterable#iterator()
138      */

139     public Iterator JavaDoc<CSVFieldMapping> iterator() {
140         return fields.iterator();
141     }
142
143     /**
144      * Returns the mapped bean's fully qualified class name.
145      *
146      * @return Returns the mapped bean's class name
147      */

148     public String JavaDoc getBeanClass() {
149         return this.beanClass;
150     }
151
152     /**
153      * Sets the mapped bean's fully qualified class name.
154      *
155      * @param beanClass The mapped bean's class name
156      */

157     public void setBeanClass(final String JavaDoc beanClass) {
158         this.beanClass = StringUtils.trim(beanClass);
159     }
160
161     /**
162      * Returns the user defined name of the mapped bean.
163      *
164      * @return Returns the mapped bean's name
165      */

166     public String JavaDoc getBeanName() {
167         return this.beanName;
168     }
169
170     /**
171      * Sets the mapped bean's user defined name.
172      *
173      * @param beanName The mapped bean's user defined name
174      */

175     public void setBeanName(final String JavaDoc beanName) {
176         this.beanName = StringUtils.trim(beanName);
177     }
178
179     /**
180      * Adds a new field mapping for this bean mapping.
181      *
182      * @param fieldMapping the field mapping to add
183      */

184     public void addFieldMapping(final CSVFieldMapping fieldMapping) {
185         fields.add(fieldMapping);
186         maxFieldPosition = Math.max(maxFieldPosition, fieldMapping
187                 .getFieldPosition());
188     }
189
190     /**
191      * Returns the maximum (i.e. highest) field position present in this bean mapping. All field positions start from
192      * zero.
193      *
194      * @return Returns the highest field position
195      */

196     public int getMaxFieldPosition() {
197         return this.maxFieldPosition;
198     }
199
200     /**
201      * Indicates whether a header row is present in the CSV mapping.
202      *
203      * @return Returns <code>true</code> if the mapped CSV file or stream has a header
204      */

205     public boolean isCsvHeaderPresent() {
206         return this.csvHeaderPresent;
207     }
208
209     /**
210      * Sets the flag which indicates whether a header row is present in the mapped CSV file or stream.
211      *
212      * @param csvHeaderPresent The flag value to set
213      */

214     public void setCsvHeaderPresent(final boolean csvHeaderPresent) {
215         this.csvHeaderPresent = csvHeaderPresent;
216     }
217 }
218
Popular Tags