Skip to content

Commit

Permalink
Merge pull request #380 from gdgib/G2-1649-FileSystemAPI
Browse files Browse the repository at this point in the history
G2-1649 Support flexible arity for parent & child paths
  • Loading branch information
gdgib authored Sep 26, 2024
2 parents 3017e0d + d65f676 commit 68a70fb
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.g2forge.alexandria.path.file.system.IFileSystem;

public interface IFile<T> {
public IFileSystem<T> getDirectorySystem();
public IFile<T> get(T filename);

public IFile<T> getParent();
public IFileSystem<T> getFileSystem();

public IFile<T> get(T filename);
public IFile<T> getParent();

public boolean isDirectory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,8 +26,8 @@ public IFile<String> get(String filename) {
}

@Override
public IFileSystem<String> getDirectorySystem() {
return OSFileSystem.getDirectorySystem();
public IFileSystem<String> getFileSystem() {
return OSFileSystem.getFileSystem();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends IFileSystem<T> {
public T getSelf();

@Override
public default IPath<T> normalize(IPath<T> path) {
if (path.isEmpty()) return path;

final List<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.g2forge.alexandria.path.path.IPath;
import com.g2forge.alexandria.path.path.Path;

public interface IStandardFileSystem extends IFileSystem<String> {
public interface IStandardFileSystem extends ISelfFileSystem<String>, ITreeFileSystem<String> {
public default String getParent() {
return "..";
}
Expand All @@ -15,11 +15,6 @@ public default String getSelf() {
return ".";
}

@Override
public default boolean isRootEscape(IPath<String> path) {
return normalize(path).startsWith(new Path<>(getParent()));
}

@Override
public default IPath<String> normalize(IPath<String> path) {
if (path.isEmpty()) return path;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T> extends IFileSystem<T> {
public T getParent();

@Override
public default boolean isRootEscape(IPath<T> path) {
return normalize(path).startsWith(new Path<>(getParent()));
}

@Override
public default IPath<T> normalize(IPath<T> path) {
if (path.isEmpty()) return path;

final List<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 68a70fb

Please sign in to comment.