-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
153 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
planetiler-core/src/main/java/com/onthegomap/planetiler/util/Glob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.onthegomap.planetiler.util; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
/** | ||
* Utility for constructing base+glob paths for matching many files | ||
*/ | ||
public record Glob(Path base, String pattern) { | ||
|
||
private static final Pattern GLOB_PATTERN = Pattern.compile("[?*{\\[].*$"); | ||
|
||
/** Wrap a base path with no globs in it yet. */ | ||
public static Glob of(Path path) { | ||
return new Glob(path, null); | ||
} | ||
|
||
/** Resolves a subdirectory using parts separated by the platform file separator. */ | ||
public Glob resolve(String... subPath) { | ||
String separator = base.getFileSystem().getSeparator(); | ||
if (pattern != null) { | ||
return new Glob(base, pattern + separator + String.join(separator, subPath)); | ||
} else if (subPath == null || subPath.length == 0) { | ||
return this; | ||
} else if (GLOB_PATTERN.matcher(subPath[0]).find()) { | ||
return new Glob(base, String.join(separator, subPath)); | ||
} else { | ||
return of(base.resolve(subPath[0])).resolve(Arrays.copyOfRange(subPath, 1, subPath.length)); | ||
} | ||
} | ||
|
||
/** Parse a string containing platform-specific file separators into a base+glob pattern. */ | ||
public static Glob parse(String path) { | ||
var matcher = GLOB_PATTERN.matcher(path); | ||
if (!matcher.find()) { | ||
return of(Path.of(path)); | ||
} | ||
matcher.reset(); | ||
String base = matcher.replaceAll(""); | ||
String separator = Path.of(base).getFileSystem().getSeparator(); | ||
int idx = base.lastIndexOf(separator); | ||
if (idx > 0) { | ||
base = base.substring(0, idx); | ||
} | ||
return of(Path.of(base)).resolve(path.substring(idx + 1).split(Pattern.quote(separator))); | ||
} | ||
|
||
/** Search the filesystem for all files beneath {@link #base()} matching {@link #pattern()}. */ | ||
public List<Path> find() { | ||
return pattern == null ? List.of(base) : FileUtils.walkPathWithPattern(base, pattern); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.