@Test public void testAndFilterLogicallyAndsAllFiltersAndDoesNotAcceptIfOneOfTheFiltersIsFalse() throws IOException { Filter<Path> filter1 = context.mock(Filter.class, "filter1"); Filter<Path> filter2 = context.mock(Filter.class, "filter2"); Filter<Path> filter3 = context.mock(Filter.class, "filter3"); Path mockPath = context.mock(Path.class); context.checking(new Expectations() {{ atMost(1).of(filter1).accept(with(any(Path.class))); will(returnValue(true)); atMost(1).of(filter2).accept(with(any(Path.class))); will(returnValue(true)); atMost(1).of(filter3).accept(with(any(Path.class))); will(returnValue(false)); }}); AggregateAndFilter andFilter = new AggregateAndFilter(Sets.newHashSet(filter1, filter2, filter3)); Assert.assertFalse(andFilter.accept(mockPath)); }
@Test public void testAndFilterLogicallyAndsAllFiltersAndAcceptsIfAllOfTheFiltersAreTrue() throws IOException { Filter<Path> filter1 = context.mock(Filter.class, "filter1"); Filter<Path> filter2 = context.mock(Filter.class, "filter2"); Filter<Path> filter3 = context.mock(Filter.class, "filter3"); Path mockPath = context.mock(Path.class); context.checking(new Expectations() {{ atMost(1).of(filter1).accept(with(any(Path.class))); will(returnValue(true)); atMost(1).of(filter2).accept(with(any(Path.class))); will(returnValue(true)); atMost(1).of(filter3).accept(with(any(Path.class))); will(returnValue(true)); }}); AggregateAndFilter andFilter = new AggregateAndFilter(Sets.newHashSet(filter1, filter2, filter3)); Assert.assertTrue(andFilter.accept(mockPath)); }
@Test public void testOrFilterLogicallyOrsAllFiltersAndDoesNotAcceptIfAllOfTheFiltersAreFalse() throws IOException { Filter<Path> filter1 = context.mock(Filter.class, "filter1"); Filter<Path> filter2 = context.mock(Filter.class, "filter2"); Filter<Path> filter3 = context.mock(Filter.class, "filter3"); Path mockPath = context.mock(Path.class); context.checking(new Expectations() {{ atMost(1).of(filter1).accept(with(any(Path.class))); will(returnValue(false)); atMost(1).of(filter2).accept(with(any(Path.class))); will(returnValue(false)); atMost(1).of(filter3).accept(with(any(Path.class))); will(returnValue(false)); }}); AggregateOrFilter orFilter = new AggregateOrFilter(Sets.newHashSet(filter1, filter2, filter3)); Assert.assertFalse(orFilter.accept(mockPath)); }
@Test public void testOrFilterLogicallyOrsAllFiltersAndAcceptsIfAnyOfTheFiltersAreTrue() throws IOException { Filter<Path> filter1 = context.mock(Filter.class, "filter1"); Filter<Path> filter2 = context.mock(Filter.class, "filter2"); Filter<Path> filter3 = context.mock(Filter.class, "filter3"); Path mockPath = context.mock(Path.class); context.checking(new Expectations() {{ atMost(1).of(filter1).accept(with(any(Path.class))); will(returnValue(false)); atMost(1).of(filter2).accept(with(any(Path.class))); will(returnValue(false)); atMost(1).of(filter3).accept(with(any(Path.class))); will(returnValue(true)); }}); AggregateOrFilter orFilter = new AggregateOrFilter(Sets.newHashSet(filter1, filter2, filter3)); Assert.assertTrue(orFilter.accept(mockPath)); }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, final Filter<? super Path> filter) throws IOException { final BundleFileSystem fs = (BundleFileSystem) dir.getFileSystem(); final DirectoryStream<Path> stream = origProvider(dir) .newDirectoryStream(fs.unwrap(dir), new Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { return filter.accept(fs.wrap(entry)); } }); return new DirectoryStream<Path>() { @Override public void close() throws IOException { stream.close(); } @Override public Iterator<Path> iterator() { return fs.wrapIterator(stream.iterator()); } }; }
/** * Constructs a filter according to the {@link PathMatcher} rules implemented by * {@link DefaultPathMatcher}. * * @param pathFilterString * @return A filter for the pattern * @see FileSystem#getPathMatcher(String) * @throws IllegalArgumentException * If the syntax and pattern cannot be parsed */ public Filter<Path> parsePathFilter(String syntaxAndPattern, String fileSystemPathSeparator) throws IllegalArgumentException { final DefaultPathMatcher matcher = new DefaultPathMatcher(syntaxAndPattern, fileSystemPathSeparator); return new Filter<Path>() { @Override public boolean accept(Path path) throws IOException { return matcher.matches(path); } }; }
/** * Creates an {@link AggregateOrFilter} from all of the filters, so that if any of them match * then the path is accepted. Filters are created using {@link #parsePathFilter(String)}. * * @param commandOptions * @return null if there are no filters created or the {@link AggregateOrFilter} * * @see #parsePathFilter(String) */ public Filter<Path> createPathFilters(List<UserCommandOption> commandOptions, String fileSystemPathSeparator) { if (commandOptions == null || commandOptions.isEmpty()) { return null; } AggregateOrFilter orFilter = new AggregateOrFilter(); for (UserCommandOption opt : commandOptions) { Filter<Path> filter = parsePathFilter(opt.getValue(), fileSystemPathSeparator); orFilter.addAggregateFilter(filter); } return orFilter; }
protected int listDirectoryContents(FileSystemProvider provider, FileSystem fileSystem, Filter<Path> pathFilters, Path path, boolean recursive) { AtomicInteger pathsCounter = new AtomicInteger(0); FileSystemProviderHelper.iterateOverDirectoryContents(fileSystem, Optional.ofNullable(path), PathFilters.ACCEPT_ALL_FILTER, recursive, subPath -> { pathsCounter.addAndGet(printCloudPathAttributes(fileSystem, pathFilters, subPath.getResultPath())); return true; }); return pathsCounter.get(); }
/** * Directory access is checked using {@link #checkAccess(BlobStoreContext, CloudPath, Set)} * with {@link AclEntryPermission#LIST_DIRECTORY}. */ @Override public CloudDirectoryStream newDirectoryStream(BlobStoreContext context, CloudPath dir, Filter<CloudPath> filter, boolean isRecursive) throws IOException { checkAccess(context, dir, NEW_DIRECTORY_STREAM_PERMS); CloudPath dirPath = dir; boolean isContainer = dirPath.getRoot() == null; // Search in a container if root is null, otherwise check to find a directory if (!isContainer) { // Attempt to read attributes try { CloudBasicFileAttributes attributes = readAttributes(context, CloudBasicFileAttributes.class, dir); // Look at the parent if this is a file if (!attributes.isDirectory()) { dirPath = dir.getParent(); } } catch (FileNotFoundException e) { LOG.warn("Cloud file path '{}' does not exist, will assume that this is a directory", dir); } } return new CloudDirectoryStream(dirPath, isContainer, isRecursive, filter); }
boolean checkAccepts(Filter<Path> filter, Path entry) { try { return filter.accept(entry); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException { MCRPath mcrPath = MCRFileSystemUtils.checkPathAbsolute(dir); MCRFilesystemNode node = resolvePath(mcrPath); if (node instanceof MCRDirectory) { return new MCRDirectoryStream((MCRDirectory) node, mcrPath); } throw new NotDirectoryException(dir.toString()); }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException { throw new UnsupportedOperationException(); }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException { JPath jPath = (JPath) dir; return new JDirectoryStream(jPath, filter); }
private <T extends Resource> Map<String, T> fromDirectoryStream(Filter<Path> filter, Function<Path, T> mapper, Comparator<T> comparator) { //linkedhashmap as we want to preserve the insertion order try (DirectoryStream<Path> dirs = Files.newDirectoryStream(path, filter)) { return StreamSupport.stream(dirs.spliterator(), false) .map(mapper) .sorted(comparator) .collect(Collectors.toMap(Resource::getName, Function.identity(), (k, v) -> {throw new IllegalStateException("duplicate key " + k);}, LinkedHashMap::new)); } catch (IOException e) { throw new IllegalStateException(e); } }
public static List<Path> listFiles(Path path, Filter<? super Path> filter) throws IOException { List<Path> files = new ArrayList<>(); try(DirectoryStream<Path> ds = Files.newDirectoryStream(path, filter)) { for(Path p : ds) { files.add(p); } } return files; }
public static List<Path> getDirectories(Path path) throws IOException { List<Path> dirs = new ArrayList<>(); DirectoryStream.Filter<Path> filter = (Path file) -> (Files.isDirectory(file)); try(DirectoryStream<Path> ds = Files.newDirectoryStream(path, filter)) { for(Path p: ds) { dirs.add(p); } } return dirs; }
public static void deleteAllExtensions(final Path file) throws IOException { Filter<Path> filter = new ExtensionIgnoringFilter(file); try (DirectoryStream<Path> ds = newDirectoryStream(file.getParent(), filter)) { for (Path p : ds) deleteRecursively(p); } }
private void init() throws IOException { // 1. detect interpreter setting via interpreter-setting.json in each interpreter folder // 2. detect interpreter setting in interpreter.json that is saved before String interpreterJson = conf.getInterpreterJson(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (Files.exists(interpreterDirPath)) { for (Path interpreterDir : Files .newDirectoryStream(interpreterDirPath, new Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { return Files.exists(entry) && Files.isDirectory(entry); } })) { String interpreterDirString = interpreterDir.toString(); /** * Register interpreter by the following ordering * 1. Register it from path {ZEPPELIN_HOME}/interpreter/{interpreter_name}/ * interpreter-setting.json * 2. Register it from interpreter-setting.json in classpath * {ZEPPELIN_HOME}/interpreter/{interpreter_name} */ if (!registerInterpreterFromPath(interpreterDirString, interpreterJson)) { if (!registerInterpreterFromResource(cl, interpreterDirString, interpreterJson)) { LOGGER.warn("No interpreter-setting.json found in " + interpreterDirPath); } } } } else { LOGGER.warn("InterpreterDir {} doesn't exist", interpreterDirPath); } loadFromFile(); saveToFile(); }
/** * @param args the command line arguments */ public static void main(String[] args) throws IOException { Scanner cin = new Scanner(System.in); System.out.print("Directory Path: "); Path dir = Paths.get(cin.nextLine()); System.out.print("Filter: "); String glob = cin.nextLine().trim(); if(glob.isEmpty()){ glob="*"; } Filter<Path> filter = new GlobFilter<>(glob); if(Files.isDirectory(dir)){ for(Path p:Files.newDirectoryStream(dir,filter)){ BasicFileAttributes attr = Files.readAttributes(p, BasicFileAttributes.class); System.out.printf("%s %s%n", p, (attr.isDirectory()?"Directory":"File").toString()); } }else{ System.out.println("Not a directory."); } }
/** {@inheritDoc} */ @Override public DirectoryStream< Path > newDirectoryStream(Path dir, Filter< ? super Path > filter) throws IOException { dir = ((TestPath) dir).unwrap(); checkIfRemoved(dir); checkPermissions(dir, true, AccessMode.READ); return provider.newDirectoryStream(dir, filter); }
DirectoryStream<Path> newDirectoryStream( EphemeralFsPath dir, EphemeralFsPath relativeDir, Filter<? super Path> filter) throws IOException { synchronized(fsLock) { ResolvedPath resolvedDir = ResolvedPath.resolve(dir); if(!resolvedDir.hasTarget()) { throw new NoSuchFileException(dir.toString()); } if(!resolvedDir.getTarget().isDir()) { throw new NotDirectoryException(dir.toString()); } List<Path> parts = new ArrayList<>(); for(EphemeralFsPath childName : resolvedDir.getTarget().getChildNames()) { Path child = dir.resolve(childName); if(filter.accept(child)) { parts.add(relativeDir.resolve(child.getFileName())); } } return EphemeralFsSecureDirectoryStream.makeDirectoryStream( resolvedDir.getTarget(), relativeDir, parts); } }
private static void findExamples( List<Path> examples, Path dir, DirectoryStream.Filter<Path> filter) throws IOException { try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, filter)) { for (Path entry : stream) { if (Files.isDirectory(entry)) { findExamples(examples, entry, filter); } else { examples.add(entry); } } } }
private static List<File> listDirectoryChildren(File directory, DirectoryStream.Filter<Path> filter) { try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory.toPath(), filter)) { final List<File> result = new ArrayList<>(); for (Path entry : stream) { result.add(entry.toFile()); } return result; } catch (SecurityException | IOException ex) { throw DeploymentScannerLogger.ROOT_LOGGER.cannotListDirectoryFiles(ex, directory); } }
@Override public DirectoryStream<Resource> newResourceDirectoryStream() throws IOException { return newResourceDirectoryStream(new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { return true; } }); }
DirectoryStream<Path> newDirectoryStream( final TPath path, final Filter<? super Path> filter) throws IOException { final FsNode entry = stat(path); final Set<String> set; if (null == entry || null == (set = entry.getMembers())) throw new NotDirectoryException(path.toString()); @NotThreadSafe class Adapter implements Iterator<Path> { final Iterator<String> it = set.iterator(); @Override public boolean hasNext() { return it.hasNext(); } @Override public Path next() { return path.resolve(it.next()); } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); } } // Adapter @NotThreadSafe class FilterIterator extends FilteringIterator<Path> { FilterIterator() { super(new Adapter()); } @Override protected boolean accept(Path element) { try { return filter.accept(element); } catch (IOException ex) { throw new DirectoryIteratorException(ex); } } } // FilterIterator return new Stream(new FilterIterator()); }
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter) throws IOException { return getFileSystem().newDirectoryStream(this, filter); }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException { return promote(dir).newDirectoryStream(filter); }
@Override public DirectoryStream<Path> newDirectoryStream(Path dir, Filter<? super Path> filter) throws IOException { return null; }
DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter) throws IOException { return new ZipDirectoryStream(this, filter); }
@Override public DirectoryStream<Path> newDirectoryStream( Path path, Filter<? super Path> filter) throws IOException { return toZipPath(path).newDirectoryStream(filter); }
final DirectoryStream<Path> newDirectoryStream(Filter<? super Path> filter) throws IOException { return new JrtDirectoryStream(this, filter); }
@Override public DirectoryStream<Path> newDirectoryStream( Path path, Filter<? super Path> filter) throws IOException { return toJrtPath(path).newDirectoryStream(filter); }