From 658b398134ecb744920d059bc5317e3087fc740d Mon Sep 17 00:00:00 2001 From: Greg Gibeling Date: Wed, 25 Sep 2024 18:15:46 -0700 Subject: [PATCH] G2-1648 Ensure Path return types can be fully overridden by subclasses --- .../g2forge/alexandria/path/path/Path.java | 14 ++++++++--- .../path/path/filename/Filename.java | 23 +++++++++++-------- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/path/Path.java b/ax-path/src/main/java/com/g2forge/alexandria/path/path/Path.java index aaf61402..0b8b0016 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/path/Path.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/path/Path.java @@ -35,17 +35,25 @@ public Path(T... components) { this(components.length < 1 ? EmptyCollection.create() : new CollectionCollection<>(components)); } + protected IPath cast(IPath path) { + return path; + } + + protected IPath create(Collection components) { + return new Path<>(components); + } + @Override public IPath getParent() { if (isEmpty()) throw new IllegalOperationException(); final List 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 resolve(IPath 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())); } } diff --git a/ax-path/src/main/java/com/g2forge/alexandria/path/path/filename/Filename.java b/ax-path/src/main/java/com/g2forge/alexandria/path/path/filename/Filename.java index d1d522a7..71b97293 100644 --- a/ax-path/src/main/java/com/g2forge/alexandria/path/path/filename/Filename.java +++ b/ax-path/src/main/java/com/g2forge/alexandria/path/path/filename/Filename.java @@ -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; @@ -46,6 +45,17 @@ public Filename(String... components) { super(components); } + @Override + protected Filename cast(IPath path) { + if (path instanceof Filename) return (Filename) path; + return create(path.getComponents().toCollection()); + } + + @Override + protected Filename create(Collection components) { + return new Filename(components); + } + /** * Get the last extension on the filename. * @@ -68,13 +78,6 @@ public String getName() { return getFirst(); } - @Override - public Filename getParent() { - if (isEmpty()) throw new IllegalOperationException(); - final List list = HCollection.asList(getComponents().toCollection()); - return new Filename(list.subList(0, list.size() - 1)); - } - /** * Get the name of the file without the last extension. * @@ -83,7 +86,7 @@ public Filename getParent() { */ public Filename getPrefix() { final List 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)); } /** @@ -95,7 +98,7 @@ public Filename getPrefix() { public Filename getSuffix() { if (size() <= 1) return null; final List list = HCollection.asList(components.toCollection()); - return new Filename(list.subList(1, list.size())); + return create(list.subList(1, list.size())); } @Override