Skip to content

Commit

Permalink
Remove explicit Properties.File#getContent() method
Browse files Browse the repository at this point in the history
While indeed the LST should not be modified by modifying a `List` returned by a getter, it is also not right for a getter to return a new list every time (e.g. by wrapping an internal field using `Collections.unmodifiableList()`.

This commit removes the explicit getter and wither and instead has these generated by Lombok. Further, the initial contents, as populated by the parser, are now wrapped using `Collections.unmodifiableList()` to avoid any situations, where the contents are directly modified.
  • Loading branch information
knutwannheden committed Mar 12, 2024
1 parent 0b23677 commit b882a66
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

Expand Down Expand Up @@ -103,7 +104,7 @@ private Properties.File parseFromInput(Path sourceFile, EncodingDetectingInputSt
"",
Markers.EMPTY,
sourceFile,
contents,
Collections.unmodifiableList(contents),
prefix.toString(),
source.getCharset().name(),
source.isCharsetBomMarked(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -70,6 +68,7 @@ class File implements Properties, SourceFile {
@With
Path sourcePath;

@With
List<Content> content;
@With
String eof;
Expand Down Expand Up @@ -100,28 +99,6 @@ public SourceFile withCharset(Charset charset) {
return withCharsetName(charset.name());
}

public List<Content> getContent() {
return Collections.unmodifiableList(content);
}

public File withContent(List<Content> content) {
int size = content.size();
if (size == this.content.size()) {
boolean allIdentical = true;
for (int i = 0; i < size; i++) {
if (content.get(i) != this.content.get(i)) {
allIdentical = false;
break;
}
}
if (allIdentical) {
return this;
}
}

return new File(id, prefix, markers, sourcePath, new ArrayList<>(content), eof, charsetName, charsetBomMarked, fileAttributes, checksum);
}

@Override
public <P> Properties acceptProperties(PropertiesVisitor<P> v, P p) {
return v.visitFile(this, p);
Expand Down

0 comments on commit b882a66

Please sign in to comment.