Skip to content

Commit b5800eb

Browse files
committed
Add NIO ClipboardFormat methods, and deprecate File ones
1 parent 32229bd commit b5800eb

File tree

5 files changed

+50
-8
lines changed

5 files changed

+50
-8
lines changed

verification/src/changes/accepted-core-public-api-changes.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,14 @@
6565
"FIELD_REMOVED_IN_SUPERCLASS"
6666
]
6767
}
68-
]
68+
],
69+
"Acceptable addition of default method in interface": [
70+
{
71+
"type": "com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat",
72+
"member": "Method com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat.isFormat(java.nio.file.Path)",
73+
"changes": [
74+
"METHOD_NEW_DEFAULT"
75+
]
76+
}
77+
]
6978
}

worldedit-cli/src/main/java/com/sk89q/worldedit/cli/CLIWorldEdit.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public static void main(String[] args) {
322322
if (file.getName().endsWith("level.dat")) {
323323
throw new IllegalArgumentException("level.dat file support is unfinished.");
324324
} else {
325-
ClipboardFormat format = ClipboardFormats.findByFile(file);
325+
ClipboardFormat format = ClipboardFormats.findByPath(file.toPath());
326326
if (format != null) {
327327
int dataVersion;
328328
if (format != BuiltInClipboardFormat.MCEDIT_SCHEMATIC) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void load(Actor actor, LocalSession session,
127127
return;
128128
}
129129

130-
ClipboardFormat inferredFormat = ClipboardFormats.findByFile(f);
130+
ClipboardFormat inferredFormat = ClipboardFormats.findByPath(f.toPath());
131131
if (inferredFormat != null) {
132132
format = inferredFormat;
133133
}

worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormat.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.InputStream;
2525
import java.io.OutputStream;
2626
import java.nio.file.Files;
27+
import java.nio.file.Path;
2728
import java.util.Set;
2829

2930
/**
@@ -68,9 +69,21 @@ public interface ClipboardFormat {
6869
*
6970
* @param file the file
7071
* @return true if the given file is of this format
72+
* @deprecated Use {@link #isFormat(Path)} instead
7173
*/
74+
@Deprecated
7275
default boolean isFormat(File file) {
73-
try (InputStream stream = Files.newInputStream(file.toPath())) {
76+
return isFormat(file.toPath());
77+
}
78+
79+
/**
80+
* Return whether the given path is of this format.
81+
*
82+
* @param path the path
83+
* @return true if the given path is of this format
84+
*/
85+
default boolean isFormat(Path path) {
86+
try (InputStream stream = Files.newInputStream(path)) {
7487
return isFormat(stream);
7588
} catch (IOException e) {
7689
return false;

worldedit-core/src/main/java/com/sk89q/worldedit/extent/clipboard/io/ClipboardFormats.java

+24-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.File;
2828
import java.io.IOException;
2929
import java.io.InputStream;
30+
import java.nio.file.Path;
3031
import java.util.ArrayList;
3132
import java.util.Collection;
3233
import java.util.Collections;
@@ -72,8 +73,7 @@ public static void registerClipboardFormat(ClipboardFormat format) {
7273
/**
7374
* Find the clipboard format named by the given alias.
7475
*
75-
* @param alias
76-
* the alias
76+
* @param alias the alias
7777
* @return the format, otherwise null if none is matched
7878
*/
7979
@Nullable
@@ -85,10 +85,11 @@ public static ClipboardFormat findByAlias(String alias) {
8585
/**
8686
* Detect the format of a given file.
8787
*
88-
* @param file
89-
* the file
88+
* @param file the file
9089
* @return the format, otherwise null if one cannot be detected
90+
* @deprecated Use {@link #findByPath(Path)} instead.
9191
*/
92+
@Deprecated
9293
@Nullable
9394
public static ClipboardFormat findByFile(File file) {
9495
checkNotNull(file);
@@ -102,6 +103,25 @@ public static ClipboardFormat findByFile(File file) {
102103
return null;
103104
}
104105

106+
/**
107+
* Detect the format of a given path.
108+
*
109+
* @param path the path
110+
* @return the format, otherwise null if one cannot be detected
111+
*/
112+
@Nullable
113+
public static ClipboardFormat findByPath(Path path) {
114+
checkNotNull(path);
115+
116+
for (ClipboardFormat format : registeredFormats) {
117+
if (format.isFormat(path)) {
118+
return format;
119+
}
120+
}
121+
122+
return null;
123+
}
124+
105125
/**
106126
* Detect the format of a given input stream.
107127
*

0 commit comments

Comments
 (0)