Skip to content

Commit d7239c3

Browse files
committed
chore: Use modern language features
1 parent 6b9e291 commit d7239c3

4 files changed

Lines changed: 8 additions & 30 deletions

File tree

src/main/java/io/github/helpermethod/zipforge/DirectoryNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.io.InputStream;
55
import java.nio.file.Path;
66
import java.util.ArrayList;
7-
import java.util.Collections;
87
import java.util.List;
98

109
public class DirectoryNode implements Node {
@@ -33,6 +32,6 @@ Path path() {
3332
}
3433

3534
List<Node> children() {
36-
return Collections.unmodifiableList(children);
35+
return List.copyOf(children);
3736
}
3837
}

src/main/java/io/github/helpermethod/zipforge/FileNode.java

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,9 @@
44
import java.io.InputStream;
55
import java.nio.file.Path;
66

7-
class FileNode implements Node {
8-
private final Path path;
9-
private final InputStream content;
10-
11-
FileNode(Path path, InputStream content) {
12-
this.path = path;
13-
this.content = content;
14-
}
15-
7+
public record FileNode(Path path, InputStream content) implements Node {
168
@Override
179
public void accept(Visitor visitor) throws IOException {
1810
visitor.visit(this);
1911
}
20-
21-
Path path() {
22-
return path;
23-
}
24-
25-
InputStream content() {
26-
return content;
27-
}
2812
}

src/main/java/io/github/helpermethod/zipforge/ZipFileVisitor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.helpermethod.zipforge;
22

33
import java.io.IOException;
4-
import java.io.InputStream;
54
import java.nio.file.FileSystem;
65
import java.nio.file.Files;
76
import java.nio.file.StandardCopyOption;
@@ -16,7 +15,7 @@ class ZipFileVisitor implements Visitor {
1615

1716
@Override
1817
public void visit(FileNode file) throws IOException {
19-
try (InputStream content = file.content()) {
18+
try (var content = file.content()) {
2019
Files.copy(content, zipFileSystem.getPath(file.path().toString()), StandardCopyOption.REPLACE_EXISTING);
2120
}
2221
}
@@ -25,7 +24,7 @@ public void visit(FileNode file) throws IOException {
2524
public void visit(DirectoryNode directory) throws IOException {
2625
Files.createDirectories(zipFileSystem.getPath(directory.path().toString()));
2726

28-
for (Node node : directory.children()) {
27+
for (var node : directory.children()) {
2928
node.accept(this);
3029
}
3130
}

src/main/java/io/github/helpermethod/zipforge/ZipForge.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
import java.io.ByteArrayInputStream;
66
import java.io.IOException;
77
import java.net.URI;
8-
import java.nio.file.FileSystem;
98
import java.nio.file.FileSystems;
109
import java.nio.file.Files;
1110
import java.nio.file.Path;
1211
import java.nio.file.Paths;
1312
import java.util.ArrayDeque;
1413
import java.util.Deque;
15-
import java.util.HashMap;
1614
import java.util.Map;
1715

1816
public class ZipForge {
@@ -21,7 +19,7 @@ public class ZipForge {
2119
private ZipForge() {}
2220

2321
public static Path createZipFile(Path path, NodeGroup nodeGroup) throws IOException {
24-
DirectoryNode rootNode = new DirectoryNode(Paths.get(""));
22+
var rootNode = new DirectoryNode(Paths.get(""));
2523

2624
try {
2725
nodeDeque.get().addLast(rootNode);
@@ -30,10 +28,8 @@ public static Path createZipFile(Path path, NodeGroup nodeGroup) throws IOExcept
3028
nodeDeque.remove();
3129
}
3230

33-
Map<String, String> env = new HashMap<>();
34-
env.put("create", "true");
35-
36-
try (FileSystem zipFileSystem = FileSystems.newFileSystem(URI.create("jar:" + path.toUri()), env)) {
31+
try (var zipFileSystem =
32+
FileSystems.newFileSystem(URI.create("jar:" + path.toUri()), Map.of("create", "true"))) {
3733
new ZipFileVisitor(zipFileSystem).visit(rootNode);
3834
}
3935

@@ -57,7 +53,7 @@ public static void file(String name, Path content) {
5753
}
5854

5955
public static void directory(String name, NodeGroup nodeGroup) {
60-
DirectoryNode directoryNode = new DirectoryNode(path(name));
56+
var directoryNode = new DirectoryNode(path(name));
6157
nodeDeque.get().getLast().directory(directoryNode);
6258

6359
nodeDeque.get().addLast(directoryNode);

0 commit comments

Comments
 (0)