Skip to content

Commit

Permalink
Merge pull request #379 from gdgib/G2-1648-Filename
Browse files Browse the repository at this point in the history
G2-1648 Ensure Path return types can be fully overridden by subclasses
  • Loading branch information
gdgib authored Sep 26, 2024
2 parents e90dffc + 658b398 commit 3017e0d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
14 changes: 11 additions & 3 deletions ax-path/src/main/java/com/g2forge/alexandria/path/path/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,25 @@ public Path(T... components) {
this(components.length < 1 ? EmptyCollection.create() : new CollectionCollection<>(components));
}

protected IPath<T> cast(IPath<T> path) {
return path;
}

protected IPath<T> create(Collection<T> components) {
return new Path<>(components);
}

@Override
public IPath<T> getParent() {
if (isEmpty()) throw new IllegalOperationException();
final List<T> list = HCollection.asList(getComponents().toCollection());
return new Path<>(list.subList(0, list.size() - 1));
return create(list.subList(0, list.size() - 1));
}

@Override
public IPath<T> resolve(IPath<T> subpath) {
if (isEmpty()) return subpath;
if (isEmpty()) return cast(subpath);
if (subpath.isEmpty()) return this;
return new Path<>(HCollection.concatenate(getComponents().toCollection(), subpath.getComponents().toCollection()));
return create(HCollection.concatenate(getComponents().toCollection(), subpath.getComponents().toCollection()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.List;

import com.g2forge.alexandria.collection.ICollection;
import com.g2forge.alexandria.java.core.error.IllegalOperationException;
import com.g2forge.alexandria.java.core.helpers.HCollection;
import com.g2forge.alexandria.java.io.HPath;
import com.g2forge.alexandria.path.path.IPath;
Expand Down Expand Up @@ -46,6 +45,17 @@ public Filename(String... components) {
super(components);
}

@Override
protected Filename cast(IPath<String> path) {
if (path instanceof Filename) return (Filename) path;
return create(path.getComponents().toCollection());
}

@Override
protected Filename create(Collection<String> components) {
return new Filename(components);
}

/**
* Get the last extension on the filename.
*
Expand All @@ -68,13 +78,6 @@ public String getName() {
return getFirst();
}

@Override
public Filename getParent() {
if (isEmpty()) throw new IllegalOperationException();
final List<String> list = HCollection.asList(getComponents().toCollection());
return new Filename(list.subList(0, list.size() - 1));
}

/**
* Get the name of the file without the last extension.
*
Expand All @@ -83,7 +86,7 @@ public Filename getParent() {
*/
public Filename getPrefix() {
final List<String> list = HCollection.asList(components.toCollection());
return new Filename(list.subList(0, list.size() == 1 ? 1 : list.size() - 1));
return create(list.subList(0, list.size() == 1 ? 1 : list.size() - 1));
}

/**
Expand All @@ -95,7 +98,7 @@ public Filename getPrefix() {
public Filename getSuffix() {
if (size() <= 1) return null;
final List<String> list = HCollection.asList(components.toCollection());
return new Filename(list.subList(1, list.size()));
return create(list.subList(1, list.size()));
}

@Override
Expand Down

0 comments on commit 3017e0d

Please sign in to comment.