From 40c045e556711b885a2c067c58fd5c4a2315af73 Mon Sep 17 00:00:00 2001 From: Markus Ebner Date: Tue, 8 Nov 2022 23:24:43 +0100 Subject: [PATCH] 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 --- .../worldedit/command/SchematicCommands.java | 27 ++++----- .../command/argument/SchematicConverter.java | 13 ++--- .../{Schematic.java => SchematicPath.java} | 14 +---- .../util/schematic/SchematicsManager.java | 2 +- .../backends/DummySchematicsBackend.java | 5 +- .../FileWatcherSchematicsBackend.java | 12 ++-- .../backends/PollingSchematicsBackend.java | 12 ++-- .../schematic/backends/SchematicsBackend.java | 4 +- .../util/schematic/SchematicPathTest.java | 57 +++++++++++++++++++ 9 files changed, 95 insertions(+), 51 deletions(-) rename worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/{Schematic.java => SchematicPath.java} (81%) create mode 100644 worldedit-core/src/test/java/com/sk89q/worldedit/util/schematic/SchematicPathTest.java diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java index 566bee2bcf..de0c64d67b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/SchematicCommands.java @@ -55,7 +55,7 @@ import com.sk89q.worldedit.util.io.Closer; import com.sk89q.worldedit.util.io.file.FilenameException; import com.sk89q.worldedit.util.io.file.MorePaths; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; import com.sk89q.worldedit.util.schematic.SchematicsManager; import org.apache.logging.log4j.Logger; import org.enginehub.piston.annotation.Command; @@ -73,10 +73,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; -import java.nio.file.DirectoryStream; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.concurrent.Callable; @@ -111,13 +108,13 @@ public SchematicCommands(WorldEdit worldEdit) { @CommandPermissions({"worldedit.clipboard.load", "worldedit.schematic.load"}) public void load(Actor actor, LocalSession session, @Arg(desc = "File name.") - Schematic schematic, + SchematicPath schematic, @Arg(desc = "Format name.", def = "sponge") ClipboardFormat format) throws FilenameException { LocalConfiguration config = worldEdit.getConfiguration(); // Schematic.path is relative, so treat it as filename - String filename = schematic.getPath().toString(); + String filename = schematic.path().toString(); File schematicsRoot = worldEdit.getSchematicsManager().getRoot().toFile(); File f = worldEdit.getSafeOpenFile(actor, schematicsRoot, filename, BuiltInClipboardFormat.SPONGE_V3_SCHEMATIC.getPrimaryFileExtension(), @@ -251,12 +248,12 @@ public void share(Actor actor, LocalSession session, @CommandPermissions("worldedit.schematic.delete") public void delete(Actor actor, @Arg(desc = "File name.") - Schematic schematic) throws WorldEditException { + SchematicPath schematic) throws WorldEditException { LocalConfiguration config = worldEdit.getConfiguration(); File dir = worldEdit.getWorkingDirectoryPath(config.saveDir).toFile(); // Schematic.path is relative, so treat it as filename - String filename = schematic.getPath().toString(); + String filename = schematic.path().toString(); File f = worldEdit.getSafeOpenFile(actor, dir, filename, "schematic", ClipboardFormats.getFileExtensionArray()); @@ -335,7 +332,7 @@ public void list(Actor actor, final String pageCommand = actor.isPlayer() ? "//schem list -p %page%" + flag : null; - Comparator schematicComparator = (s0, s1) -> pathComparator.compare(s0.getPath(), s1.getPath()); + Comparator schematicComparator = (s0, s1) -> pathComparator.compare(s0.path(), s1.path()); WorldEditAsyncCommandBuilder.createAndSendMessage(actor, new SchematicListTask(schematicComparator, page, pageCommand), @@ -445,11 +442,11 @@ public Consumer call() throws Exception { } private static class SchematicListTask implements Callable { - private final Comparator pathComparator; + private final Comparator pathComparator; private final int page; private final String pageCommand; - SchematicListTask(Comparator pathComparator, int page, String pageCommand) { + SchematicListTask(Comparator pathComparator, int page, String pageCommand) { this.pathComparator = pathComparator; this.page = page; this.pageCommand = pageCommand; @@ -458,7 +455,7 @@ private static class SchematicListTask implements Callable { @Override public Component call() throws Exception { SchematicsManager schematicsManager = WorldEdit.getInstance().getSchematicsManager(); - List fileList = schematicsManager.getList(); + List fileList = schematicsManager.getList(); if (fileList.isEmpty()) { return ErrorFormat.wrap("No schematics found."); @@ -473,9 +470,9 @@ public Component call() throws Exception { private static class SchematicPaginationBox extends PaginationBox { private final Path rootDir; - private final List files; + private final List files; - SchematicPaginationBox(Path rootDir, List files, String pageCommand) { + SchematicPaginationBox(Path rootDir, List files, String pageCommand) { super("Available schematics", pageCommand); this.rootDir = rootDir; this.files = files; @@ -484,7 +481,7 @@ private static class SchematicPaginationBox extends PaginationBox { @Override public Component getComponent(int number) { checkArgument(number < files.size() && number >= 0); - Path file = files.get(number).getPath(); + Path file = files.get(number).path(); String format = ClipboardFormats.getFileExtensionMap() .get(MoreFiles.getFileExtension(file)) diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java index 9a8d65df55..65203e80c0 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/command/argument/SchematicConverter.java @@ -20,11 +20,10 @@ package com.sk89q.worldedit.command.argument; import com.sk89q.worldedit.WorldEdit; -import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.util.formatting.text.Component; import com.sk89q.worldedit.util.formatting.text.TextComponent; import com.sk89q.worldedit.util.io.file.FilenameException; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; import com.sk89q.worldedit.util.schematic.SchematicsManager; import org.enginehub.piston.CommandManager; import org.enginehub.piston.converter.ArgumentConverter; @@ -40,10 +39,10 @@ import static org.enginehub.piston.converter.SuggestionHelper.limitByPrefix; -public class SchematicConverter implements ArgumentConverter { +public class SchematicConverter implements ArgumentConverter { public static void register(WorldEdit worldEdit, CommandManager commandManager) { - commandManager.registerConverter(Key.of(Schematic.class), new SchematicConverter(worldEdit)); + commandManager.registerConverter(Key.of(SchematicPath.class), new SchematicConverter(worldEdit)); } private final WorldEdit worldEdit; @@ -65,11 +64,11 @@ public List getSuggestions(String input, InjectedValueAccess context) { Path schematicsRootPath = schematicsManager.getRoot(); return limitByPrefix(schematicsManager.getList().stream() - .map(s -> schematicsRootPath.relativize(s.getPath()).toString()), input); + .map(s -> schematicsRootPath.relativize(s.path()).toString()), input); } @Override - public ConversionResult convert(String s, InjectedValueAccess injectedValueAccess) { + public ConversionResult convert(String s, InjectedValueAccess injectedValueAccess) { Path schematicsRoot = worldEdit.getSchematicsManager().getRoot(); // resolve as subpath of schematicsRoot Path schematicPath = schematicsRoot.resolve(s).toAbsolutePath(); @@ -81,7 +80,7 @@ public ConversionResult convert(String s, InjectedValueAccess injecte if (Files.exists(schematicPath)) { // continue as relative path to schematicsRoot schematicPath = schematicsRoot.relativize(schematicPath); - return SuccessfulConversion.fromSingle(new Schematic(schematicPath)); + return SuccessfulConversion.fromSingle(new SchematicPath(schematicPath)); } else { return FailedConversion.from(new FilenameException(s)); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/Schematic.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicPath.java similarity index 81% rename from worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/Schematic.java rename to worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicPath.java index 69317c2eb8..c8c3288d43 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/Schematic.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicPath.java @@ -24,15 +24,7 @@ /** * Record representing one Schematic file. */ -public record Schematic(Path path) { - - /** - * Get this Schematic's path. - * @return This Schematic's path. - */ - public Path getPath() { - return path; - } +public record SchematicPath(Path path) { @Override public String toString() { @@ -46,8 +38,8 @@ public int hashCode() { @Override public boolean equals(Object obj) { - Schematic other = (Schematic) obj; + SchematicPath other = (SchematicPath) obj; if (other == null) { return false; } - return path.equals(other.getPath()); + return path.equals(other.path()); } } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicsManager.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicsManager.java index c1adc4cd8e..6a7db8498e 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicsManager.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/SchematicsManager.java @@ -102,7 +102,7 @@ public Path getRoot() { * Get a list of all known schematics. * @return List of all known schematics. */ - public List getList() { + public List getList() { return backend.getList(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/DummySchematicsBackend.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/DummySchematicsBackend.java index 7594b1026c..17cf24e61a 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/DummySchematicsBackend.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/DummySchematicsBackend.java @@ -19,9 +19,8 @@ package com.sk89q.worldedit.util.schematic.backends; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; -import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -39,7 +38,7 @@ public void uninit() { } @Override - public List getList() { + public List getList() { return new ArrayList<>(); } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/FileWatcherSchematicsBackend.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/FileWatcherSchematicsBackend.java index 038edb09e0..ba9a294452 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/FileWatcherSchematicsBackend.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/FileWatcherSchematicsBackend.java @@ -21,7 +21,7 @@ import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.util.io.file.RecursiveDirectoryWatcher; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; import org.apache.logging.log4j.Logger; import java.io.IOException; @@ -41,7 +41,7 @@ public class FileWatcherSchematicsBackend implements SchematicsBackend { private static final Logger LOGGER = LogManagerCompat.getLogger(); private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); - private final Set schematics = new HashSet<>(); + private final Set schematics = new HashSet<>(); private final RecursiveDirectoryWatcher directoryWatcher; private FileWatcherSchematicsBackend(RecursiveDirectoryWatcher directoryWatcher) { @@ -64,9 +64,9 @@ public void init() { directoryWatcher.start(event -> { lock.writeLock().lock(); if (event instanceof RecursiveDirectoryWatcher.FileCreatedEvent) { - schematics.add(new Schematic(event.getPath())); + schematics.add(new SchematicPath(event.getPath())); } else if (event instanceof RecursiveDirectoryWatcher.FileDeletedEvent) { - schematics.remove(new Schematic(event.getPath())); + schematics.remove(new SchematicPath(event.getPath())); } lock.writeLock().unlock(); if (event instanceof RecursiveDirectoryWatcher.FileCreatedEvent) { @@ -84,9 +84,9 @@ public void uninit() { } @Override - public List getList() { + public List getList() { lock.readLock().lock(); - List result = new ArrayList<>(schematics); + List result = new ArrayList<>(schematics); lock.readLock().unlock(); return result; } diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/PollingSchematicsBackend.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/PollingSchematicsBackend.java index e47863c433..10f5ebc307 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/PollingSchematicsBackend.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/PollingSchematicsBackend.java @@ -20,7 +20,7 @@ package com.sk89q.worldedit.util.schematic.backends; import com.sk89q.worldedit.internal.util.LogManagerCompat; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; import org.apache.logging.log4j.Logger; import java.io.IOException; @@ -46,7 +46,7 @@ public class PollingSchematicsBackend implements SchematicsBackend { private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); private final Path schematicsDir; private Instant lastUpdateTs = Instant.EPOCH; - private List schematics = new ArrayList<>(); + private List schematics = new ArrayList<>(); private PollingSchematicsBackend(Path schematicsDir) { this.schematicsDir = schematicsDir; @@ -61,14 +61,14 @@ public static PollingSchematicsBackend create(Path schematicsFolder) { return new PollingSchematicsBackend(schematicsFolder); } - private List scanFolder(Path root) { - List pathList = new ArrayList<>(); + private List scanFolder(Path root) { + List pathList = new ArrayList<>(); try (DirectoryStream stream = Files.newDirectoryStream(root)) { for (Path path : stream) { if (Files.isDirectory(path)) { pathList.addAll(scanFolder(path)); } else { - pathList.add(new Schematic(path)); + pathList.add(new SchematicPath(path)); } } } catch (IOException e) { @@ -94,7 +94,7 @@ public void uninit() { } @Override - public synchronized List getList() { + public synchronized List getList() { // udpate internal cache if requried (determined by age) Duration age = Duration.between(lastUpdateTs, Instant.now()); if (age.compareTo(MAX_RESULT_AGE) >= 0) { diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/SchematicsBackend.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/SchematicsBackend.java index 46c1f111b2..4c7a35b803 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/SchematicsBackend.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/schematic/backends/SchematicsBackend.java @@ -19,7 +19,7 @@ package com.sk89q.worldedit.util.schematic.backends; -import com.sk89q.worldedit.util.schematic.Schematic; +import com.sk89q.worldedit.util.schematic.SchematicPath; import java.util.List; @@ -43,7 +43,7 @@ public interface SchematicsBackend { * * @return List of known schematics. */ - List getList(); + List getList(); /** * Tells the backend that there are changes it should take into account. diff --git a/worldedit-core/src/test/java/com/sk89q/worldedit/util/schematic/SchematicPathTest.java b/worldedit-core/src/test/java/com/sk89q/worldedit/util/schematic/SchematicPathTest.java new file mode 100644 index 0000000000..a992754724 --- /dev/null +++ b/worldedit-core/src/test/java/com/sk89q/worldedit/util/schematic/SchematicPathTest.java @@ -0,0 +1,57 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.schematic; + +import org.junit.jupiter.api.Test; + +import java.nio.file.Path; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests {@link SchematicPath}. + * + * @see + * Trusting me4502 is good, controlling is better! :) + * + */ +public class SchematicPathTest { + + @Test + public void testHashAndEquality() { + Path p0 = Path.of("/tmp/testpath0"); + Path p1 = Path.of("/tmp/testpath1"); + Path p0equiv = Path.of("/tmp/testpath0"); + + SchematicPath s0 = new SchematicPath(p0); + SchematicPath s1 = new SchematicPath(p1); + SchematicPath s0equiv = new SchematicPath(p0equiv); + + assertEquals(s0.hashCode(), s0equiv.hashCode()); + assertNotEquals(s0.hashCode(), s1.hashCode()); + + assertTrue(s0.equals(s0equiv)); + assertFalse(s0.equals(s1)); + } + +}