Skip to content

Commit dd82bfd

Browse files
seijikunme4502
authored andcommitted
Rename Schematic to SchematicPath and cleanup
- Resolve first batch of review comments - Renamed Schematic to SchematicPath - Removed custom hashCode() and equals() implementations - Added unit-test to make sure it behaves as expected
1 parent f6b0234 commit dd82bfd

9 files changed

Lines changed: 93 additions & 60 deletions

File tree

worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
import com.sk89q.worldedit.util.io.Closer;
5656
import com.sk89q.worldedit.util.io.file.FilenameException;
5757
import com.sk89q.worldedit.util.io.file.MorePaths;
58-
import com.sk89q.worldedit.util.schematic.Schematic;
58+
import com.sk89q.worldedit.util.schematic.SchematicPath;
5959
import com.sk89q.worldedit.util.schematic.SchematicsManager;
6060
import org.apache.logging.log4j.Logger;
6161
import org.enginehub.piston.annotation.Command;
@@ -73,10 +73,7 @@
7373
import java.io.FileOutputStream;
7474
import java.io.IOException;
7575
import java.io.OutputStream;
76-
import java.nio.file.DirectoryStream;
77-
import java.nio.file.Files;
7876
import java.nio.file.Path;
79-
import java.util.ArrayList;
8077
import java.util.Comparator;
8178
import java.util.List;
8279
import java.util.concurrent.Callable;
@@ -111,13 +108,13 @@ public SchematicCommands(WorldEdit worldEdit) {
111108
@CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load"})
112109
public void load(Actor actor, LocalSession session,
113110
@Arg(desc = "File name.")
114-
Schematic schematic,
111+
SchematicPath schematic,
115112
@Arg(desc = "Format name.", def = "sponge")
116113
ClipboardFormat format) throws FilenameException {
117114
LocalConfiguration config = worldEdit.getConfiguration();
118115

119116
// Schematic.path is relative, so treat it as filename
120-
String filename = schematic.getPath().toString();
117+
String filename = schematic.path().toString();
121118
File schematicsRoot = worldEdit.getSchematicsManager().getRoot().toFile();
122119
File f = worldEdit.getSafeOpenFile(actor, schematicsRoot, filename,
123120
BuiltInClipboardFormat.SPONGE_V3_SCHEMATIC.getPrimaryFileExtension(),
@@ -251,12 +248,12 @@ public void share(Actor actor, LocalSession session,
251248
@CommandPermissions("worldedit.schematic.delete")
252249
public void delete(Actor actor,
253250
@Arg(desc = "File name.")
254-
Schematic schematic) throws WorldEditException {
251+
SchematicPath schematic) throws WorldEditException {
255252
LocalConfiguration config = worldEdit.getConfiguration();
256253
File dir = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile();
257254

258255
// Schematic.path is relative, so treat it as filename
259-
String filename = schematic.getPath().toString();
256+
String filename = schematic.path().toString();
260257
File f = worldEdit.getSafeOpenFile(actor,
261258
dir, filename, "schematic", ClipboardFormats.getFileExtensionArray());
262259

@@ -335,7 +332,7 @@ public void list(Actor actor,
335332
final String pageCommand = actor.isPlayer()
336333
? "//schem list -p %page%" + flag : null;
337334

338-
Comparator<Schematic> schematicComparator = (s0, s1) -> pathComparator.compare(s0.getPath(), s1.getPath());
335+
Comparator<SchematicPath> schematicComparator = (s0, s1) -> pathComparator.compare(s0.path(), s1.path());
339336

340337
WorldEditAsyncCommandBuilder.createAndSendMessage(actor,
341338
new SchematicListTask(schematicComparator, page, pageCommand),
@@ -445,11 +442,11 @@ public Consumer<Actor> call() throws Exception {
445442
}
446443

447444
private static class SchematicListTask implements Callable<Component> {
448-
private final Comparator<Schematic> pathComparator;
445+
private final Comparator<SchematicPath> pathComparator;
449446
private final int page;
450447
private final String pageCommand;
451448

452-
SchematicListTask(Comparator<Schematic> pathComparator, int page, String pageCommand) {
449+
SchematicListTask(Comparator<SchematicPath> pathComparator, int page, String pageCommand) {
453450
this.pathComparator = pathComparator;
454451
this.page = page;
455452
this.pageCommand = pageCommand;
@@ -458,7 +455,7 @@ private static class SchematicListTask implements Callable<Component> {
458455
@Override
459456
public Component call() throws Exception {
460457
SchematicsManager schematicsManager = WorldEdit.getInstance().getSchematicsManager();
461-
List<Schematic> fileList = schematicsManager.getList();
458+
List<SchematicPath> fileList = schematicsManager.getList();
462459

463460
if (fileList.isEmpty()) {
464461
return ErrorFormat.wrap("No schematics found.");
@@ -473,9 +470,9 @@ public Component call() throws Exception {
473470

474471
private static class SchematicPaginationBox extends PaginationBox {
475472
private final Path rootDir;
476-
private final List<Schematic> files;
473+
private final List<SchematicPath> files;
477474

478-
SchematicPaginationBox(Path rootDir, List<Schematic> files, String pageCommand) {
475+
SchematicPaginationBox(Path rootDir, List<SchematicPath> files, String pageCommand) {
479476
super("Available schematics", pageCommand);
480477
this.rootDir = rootDir;
481478
this.files = files;
@@ -484,7 +481,7 @@ private static class SchematicPaginationBox extends PaginationBox {
484481
@Override
485482
public Component getComponent(int number) {
486483
checkArgument(number < files.size() && number >= 0);
487-
Path file = files.get(number).getPath();
484+
Path file = files.get(number).path();
488485

489486
String format = ClipboardFormats.getFileExtensionMap()
490487
.get(MoreFiles.getFileExtension(file))

worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020
package com.sk89q.worldedit.command.argument;
2121

2222
import com.sk89q.worldedit.WorldEdit;
23-
import com.sk89q.worldedit.internal.util.LogManagerCompat;
2423
import com.sk89q.worldedit.util.formatting.text.Component;
2524
import com.sk89q.worldedit.util.formatting.text.TextComponent;
2625
import com.sk89q.worldedit.util.io.file.FilenameException;
27-
import com.sk89q.worldedit.util.schematic.Schematic;
26+
import com.sk89q.worldedit.util.schematic.SchematicPath;
2827
import com.sk89q.worldedit.util.schematic.SchematicsManager;
2928
import org.enginehub.piston.CommandManager;
3029
import org.enginehub.piston.converter.ArgumentConverter;
@@ -40,10 +39,10 @@
4039

4140
import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix;
4241

43-
public class SchematicConverter implements ArgumentConverter<Schematic> {
42+
public class SchematicConverter implements ArgumentConverter<SchematicPath> {
4443

4544
public static void register(WorldEdit worldEdit, CommandManager commandManager) {
46-
commandManager.registerConverter(Key.of(Schematic.class), new SchematicConverter(worldEdit));
45+
commandManager.registerConverter(Key.of(SchematicPath.class), new SchematicConverter(worldEdit));
4746
}
4847

4948
private final WorldEdit worldEdit;
@@ -65,11 +64,11 @@ public List<String> getSuggestions(String input, InjectedValueAccess context) {
6564
Path schematicsRootPath = schematicsManager.getRoot();
6665

6766
return limitByPrefix(schematicsManager.getList().stream()
68-
.map(s -> schematicsRootPath.relativize(s.getPath()).toString()), input);
67+
.map(s -> schematicsRootPath.relativize(s.path()).toString()), input);
6968
}
7069

7170
@Override
72-
public ConversionResult<Schematic> convert(String s, InjectedValueAccess injectedValueAccess) {
71+
public ConversionResult<SchematicPath> convert(String s, InjectedValueAccess injectedValueAccess) {
7372
Path schematicsRoot = worldEdit.getSchematicsManager().getRoot();
7473
// resolve as subpath of schematicsRoot
7574
Path schematicPath = schematicsRoot.resolve(s).toAbsolutePath();
@@ -81,7 +80,7 @@ public ConversionResult<Schematic> convert(String s, InjectedValueAccess injecte
8180
if (Files.exists(schematicPath)) {
8281
// continue as relative path to schematicsRoot
8382
schematicPath = schematicsRoot.relativize(schematicPath);
84-
return SuccessfulConversion.fromSingle(new Schematic(schematicPath));
83+
return SuccessfulConversion.fromSingle(new SchematicPath(schematicPath));
8584
} else {
8685
return FailedConversion.from(new FilenameException(s));
8786
}

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/Schematic.java renamed to worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicPath.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,30 +24,11 @@
2424
/**
2525
* Record representing one Schematic file.
2626
*/
27-
public record Schematic(Path path) {
28-
29-
/**
30-
* Get this Schematic's path.
31-
* @return This Schematic's path.
32-
*/
33-
public Path getPath() {
34-
return path;
35-
}
27+
public record SchematicPath(Path path) {
3628

3729
@Override
3830
public String toString() {
3931
return path.toString();
4032
}
4133

42-
@Override
43-
public int hashCode() {
44-
return path.hashCode();
45-
}
46-
47-
@Override
48-
public boolean equals(Object obj) {
49-
Schematic other = (Schematic) obj;
50-
if (other == null) { return false; }
51-
return path.equals(other.getPath());
52-
}
5334
}

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicsManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public Path getRoot() {
102102
* Get a list of all known schematics.
103103
* @return List of all known schematics.
104104
*/
105-
public List<Schematic> getList() {
105+
public List<SchematicPath> getList() {
106106
return backend.getList();
107107
}
108108

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/DummySchematicsBackend.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919

2020
package com.sk89q.worldedit.util.schematic.backends;
2121

22-
import com.sk89q.worldedit.util.schematic.Schematic;
22+
import com.sk89q.worldedit.util.schematic.SchematicPath;
2323

24-
import java.nio.file.Path;
2524
import java.util.ArrayList;
2625
import java.util.List;
2726

@@ -39,7 +38,7 @@ public void uninit() {
3938
}
4039

4140
@Override
42-
public List<Schematic> getList() {
41+
public List<SchematicPath> getList() {
4342
return new ArrayList<>();
4443
}
4544

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/FileWatcherSchematicsBackend.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
import com.sk89q.worldedit.internal.util.LogManagerCompat;
2323
import com.sk89q.worldedit.util.io.file.RecursiveDirectoryWatcher;
24-
import com.sk89q.worldedit.util.schematic.Schematic;
24+
import com.sk89q.worldedit.util.schematic.SchematicPath;
2525
import org.apache.logging.log4j.Logger;
2626

2727
import java.io.IOException;
@@ -41,7 +41,7 @@ public class FileWatcherSchematicsBackend implements SchematicsBackend {
4141
private static final Logger LOGGER = LogManagerCompat.getLogger();
4242

4343
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
44-
private final Set<Schematic> schematics = new HashSet<>();
44+
private final Set<SchematicPath> schematics = new HashSet<>();
4545
private final RecursiveDirectoryWatcher directoryWatcher;
4646

4747
private FileWatcherSchematicsBackend(RecursiveDirectoryWatcher directoryWatcher) {
@@ -64,9 +64,9 @@ public void init() {
6464
directoryWatcher.start(event -> {
6565
lock.writeLock().lock();
6666
if (event instanceof RecursiveDirectoryWatcher.FileCreatedEvent) {
67-
schematics.add(new Schematic(event.getPath()));
67+
schematics.add(new SchematicPath(event.getPath()));
6868
} else if (event instanceof RecursiveDirectoryWatcher.FileDeletedEvent) {
69-
schematics.remove(new Schematic(event.getPath()));
69+
schematics.remove(new SchematicPath(event.getPath()));
7070
}
7171
lock.writeLock().unlock();
7272
if (event instanceof RecursiveDirectoryWatcher.FileCreatedEvent) {
@@ -84,9 +84,9 @@ public void uninit() {
8484
}
8585

8686
@Override
87-
public List<Schematic> getList() {
87+
public List<SchematicPath> getList() {
8888
lock.readLock().lock();
89-
List<Schematic> result = new ArrayList<>(schematics);
89+
List<SchematicPath> result = new ArrayList<>(schematics);
9090
lock.readLock().unlock();
9191
return result;
9292
}

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/PollingSchematicsBackend.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
package com.sk89q.worldedit.util.schematic.backends;
2121

2222
import com.sk89q.worldedit.internal.util.LogManagerCompat;
23-
import com.sk89q.worldedit.util.schematic.Schematic;
23+
import com.sk89q.worldedit.util.schematic.SchematicPath;
2424
import org.apache.logging.log4j.Logger;
2525

2626
import java.io.IOException;
@@ -46,7 +46,7 @@ public class PollingSchematicsBackend implements SchematicsBackend {
4646
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
4747
private final Path schematicsDir;
4848
private Instant lastUpdateTs = Instant.EPOCH;
49-
private List<Schematic> schematics = new ArrayList<>();
49+
private List<SchematicPath> schematics = new ArrayList<>();
5050

5151
private PollingSchematicsBackend(Path schematicsDir) {
5252
this.schematicsDir = schematicsDir;
@@ -61,14 +61,14 @@ public static PollingSchematicsBackend create(Path schematicsFolder) {
6161
return new PollingSchematicsBackend(schematicsFolder);
6262
}
6363

64-
private List<Schematic> scanFolder(Path root) {
65-
List<Schematic> pathList = new ArrayList<>();
64+
private List<SchematicPath> scanFolder(Path root) {
65+
List<SchematicPath> pathList = new ArrayList<>();
6666
try (DirectoryStream<Path> stream = Files.newDirectoryStream(root)) {
6767
for (Path path : stream) {
6868
if (Files.isDirectory(path)) {
6969
pathList.addAll(scanFolder(path));
7070
} else {
71-
pathList.add(new Schematic(path));
71+
pathList.add(new SchematicPath(path));
7272
}
7373
}
7474
} catch (IOException e) {
@@ -94,7 +94,7 @@ public void uninit() {
9494
}
9595

9696
@Override
97-
public synchronized List<Schematic> getList() {
97+
public synchronized List<SchematicPath> getList() {
9898
// udpate internal cache if requried (determined by age)
9999
Duration age = Duration.between(lastUpdateTs, Instant.now());
100100
if (age.compareTo(MAX_RESULT_AGE) >= 0) {

worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/SchematicsBackend.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
package com.sk89q.worldedit.util.schematic.backends;
2121

22-
import com.sk89q.worldedit.util.schematic.Schematic;
22+
import com.sk89q.worldedit.util.schematic.SchematicPath;
2323

2424
import java.util.List;
2525

@@ -43,7 +43,7 @@ public interface SchematicsBackend {
4343
*
4444
* @return List of known schematics.
4545
*/
46-
List<Schematic> getList();
46+
List<SchematicPath> getList();
4747

4848
/**
4949
* Tells the backend that there are changes it should take into account.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* WorldEdit, a Minecraft world manipulation toolkit
3+
* Copyright (C) sk89q <http://www.sk89q.com>
4+
* Copyright (C) WorldEdit team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
package com.sk89q.worldedit.util.schematic;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.nio.file.Path;
25+
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
/**
32+
* Tests {@link SchematicPath}.
33+
*
34+
* @see <a href="https://github.com/EngineHub/WorldEdit/pull/2212#discussion_r1016580671">
35+
* Trusting me4502 is good, controlling is better! :)
36+
* </a>
37+
*/
38+
public class SchematicPathTest {
39+
40+
@Test
41+
public void testHashAndEquality() {
42+
Path p0 = Path.of("/tmp/testpath0");
43+
Path p1 = Path.of("/tmp/testpath1");
44+
Path p0equiv = Path.of("/tmp/testpath0");
45+
46+
SchematicPath s0 = new SchematicPath(p0);
47+
SchematicPath s1 = new SchematicPath(p1);
48+
SchematicPath s0equiv = new SchematicPath(p0equiv);
49+
50+
assertEquals(s0.hashCode(), s0equiv.hashCode());
51+
assertNotEquals(s0.hashCode(), s1.hashCode());
52+
53+
assertTrue(s0.equals(s0equiv));
54+
assertFalse(s0.equals(s1));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)