Skip to content

Commit

Permalink
Refactor Resource Path Utils to be more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Feb 17, 2025
1 parent 73b010b commit 0a61b58
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,39 @@

public class ResourcePathUtils {
public static Optional<ResourcePath> stripPrefix(String prefix, ResourcePath path) {
return stripPrefix(prefix, path.path()).map(pathPart -> new ResourcePathImpl(path.namespace(), pathPart));
Optional<String> strippedPath = stripPrefix(prefix, path.path());

if (!strippedPath.isPresent()) {
return Optional.empty();
}

return Optional.of(new ResourcePathImpl(path.namespace(), strippedPath.get()));
}

public static Optional<ResourcePath> stripSuffix(String suffix, ResourcePath path) {
return stripSuffix(suffix, path.path()).map(pathPart -> new ResourcePathImpl(path.namespace(), pathPart));
Optional<String> strippedPath = stripSuffix(suffix, path.path());

if (!strippedPath.isPresent()) {
return Optional.empty();
}

return Optional.of(new ResourcePathImpl(path.namespace(), strippedPath.get()));
}

public static Optional<ResourcePath> stripSuffixAndPrefix(String prefix, String suffix, ResourcePath path) {
return stripPrefix(prefix, path.path()).flatMap(pathPart -> stripSuffix(suffix, pathPart))
.map(pathPart -> new ResourcePathImpl(path.namespace(), pathPart));
Optional<String> strippedPrefixPath = stripPrefix(prefix, path.path());

if (!strippedPrefixPath.isPresent()) {
return Optional.empty();
}

Optional<String> strippedPath = stripSuffix(prefix, strippedPrefixPath.get());

if (!strippedPath.isPresent()) {
return Optional.empty();
}

return Optional.of(new ResourcePathImpl(path.namespace(), strippedPath.get()));
}

public static Optional<ResourcePath> metadataPathToResource(Path metadataPath) {
Expand All @@ -41,5 +64,4 @@ private static Optional<String> stripSuffix(String suffix, String text) {
return Optional.empty();
}
}

}

0 comments on commit 0a61b58

Please sign in to comment.