|
|
@@ -10,8 +10,12 @@ package net.ranides.assira.io;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import net.ranides.assira.collection.list.JoinList;
|
|
|
+import net.ranides.assira.collection.list.ListAdapter;
|
|
|
import net.ranides.assira.collection.list.VirtualList;
|
|
|
import net.ranides.assira.text.StringUtils;
|
|
|
import net.ranides.assira.text.Strings;
|
|
|
@@ -21,7 +25,7 @@ import net.ranides.assira.text.Strings;
|
|
|
* @author ranides
|
|
|
*/
|
|
|
public final class PathHelper {
|
|
|
-
|
|
|
+
|
|
|
private PathHelper() { }
|
|
|
|
|
|
/**
|
|
|
@@ -39,9 +43,35 @@ public final class PathHelper {
|
|
|
public static final class FilePath extends VirtualList<String> {
|
|
|
private final String[] components;
|
|
|
|
|
|
+ FilePath(String[] components) {
|
|
|
+ this.components = components;
|
|
|
+ }
|
|
|
+
|
|
|
FilePath(List<String> components) {
|
|
|
this.components = components.toArray(new String[components.size()]);
|
|
|
}
|
|
|
+
|
|
|
+ FilePath(String path) {
|
|
|
+ String text = path.replace('/', File.separatorChar).replace('\\', File.separatorChar);
|
|
|
+ this.components = text.split(Pattern.quote(File.separator));
|
|
|
+ }
|
|
|
+
|
|
|
+ FilePath(File path) {
|
|
|
+ this.components = path.getPath().split(Pattern.quote(File.separator));
|
|
|
+ }
|
|
|
+
|
|
|
+ static FilePath valueOf(Object value) {
|
|
|
+ if(value instanceof FilePath) {
|
|
|
+ return (FilePath)value;
|
|
|
+ }
|
|
|
+ if(value instanceof String) {
|
|
|
+ return new FilePath((String)value);
|
|
|
+ }
|
|
|
+ if(value instanceof File) {
|
|
|
+ return new FilePath((File)value);
|
|
|
+ }
|
|
|
+ throw new IllegalArgumentException("Unsupported value: " + value);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public String get(int index) {
|
|
|
@@ -61,6 +91,11 @@ public final class PathHelper {
|
|
|
return new File(StringUtils.join(components, File.separator));
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return StringUtils.join(components, File.separator);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -327,4 +362,33 @@ public final class PathHelper {
|
|
|
public static int compare(String path1, String path2) {
|
|
|
return asFile(path1).compareTo(asFile(path2));
|
|
|
}
|
|
|
+
|
|
|
+ public static String join(FilePath... paths) {
|
|
|
+ return new FilePath(new JoinList<String>(paths)).toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String join(File... paths) throws PathConvertException {
|
|
|
+ FilePath[] out = new FilePath[paths.length];
|
|
|
+ for(int i=0; i<paths.length; i++) {
|
|
|
+ out[i] = new FilePath(paths[i]);
|
|
|
+ }
|
|
|
+ return join(out);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String join(String... paths) throws PathConvertException {
|
|
|
+ FilePath[] out = new FilePath[paths.length];
|
|
|
+ for(int i=0; i<paths.length; i++) {
|
|
|
+ out[i] = new FilePath(paths[i]);
|
|
|
+ }
|
|
|
+ return join(out);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String join(Object... paths) {
|
|
|
+ FilePath[] out = new FilePath[paths.length];
|
|
|
+ for(int i=0; i<paths.length; i++) {
|
|
|
+ out[i] = FilePath.valueOf(paths[i]);
|
|
|
+ }
|
|
|
+ return join(out);
|
|
|
+ }
|
|
|
+
|
|
|
}
|