1111import java .nio .file .attribute .*;
1212import java .nio .file .spi .FileSystemProvider ;
1313import java .util .*;
14- import java .util .function .BiPredicate ;
1514import java .util .stream .Stream ;
1615
1716public class UnionFileSystemProvider extends FileSystemProvider {
@@ -55,24 +54,7 @@ protected Path uriToPath(URI uri) {
5554 */
5655 @ Override
5756 public FileSystem newFileSystem (final URI uri , final Map <String , ?> env ) throws IOException {
58- @ SuppressWarnings ("unchecked" )
59- var additional = ((Map <String , List <Path >>)env ).getOrDefault ("additional" , List .<Path >of ());
60- @ SuppressWarnings ("unchecked" )
61- var filter = ((Map <String , BiPredicate <String , String >>)env ).getOrDefault ("filter" , null );
62-
63- if (filter == null && additional .isEmpty ())
64- throw new IllegalArgumentException ("Missing additional and/or filter" );
65-
66- if (filter == null )
67- filter = (p , b ) -> true ;
68-
69- var path = uriToPath (uri );
70- var key = makeKey (path );
71- try {
72- return newFileSystemInternal (key , filter , Stream .concat (Stream .of (path ), additional .stream ()).toArray (Path []::new ));
73- } catch (UncheckedIOException e ) {
74- throw e .getCause ();
75- }
57+ return newFileSystem (uriToPath (uri ), env );
7658 }
7759
7860 /**
@@ -87,8 +69,7 @@ public FileSystem newFileSystem(final URI uri, final Map<String, ?> env) throws
8769 public FileSystem newFileSystem (final Path path , final Map <String , ?> env ) throws IOException {
8870 @ SuppressWarnings ("unchecked" )
8971 var additional = ((Map <String , List <Path >>)env ).getOrDefault ("additional" , List .<Path >of ());
90- @ SuppressWarnings ("unchecked" )
91- var filter = ((Map <String , BiPredicate <String , String >>)env ).getOrDefault ("filter" , null );
72+ var filter = readFilterFromEnv (env );
9273
9374 if (filter == null && additional .isEmpty ())
9475 throw new UnsupportedOperationException ("Missing additional and/or filter" );
@@ -101,13 +82,26 @@ public FileSystem newFileSystem(final Path path, final Map<String, ?> env) throw
10182 }
10283 }
10384
104- public UnionFileSystem newFileSystem (@ Nullable BiPredicate <String , String > pathfilter , final Path ... paths ) {
85+ @ Nullable
86+ private static UnionPathFilter readFilterFromEnv (Map <String , ?> env ) {
87+ Object filter = env .get ("filter" );
88+
89+ if (filter == null ) {
90+ return null ;
91+ } else if (filter instanceof UnionPathFilter unionPathFilter ) {
92+ return unionPathFilter ;
93+ } else {
94+ throw new IllegalArgumentException ("Unknown type for \" filter\" UnionFileSystem env var: " + filter .getClass ().getName ());
95+ }
96+ }
97+
98+ public UnionFileSystem newFileSystem (@ Nullable UnionPathFilter pathfilter , final Path ... paths ) {
10599 if (paths .length == 0 ) throw new IllegalArgumentException ("Need at least one path" );
106100 var key = makeKey (paths [0 ]);
107101 return newFileSystemInternal (key , pathfilter , paths );
108102 }
109103
110- private UnionFileSystem newFileSystemInternal (final String key , @ Nullable BiPredicate < String , String > pathfilter , final Path ... paths ) {
104+ private UnionFileSystem newFileSystemInternal (final String key , @ Nullable UnionPathFilter pathfilter , final Path ... paths ) {
111105 var normpaths = Arrays .stream (paths )
112106 .map (Path ::toAbsolutePath )
113107 .map (Path ::normalize )
0 commit comments