Skip to content

Commit 6ab9b46

Browse files
committed
Use final keyword where possible.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397122 13f79535-47bb-0310-9956-ffa450edef68
1 parent db37436 commit 6ab9b46

19 files changed

Lines changed: 308 additions & 307 deletions

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ public class CSVFormat implements Serializable {
155155
* @param header
156156
* the header
157157
*/
158-
CSVFormat(char delimiter, char encapsulator, char commentStart, char escape, boolean surroundingSpacesIgnored,
159-
boolean emptyLinesIgnored, String lineSeparator, String[] header) {
158+
CSVFormat(final char delimiter, final char encapsulator, final char commentStart, final char escape, final boolean surroundingSpacesIgnored,
159+
final boolean emptyLinesIgnored, final String lineSeparator, final String[] header) {
160160
this.delimiter = delimiter;
161161
this.encapsulator = encapsulator;
162162
this.commentStart = commentStart;
@@ -178,7 +178,7 @@ public class CSVFormat implements Serializable {
178178
*
179179
* @return true if <code>c</code> is a line break character
180180
*/
181-
private static boolean isLineBreak(char c) {
181+
private static boolean isLineBreak(final char c) {
182182
return c == '\n' || c == '\r';
183183
}
184184

@@ -230,7 +230,7 @@ public char getDelimiter() {
230230
* @throws IllegalArgumentException
231231
* thrown if the specified character is a line break
232232
*/
233-
public CSVFormat withDelimiter(char delimiter) {
233+
public CSVFormat withDelimiter(final char delimiter) {
234234
if (isLineBreak(delimiter)) {
235235
throw new IllegalArgumentException("The delimiter cannot be a line break");
236236
}
@@ -256,7 +256,7 @@ public char getEncapsulator() {
256256
* @throws IllegalArgumentException
257257
* thrown if the specified character is a line break
258258
*/
259-
public CSVFormat withEncapsulator(char encapsulator) {
259+
public CSVFormat withEncapsulator(final char encapsulator) {
260260
if (isLineBreak(encapsulator)) {
261261
throw new IllegalArgumentException("The encapsulator cannot be a line break");
262262
}
@@ -293,7 +293,7 @@ public char getCommentStart() {
293293
* @throws IllegalArgumentException
294294
* thrown if the specified character is a line break
295295
*/
296-
public CSVFormat withCommentStart(char commentStart) {
296+
public CSVFormat withCommentStart(final char commentStart) {
297297
if (isLineBreak(commentStart)) {
298298
throw new IllegalArgumentException("The comment start character cannot be a line break");
299299
}
@@ -330,7 +330,7 @@ public char getEscape() {
330330
* @throws IllegalArgumentException
331331
* thrown if the specified character is a line break
332332
*/
333-
public CSVFormat withEscape(char escape) {
333+
public CSVFormat withEscape(final char escape) {
334334
if (isLineBreak(escape)) {
335335
throw new IllegalArgumentException("The escape character cannot be a line break");
336336
}
@@ -365,7 +365,7 @@ public boolean getIgnoreSurroundingSpaces() {
365365
* spaces as is.
366366
* @return A copy of this format with the specified trimming behavior.
367367
*/
368-
public CSVFormat withIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces) {
368+
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
369369
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
370370
ignoreEmptyLines, lineSeparator, header);
371371
}
@@ -388,7 +388,7 @@ public boolean getIgnoreEmptyLines() {
388388
* <tt>false</tt> to translate empty lines to empty records.
389389
* @return A copy of this format with the specified empty line skipping behavior.
390390
*/
391-
public CSVFormat withIgnoreEmptyLines(boolean ignoreEmptyLines) {
391+
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
392392
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
393393
ignoreEmptyLines, lineSeparator, header);
394394
}
@@ -410,7 +410,7 @@ public String getLineSeparator() {
410410
*
411411
* @return A copy of this format using the specified output line separator
412412
*/
413-
public CSVFormat withLineSeparator(String lineSeparator) {
413+
public CSVFormat withLineSeparator(final String lineSeparator) {
414414
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
415415
ignoreEmptyLines, lineSeparator, header);
416416
}
@@ -438,7 +438,7 @@ String[] getHeader() {
438438
*
439439
* @return A copy of this format using the specified header
440440
*/
441-
public CSVFormat withHeader(String... header) {
441+
public CSVFormat withHeader(final String... header) {
442442
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
443443
ignoreEmptyLines, lineSeparator, header);
444444
}
@@ -449,7 +449,7 @@ public CSVFormat withHeader(String... header) {
449449
* @param in
450450
* the input stream
451451
*/
452-
public Iterable<CSVRecord> parse(Reader in) throws IOException {
452+
public Iterable<CSVRecord> parse(final Reader in) throws IOException {
453453
return new CSVParser(in, this);
454454
}
455455

@@ -459,20 +459,20 @@ public Iterable<CSVRecord> parse(Reader in) throws IOException {
459459
* @param values
460460
* the values to format
461461
*/
462-
public String format(String... values) {
463-
StringWriter out = new StringWriter();
462+
public String format(final String... values) {
463+
final StringWriter out = new StringWriter();
464464
try {
465465
new CSVPrinter(out, this).println(values);
466466
return out.toString().trim();
467-
} catch (IOException e) {
467+
} catch (final IOException e) {
468468
// should not happen
469469
throw new IllegalStateException(e);
470470
}
471471
}
472472

473473
@Override
474474
public String toString() {
475-
StringBuilder sb = new StringBuilder();
475+
final StringBuilder sb = new StringBuilder();
476476
sb.append("Delimiter=<").append(delimiter).append('>');
477477
if (isEscaping()) {
478478
sb.append(' ');

src/main/java/org/apache/commons/csv/CSVLexer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
class CSVLexer extends Lexer {
2929

3030
// ctor needs to be public so can be called dynamically by PerformanceTest class
31-
public CSVLexer(CSVFormat format, ExtendedBufferedReader in) {
31+
public CSVLexer(final CSVFormat format, final ExtendedBufferedReader in) {
3232
super(format, in);
3333
}
3434

@@ -44,7 +44,7 @@ public CSVLexer(CSVFormat format, ExtendedBufferedReader in) {
4444
* on stream access error
4545
*/
4646
@Override
47-
Token nextToken(Token token) throws IOException {
47+
Token nextToken(final Token token) throws IOException {
4848

4949
// get the last read char (required for empty line detection)
5050
int lastChar = in.readAgain();
@@ -81,7 +81,7 @@ Token nextToken(Token token) throws IOException {
8181
}
8282

8383
if (isStartOfLine(lastChar) && isCommentStart(c)) {
84-
String comment = in.readLine().trim();
84+
final String comment = in.readLine().trim();
8585
token.content.append(comment);
8686
token.type = COMMENT;
8787
return token;
@@ -141,7 +141,7 @@ Token nextToken(Token token) throws IOException {
141141
* @throws IOException
142142
* on stream access error
143143
*/
144-
private Token simpleTokenLexer(Token tkn, int c) throws IOException {
144+
private Token simpleTokenLexer(final Token tkn, int c) throws IOException {
145145
// Faster to use while(true)+break than while(tkn.type == INVALID)
146146
while (true) {
147147
if (isEndOfLine(c)) {
@@ -190,9 +190,9 @@ private Token simpleTokenLexer(Token tkn, int c) throws IOException {
190190
* @throws IOException
191191
* on invalid state: EOF before closing encapsulator or invalid character before delimiter or EOL
192192
*/
193-
private Token encapsulatedTokenLexer(Token tkn) throws IOException {
193+
private Token encapsulatedTokenLexer(final Token tkn) throws IOException {
194194
// save current line number in case needed for IOE
195-
int startLineNumber = getLineNumber();
195+
final int startLineNumber = getLineNumber();
196196
int c;
197197
while (true) {
198198
c = in.read();

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class CSVParser implements Iterable<CSVRecord> {
8585
* @throws IllegalArgumentException
8686
* thrown if the parameters of the format are inconsistent
8787
*/
88-
public CSVParser(Reader input) throws IOException {
88+
public CSVParser(final Reader input) throws IOException {
8989
this(input, CSVFormat.DEFAULT);
9090
}
9191

@@ -99,7 +99,7 @@ public CSVParser(Reader input) throws IOException {
9999
* @throws IllegalArgumentException
100100
* thrown if the parameters of the format are inconsistent
101101
*/
102-
public CSVParser(Reader input, CSVFormat format) throws IOException {
102+
public CSVParser(final Reader input, final CSVFormat format) throws IOException {
103103
format.validate();
104104

105105
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
@@ -117,7 +117,7 @@ public CSVParser(Reader input, CSVFormat format) throws IOException {
117117
* @throws IllegalArgumentException
118118
* thrown if the parameters of the format are inconsistent
119119
*/
120-
public CSVParser(String input, CSVFormat format) throws IOException {
120+
public CSVParser(final String input, final CSVFormat format) throws IOException {
121121
this(new StringReader(input), format);
122122
}
123123

@@ -204,7 +204,7 @@ CSVRecord getRecord() throws IOException {
204204
* on parse error or input read-failure
205205
*/
206206
public List<CSVRecord> getRecords() throws IOException {
207-
List<CSVRecord> records = new ArrayList<CSVRecord>();
207+
final List<CSVRecord> records = new ArrayList<CSVRecord>();
208208
CSVRecord rec;
209209
while ((rec = getRecord()) != null) {
210210
records.add(rec);
@@ -215,15 +215,15 @@ public List<CSVRecord> getRecords() throws IOException {
215215
/**
216216
* Initializes the name to index mapping if the format defines a header.
217217
*/
218-
private Map<String, Integer> initializeHeader(CSVFormat format) throws IOException {
218+
private Map<String, Integer> initializeHeader(final CSVFormat format) throws IOException {
219219
Map<String, Integer> hdrMap = null;
220220
if (format.getHeader() != null) {
221221
hdrMap = new LinkedHashMap<String, Integer>();
222222

223223
String[] header = null;
224224
if (format.getHeader().length == 0) {
225225
// read the header from the first line of the file
226-
CSVRecord rec = getRecord();
226+
final CSVRecord rec = getRecord();
227227
if (rec != null) {
228228
header = rec.values();
229229
}
@@ -252,7 +252,7 @@ public Iterator<CSVRecord> iterator() {
252252
private CSVRecord getNextRecord() {
253253
try {
254254
return getRecord();
255-
} catch (IOException e) {
255+
} catch (final IOException e) {
256256
throw new RuntimeException(e);
257257
}
258258
}

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class CSVPrinter {
4545
* @throws IllegalArgumentException
4646
* thrown if the parameters of the format are inconsistent
4747
*/
48-
public CSVPrinter(Appendable out, CSVFormat format) {
48+
public CSVPrinter(final Appendable out, final CSVFormat format) {
4949
this.out = out;
5050
this.format = format == null ? CSVFormat.DEFAULT : format;
5151
this.format.validate();
@@ -81,8 +81,8 @@ public void flush() throws IOException {
8181
* @param values
8282
* values to be outputted.
8383
*/
84-
public void println(String... values) throws IOException {
85-
for (String value : values) {
84+
public void println(final String... values) throws IOException {
85+
for (final String value : values) {
8686
print(value);
8787
}
8888
println();
@@ -98,7 +98,7 @@ public void println(String... values) throws IOException {
9898
* @param comment
9999
* the comment to output
100100
*/
101-
public void printComment(String comment) throws IOException {
101+
public void printComment(final String comment) throws IOException {
102102
if (!format.isCommentingEnabled()) {
103103
return;
104104
}
@@ -108,7 +108,7 @@ public void printComment(String comment) throws IOException {
108108
out.append(format.getCommentStart());
109109
out.append(' ');
110110
for (int i = 0; i < comment.length(); i++) {
111-
char c = comment.charAt(i);
111+
final char c = comment.charAt(i);
112112
switch (c) {
113113
case '\r':
114114
if (i + 1 < comment.length() && comment.charAt(i + 1) == '\n') {
@@ -128,7 +128,7 @@ public void printComment(String comment) throws IOException {
128128
println();
129129
}
130130

131-
private void print(CharSequence value, int offset, int len) throws IOException {
131+
private void print(final CharSequence value, final int offset, final int len) throws IOException {
132132
if (format.isEncapsulating()) {
133133
printAndEncapsulate(value, offset, len);
134134
} else if (format.isEscaping()) {
@@ -147,15 +147,15 @@ void printSep() throws IOException {
147147
}
148148
}
149149

150-
void printAndEscape(CharSequence value, int offset, int len) throws IOException {
150+
void printAndEscape(final CharSequence value, final int offset, final int len) throws IOException {
151151
int start = offset;
152152
int pos = offset;
153-
int end = offset + len;
153+
final int end = offset + len;
154154

155155
printSep();
156156

157-
char delim = format.getDelimiter();
158-
char escape = format.getEscape();
157+
final char delim = format.getDelimiter();
158+
final char escape = format.getEscape();
159159

160160
while (pos < end) {
161161
char c = value.charAt(pos);
@@ -185,17 +185,17 @@ void printAndEscape(CharSequence value, int offset, int len) throws IOException
185185
}
186186
}
187187

188-
void printAndEncapsulate(CharSequence value, int offset, int len) throws IOException {
189-
boolean first = newLine; // is this the first value on this line?
188+
void printAndEncapsulate(final CharSequence value, final int offset, final int len) throws IOException {
189+
final boolean first = newLine; // is this the first value on this line?
190190
boolean quote = false;
191191
int start = offset;
192192
int pos = offset;
193-
int end = offset + len;
193+
final int end = offset + len;
194194

195195
printSep();
196196

197-
char delim = format.getDelimiter();
198-
char encapsulator = format.getEncapsulator();
197+
final char delim = format.getDelimiter();
198+
final char encapsulator = format.getEncapsulator();
199199

200200
if (len <= 0) {
201201
// always quote an empty token that is the first
@@ -252,7 +252,7 @@ void printAndEncapsulate(CharSequence value, int offset, int len) throws IOExcep
252252
// Pick up where we left off: pos should be positioned on the first character that caused
253253
// the need for encapsulation.
254254
while (pos < end) {
255-
char c = value.charAt(pos);
255+
final char c = value.charAt(pos);
256256
if (c == encapsulator) {
257257
// write out the chunk up until this point
258258

@@ -277,7 +277,7 @@ void printAndEncapsulate(CharSequence value, int offset, int len) throws IOExcep
277277
* @param value
278278
* value to be outputted.
279279
*/
280-
public void print(String value, boolean checkForEscape) throws IOException {
280+
public void print(String value, final boolean checkForEscape) throws IOException {
281281
if (value == null) {
282282
// null values are considered empty
283283
value = "";
@@ -298,7 +298,7 @@ public void print(String value, boolean checkForEscape) throws IOException {
298298
* @param value
299299
* value to be outputted.
300300
*/
301-
public void print(String value) throws IOException {
301+
public void print(final String value) throws IOException {
302302
print(value, true);
303303
}
304304
}

src/main/java/org/apache/commons/csv/CSVRecord.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class CSVRecord implements Serializable, Iterable<String> {
4040
/** The accumulated comments (if any) */
4141
private final String comment;
4242

43-
CSVRecord(String[] values, Map<String, Integer> mapping, String comment) {
43+
CSVRecord(final String[] values, final Map<String, Integer> mapping, final String comment) {
4444
this.values = values != null ? values : EMPTY_STRING_ARRAY;
4545
this.mapping = mapping;
4646
this.comment = comment;
@@ -52,7 +52,7 @@ public class CSVRecord implements Serializable, Iterable<String> {
5252
* @param i
5353
* the index of the column retrieved
5454
*/
55-
public String get(int i) {
55+
public String get(final int i) {
5656
return values[i];
5757
}
5858

@@ -65,12 +65,12 @@ public String get(int i) {
6565
* @throws IllegalStateException
6666
* if no header mapping was provided
6767
*/
68-
public String get(String name) {
68+
public String get(final String name) {
6969
if (mapping == null) {
7070
throw new IllegalStateException("No header mapping was specified, the record values can't be accessed by name");
7171
}
7272

73-
Integer index = mapping.get(name);
73+
final Integer index = mapping.get(name);
7474

7575
return index != null ? values[index.intValue()] : null;
7676
}
@@ -82,7 +82,7 @@ public String get(String name) {
8282
* the name of the column to be retrieved.
8383
* @return whether a given columns is mapped.
8484
*/
85-
public boolean isMapped(String name) {
85+
public boolean isMapped(final String name) {
8686
return mapping != null ? mapping.containsKey(name) : false;
8787
}
8888

0 commit comments

Comments
 (0)