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.util;
17  
18  import java.io.Serializable;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  /**
23   * This object represents the current context of a given CSV file being either read or written to. The lineNumber is the
24   * actual line number (beginning at 1) of the file being read or written to. The rowNumber (beginning at 1) is the
25   * number of the CSV row (which will be identical to lineNumber if no rows span multiple lines) - the last rowNumber
26   * will correspond with the number of CSV records. The columnNumber (beginning at 1) is the number of the CSV column.
27   * 
28   * @author Kasper B. Graversen
29   * @author James Bassett
30   */
31  public final class CsvContext implements Serializable {
32  	
33  	private static final long serialVersionUID = 1L;
34  	
35  	/** the line number of the file being read/written */
36  	private int lineNumber;
37  	
38  	/** the CSV row number (CSV rows can span multiple lines) */
39  	private int rowNumber;
40  	
41  	/** the CSV column number */
42  	private int columnNumber;
43  	
44  	/** the row just read in, or to be written */
45  	private List<Object> rowSource;
46  	
47  	/**
48  	 * Constructs a new <tt>CsvContext</tt>.
49  	 * 
50  	 * @param lineNumber
51  	 *            the current line number
52  	 * @param rowNumber
53  	 *            the current CSV row number
54  	 * @param columnNumber
55  	 *            the current CSV column number
56  	 */
57  	public CsvContext(final int lineNumber, final int rowNumber, final int columnNumber) {
58  		this.lineNumber = lineNumber;
59  		this.rowNumber = rowNumber;
60  		this.columnNumber = columnNumber;
61  	}
62  	
63  	/**
64  	 * Constructs a new <tt>CsvContext</tt> that is a copy of the provided <tt>CsvContext</tt>. 
65  	 * 
66  	 * @param c the context to be copied
67  	 */
68  	public CsvContext(final CsvContext c)  {
69  		this (c.lineNumber, c.rowNumber, c.columnNumber);
70  		
71  		if (c.rowSource != null) {
72  			// Shallow clone is OK here. A deep clone implementation would be tricky
73  			// because the declared type of the items in the array is "Object" which does not 
74  			// have an exposed copy constructor or clone method.
75  			this.rowSource = new ArrayList<Object>(c.rowSource);
76  		}
77  	}
78  	
79  	/**
80  	 * @return the lineNumber
81  	 */
82  	public int getLineNumber() {
83  		return lineNumber;
84  	}
85  	
86  	/**
87  	 * @param lineNumber
88  	 *            the lineNumber to set
89  	 */
90  	public void setLineNumber(int lineNumber) {
91  		this.lineNumber = lineNumber;
92  	}
93  	
94  	/**
95  	 * @return the rowNumber
96  	 */
97  	public int getRowNumber() {
98  		return rowNumber;
99  	}
100 	
101 	/**
102 	 * @param rowNumber
103 	 *            the rowNumber to set
104 	 */
105 	public void setRowNumber(int rowNumber) {
106 		this.rowNumber = rowNumber;
107 	}
108 	
109 	/**
110 	 * @return the columnNumber
111 	 */
112 	public int getColumnNumber() {
113 		return columnNumber;
114 	}
115 	
116 	/**
117 	 * @param columnNumber
118 	 *            the columnNumber to set
119 	 */
120 	public void setColumnNumber(int columnNumber) {
121 		this.columnNumber = columnNumber;
122 	}
123 	
124 	/**
125 	 * @return the rowSource
126 	 */
127 	public List<Object> getRowSource() {
128 		return rowSource;
129 	}
130 	
131 	/**
132 	 * @param rowSource
133 	 *            the rowSource to set
134 	 */
135 	public void setRowSource(List<Object> rowSource) {
136 		this.rowSource = rowSource;
137 	}
138 	
139 	/**
140 	 * {@inheritDoc}
141 	 */
142 	@Override
143 	public String toString() {
144 		return String.format("{lineNo=%d, rowNo=%d, columnNo=%d, rowSource=%s}", lineNumber, rowNumber, columnNumber,
145 			rowSource);
146 	}
147 	
148 	/**
149 	 * {@inheritDoc}
150 	 */
151 	@Override
152 	public int hashCode() {
153 		final int prime = 31;
154 		int result = 1;
155 		result = prime * result + columnNumber;
156 		result = prime * result + rowNumber;
157 		result = prime * result + lineNumber;
158 		result = prime * result + ((rowSource == null) ? 0 : rowSource.hashCode());
159 		return result;
160 	}
161 	
162 	/**
163 	 * {@inheritDoc}
164 	 */
165 	@Override
166 	public boolean equals(Object obj) {
167 		if( this == obj ) {
168 			return true;
169 		}
170 		if( obj == null ) {
171 			return false;
172 		}
173 		if( getClass() != obj.getClass() ) {
174 			return false;
175 		}
176 		final CsvContext other = (CsvContext) obj;
177 		if( columnNumber != other.columnNumber ) {
178 			return false;
179 		}
180 		if( rowNumber != other.rowNumber ) {
181 			return false;
182 		}
183 		if( lineNumber != other.lineNumber ) {
184 			return false;
185 		}
186 		if( rowSource == null ) {
187 			if( other.rowSource != null ) {
188 				return false;
189 			}
190 		} else if( !rowSource.equals(other.rowSource) ) {
191 			return false;
192 		}
193 		return true;
194 	}
195 	
196 }