Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Commit 637ad2d

Browse files
committed
CSV-216: Avoid sharing CSVRecord.comment field
final modifiers to most methods ... not sure what CSVMutableRecord class is needed for now!
1 parent c66ea98 commit 637ad2d

2 files changed

Lines changed: 29 additions & 35 deletions

File tree

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,19 @@ final class CSVMutableRecord extends CSVRecord {
4141
}
4242

4343
@Override
44-
public CSVMutableRecord withValue(int index, String value) {
45-
super.put(index, value);
44+
public final CSVMutableRecord withValue(int index, String value) {
45+
put(index, value);
4646
return this;
4747
}
4848

4949
@Override
50-
public CSVMutableRecord withValue(String name, String value) {
51-
super.put(name, value);
50+
public final CSVMutableRecord withValue(String name, String value) {
51+
put(name, value);
5252
return this;
5353
}
5454

5555
@Override
56-
public CSVRecord withComment(String comment) {
57-
this.comment = comment;
58-
return this;
59-
}
60-
61-
@Override
62-
boolean isMutable() {
56+
final boolean isMutable() {
6357
return true;
6458
}
6559
}

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

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public class CSVRecord implements Serializable, Iterable<String> {
3838

3939
/** The accumulated comments (if any)
4040
*
41-
* package-private so it can be mutated by {@link CSVMutableRecord}
41+
* non-final so it can be mutated by {@link CSVMutableRecord}
4242
*/
43-
String comment;
43+
private String comment;
4444

4545
/** The column name to index mapping. */
4646
private final Map<String, Integer> mapping;
@@ -67,7 +67,7 @@ public class CSVRecord implements Serializable, Iterable<String> {
6767
* an enum
6868
* @return the String at the given enum String
6969
*/
70-
public String get(final Enum<?> e) {
70+
public final String get(final Enum<?> e) {
7171
return get(e.toString());
7272
}
7373

@@ -78,7 +78,7 @@ public String get(final Enum<?> e) {
7878
* a column index (0-based)
7979
* @return the String at the given index
8080
*/
81-
public String get(final int i) {
81+
public final String get(final int i) {
8282
return values[i];
8383
}
8484

@@ -95,7 +95,7 @@ public String get(final int i) {
9595
* @see #isConsistent()
9696
* @see CSVFormat#withNullString(String)
9797
*/
98-
public String get(final String name) {
98+
public final String get(final String name) {
9999
if (mapping == null) {
100100
throw new IllegalStateException(
101101
"No header mapping was specified, the record values can't be accessed by name");
@@ -110,7 +110,7 @@ public String get(final String name) {
110110
}
111111
}
112112

113-
int getIndex(final String name) {
113+
final int getIndex(final String name) {
114114
final Integer integerIndex = mapping.get(name);
115115
if (integerIndex == null) {
116116
throw new IllegalArgumentException(
@@ -126,7 +126,7 @@ int getIndex(final String name) {
126126
*
127127
* @return the position of this record in the source stream.
128128
*/
129-
public long getCharacterPosition() {
129+
public final long getCharacterPosition() {
130130
return characterPosition;
131131
}
132132

@@ -138,7 +138,7 @@ public long getCharacterPosition() {
138138
*
139139
* @return the comment for this record, or null if no comment for this record is available.
140140
*/
141-
public String getComment() {
141+
public final String getComment() {
142142
return comment;
143143
}
144144

@@ -153,7 +153,7 @@ public String getComment() {
153153
* @return the number of this record.
154154
* @see CSVParser#getCurrentLineNumber()
155155
*/
156-
public long getRecordNumber() {
156+
public final long getRecordNumber() {
157157
return recordNumber;
158158
}
159159

@@ -167,7 +167,7 @@ public long getRecordNumber() {
167167
*
168168
* @return true of this record is valid, false if not
169169
*/
170-
public boolean isConsistent() {
170+
public final boolean isConsistent() {
171171
return mapping == null || mapping.size() == values.length;
172172
}
173173

@@ -180,7 +180,7 @@ public boolean isConsistent() {
180180
* @return true if this record has a comment, false otherwise
181181
* @since 1.3
182182
*/
183-
public boolean hasComment() {
183+
public final boolean hasComment() {
184184
return comment != null;
185185
}
186186

@@ -196,7 +196,7 @@ public boolean hasComment() {
196196
*
197197
* @return Am immutable CSVRecord
198198
*/
199-
public CSVRecord immutable() {
199+
public final CSVRecord immutable() {
200200
if (isMutable()) {
201201
// Subclass is probably CSVMutableRecord, freeze values
202202
String[] frozenValue = Arrays.copyOf(values, values.length);
@@ -213,7 +213,7 @@ public CSVRecord immutable() {
213213
* the name of the column to be retrieved.
214214
* @return whether a given column is mapped.
215215
*/
216-
public boolean isMapped(final String name) {
216+
public final boolean isMapped(final String name) {
217217
return mapping != null && mapping.containsKey(name);
218218
}
219219

@@ -228,7 +228,7 @@ boolean isMutable() {
228228
* the name of the column to be retrieved.
229229
* @return whether a given columns is mapped and has a value
230230
*/
231-
public boolean isSet(final String name) {
231+
public final boolean isSet(final String name) {
232232
return isMapped(name) && mapping.get(name).intValue() < values.length;
233233
}
234234

@@ -238,7 +238,7 @@ public boolean isSet(final String name) {
238238
* @return an iterator over the values of this record.
239239
*/
240240
@Override
241-
public Iterator<String> iterator() {
241+
public final Iterator<String> iterator() {
242242
return toList().iterator();
243243
}
244244

@@ -256,19 +256,19 @@ public Iterator<String> iterator() {
256256
*
257257
* @return A mutable CSVRecord
258258
*/
259-
public CSVRecord mutable() {
259+
public final CSVRecord mutable() {
260260
if (isMutable()) {
261261
return this;
262262
}
263263
String[] newValues = Arrays.copyOf(values, values.length);
264264
return new CSVMutableRecord(newValues, mapping, comment, recordNumber, characterPosition);
265265
}
266266

267-
void put(final int index, String value) {
267+
final void put(final int index, String value) {
268268
values[index] = value;
269269
}
270270

271-
void put(final String name, String value) {
271+
final void put(final String name, String value) {
272272
values[getIndex(name)] = value;
273273
}
274274

@@ -279,7 +279,7 @@ void put(final String name, String value) {
279279
* The Map to populate.
280280
* @return the given map.
281281
*/
282-
<M extends Map<String, String>> M putIn(final M map) {
282+
final <M extends Map<String, String>> M putIn(final M map) {
283283
if (mapping == null) {
284284
return map;
285285
}
@@ -297,7 +297,7 @@ <M extends Map<String, String>> M putIn(final M map) {
297297
*
298298
* @return the number of values.
299299
*/
300-
public int size() {
300+
public final int size() {
301301
return values.length;
302302
}
303303

@@ -317,7 +317,7 @@ private List<String> toList() {
317317
*
318318
* @return A new Map. The map is empty if the record has no headers.
319319
*/
320-
public Map<String, String> toMap() {
320+
public final Map<String, String> toMap() {
321321
return putIn(new HashMap<String, String>(values.length));
322322
}
323323

@@ -328,13 +328,13 @@ public Map<String, String> toMap() {
328328
* @return a String representation of this record.
329329
*/
330330
@Override
331-
public String toString() {
331+
public final String toString() {
332332
return "CSVRecord [comment=" + comment + ", mapping=" + mapping +
333333
", recordNumber=" + recordNumber + ", values=" +
334334
Arrays.toString(values) + "]";
335335
}
336336

337-
String[] values() {
337+
final String[] values() {
338338
return values;
339339
}
340340

@@ -379,7 +379,7 @@ public CSVRecord withValue(int index, String value) {
379379
* the comment to set, or <code>null</code> for no comment.
380380
* @return A mutated CSVRecord
381381
*/
382-
public CSVRecord withComment(String comment) {
382+
public final CSVRecord withComment(String comment) {
383383
CSVRecord r = mutable();
384384
r.comment = comment;
385385
return r;

0 commit comments

Comments
 (0)