From d65f676b937b0c51dcb331360079ce7403d9040a Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Wed, 25 Sep 2024 19:32:46 -0700 Subject: [PATCH] G2-1649 Support flexible arity for parent & child paths --- .../g2forge/alexandria/path/file/IFile.java | 6 ++-- .../alexandria/path/file/LocalFile.java | 6 ++-- .../path/file/system/ISelfFileSystem.java | 23 ++++++++++++++ .../path/file/system/IStandardFileSystem.java | 7 +---- .../path/file/system/ITreeFileSystem.java | 31 +++++++++++++++++++ .../path/file/system/OSFileSystem.java | 2 +- 6 files changed, 62 insertions(+), 13 deletions(-) create mode 100644 ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ISelfFileSystem.java create mode 100644 ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ITreeFileSystem.java diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/IFile.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/IFile.java index efe93d0d..bc38b387 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/file/IFile.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/IFile.java @@ -3,11 +3,11 @@ import com.g2forge.alexandria.path.file.system.IFileSystem; public interface IFile { - public IFileSystem getDirectorySystem(); + public IFile get(T filename); - public IFile getParent(); + public IFileSystem getFileSystem(); - public IFile get(T filename); + public IFile getParent(); public boolean isDirectory(); } diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/LocalFile.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/LocalFile.java index 609ebe24..aaac9171 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/file/LocalFile.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/LocalFile.java @@ -3,8 +3,8 @@ import java.nio.file.Files; import java.nio.file.Path; -import com.g2forge.alexandria.path.file.system.OSFileSystem; import com.g2forge.alexandria.path.file.system.IFileSystem; +import com.g2forge.alexandria.path.file.system.OSFileSystem; import lombok.Builder; import lombok.Data; @@ -26,8 +26,8 @@ public IFile get(String filename) { } @Override - public IFileSystem getDirectorySystem() { - return OSFileSystem.getDirectorySystem(); + public IFileSystem getFileSystem() { + return OSFileSystem.getFileSystem(); } @Override diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ISelfFileSystem.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ISelfFileSystem.java new file mode 100644 index 00000000..d2dc2679 --- /dev/null +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ISelfFileSystem.java @@ -0,0 +1,23 @@ +package com.g2forge.alexandria.path.file.system; + +import java.util.ArrayList; +import java.util.List; + +import com.g2forge.alexandria.path.path.IPath; +import com.g2forge.alexandria.path.path.Path; + +public interface ISelfFileSystem extends IFileSystem { + public T getSelf(); + + @Override + public default IPath normalize(IPath path) { + if (path.isEmpty()) return path; + + final List retVal = new ArrayList<>(path.getComponents().toCollection()); + for (int i = 0; i < retVal.size(); i++) { + final T current = retVal.get(i); + if (getSelf().equals(current)) retVal.remove(i--); + } + return new Path<>(retVal); + } +} diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/IStandardFileSystem.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/IStandardFileSystem.java index 7fae0783..ab0c3dc7 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/IStandardFileSystem.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/IStandardFileSystem.java @@ -6,7 +6,7 @@ import com.g2forge.alexandria.path.path.IPath; import com.g2forge.alexandria.path.path.Path; -public interface IStandardFileSystem extends IFileSystem { +public interface IStandardFileSystem extends ISelfFileSystem, ITreeFileSystem { public default String getParent() { return ".."; } @@ -15,11 +15,6 @@ public default String getSelf() { return "."; } - @Override - public default boolean isRootEscape(IPath path) { - return normalize(path).startsWith(new Path<>(getParent())); - } - @Override public default IPath normalize(IPath path) { if (path.isEmpty()) return path; diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ITreeFileSystem.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ITreeFileSystem.java new file mode 100644 index 00000000..449328e2 --- /dev/null +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/ITreeFileSystem.java @@ -0,0 +1,31 @@ +package com.g2forge.alexandria.path.file.system; + +import java.util.ArrayList; +import java.util.List; + +import com.g2forge.alexandria.path.path.IPath; +import com.g2forge.alexandria.path.path.Path; + +public interface ITreeFileSystem extends IFileSystem { + public T getParent(); + + @Override + public default boolean isRootEscape(IPath path) { + return normalize(path).startsWith(new Path<>(getParent())); + } + + @Override + public default IPath normalize(IPath path) { + if (path.isEmpty()) return path; + + final List retVal = new ArrayList<>(path.getComponents().toCollection()); + for (int i = 0; i < retVal.size(); i++) { + final T current = retVal.get(i); + if (getParent().equals(current) && (i > 0) && !getParent().equals(retVal.get(i - 1))) { + retVal.remove(i--); + retVal.remove(i--); + } + } + return new Path<>(retVal); + } +} diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/OSFileSystem.java b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/OSFileSystem.java index 6dab60a9..45770ca0 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/OSFileSystem.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/file/system/OSFileSystem.java @@ -8,7 +8,7 @@ public enum OSFileSystem implements IStandardFileSystem { Microsoft, POSIX; - public static OSFileSystem getDirectorySystem() { + public static OSFileSystem getFileSystem() { final PlatformCategory category = HPlatform.getPlatform().getCategory(); switch (category) { case Microsoft: