1111import java .util .stream .IntStream ;
1212
1313public class UnionPath implements Path {
14+ private static final Pattern SEPARATOR_BEGIN_END ;
15+ private static final Pattern SEPARATOR_DUPLICATES ;
16+
17+ static {
18+ var sep = "(?:" + Pattern .quote (UnionFileSystem .SEP_STRING ) + ")" ;
19+ SEPARATOR_BEGIN_END = Pattern .compile ("^" + sep + "*|" + sep + "*$" );
20+ SEPARATOR_DUPLICATES = Pattern .compile (sep + "(?=" + sep + ")" );
21+ }
1422 private final UnionFileSystem fileSystem ;
1523 private final boolean absolute ;
1624 private final String [] pathParts ;
@@ -24,8 +32,10 @@ public class UnionPath implements Path {
2432 this .absolute = false ;
2533 this .pathParts = new String [0 ];
2634 } else {
27- final var longstring = Arrays .stream (pathParts ).filter (part -> !part .isEmpty ()).collect (Collectors .joining (this .getFileSystem ().getSeparator ()));
28- this .absolute = longstring .startsWith (this .getFileSystem ().getSeparator ());
35+ final var longstring = Arrays .stream (pathParts )
36+ .filter (part -> !part .isEmpty ())
37+ .collect (Collectors .joining (UnionFileSystem .SEP_STRING ));
38+ this .absolute = longstring .startsWith (UnionFileSystem .SEP_STRING );
2939 this .pathParts = getPathParts (longstring );
3040 }
3141 this .normalized = null ;
@@ -47,17 +57,13 @@ private UnionPath(final UnionFileSystem fileSystem, boolean absolute, boolean is
4757 }
4858
4959 private String [] getPathParts (final String longstring ) {
50- var sep = "(?:" + Pattern .quote (this .getFileSystem ().getSeparator ()) + ")" ;
51- String pathname = longstring
52- .replace ("\\ " , this .getFileSystem ().getSeparator ())
53- // remove separators from start and end of longstring
54- .replaceAll ("^" + sep + "*|" + sep + "*$" , "" )
55- // Remove duplicate separators
56- .replaceAll (sep + "+(?=" + sep + ")" , "" );
57- if (pathname .isEmpty ())
60+ var clean = longstring .replace ("\\ " , UnionFileSystem .SEP_STRING );
61+ clean = SEPARATOR_BEGIN_END .matcher (clean ).replaceAll ("" );
62+ clean = SEPARATOR_DUPLICATES .matcher (clean ).replaceAll ("" );
63+ if (clean .isEmpty ())
5864 return new String [0 ];
5965 else
60- return pathname .split (this . getFileSystem (). getSeparator () );
66+ return clean .split (UnionFileSystem . SEP_STRING );
6167 }
6268
6369 @ Override
@@ -302,6 +308,6 @@ public int hashCode() {
302308
303309 @ Override
304310 public String toString () {
305- return (this .absolute ? fileSystem . getSeparator () : "" ) + String .join (fileSystem . getSeparator () , this .pathParts );
311+ return (this .absolute ? UnionFileSystem . SEP_STRING : "" ) + String .join (UnionFileSystem . SEP_STRING , this .pathParts );
306312 }
307313}
0 commit comments