List of usage examples for java.nio.file DirectoryStream.Filter DirectoryStream.Filter
DirectoryStream.Filter
From source file:com.cloudbees.clickstack.util.Files2.java
/** * @param srcDir/*ww w .jav a 2 s. c o m*/ * @param pattern * @return * @throws RuntimeIOException * @throws IllegalStateException More or less than 1 child dir found */ @Nonnull public static Path findUniqueDirectoryBeginningWith(@Nonnull Path srcDir, @Nonnull final String pattern) throws RuntimeIOException, IllegalStateException { Preconditions.checkArgument(Files.isDirectory(srcDir), "Source %s is not a directory", srcDir.toAbsolutePath()); DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { String fileName = entry.getFileName().toString(); if (pattern == null) { return true; } else if (fileName.startsWith(pattern)) { return true; } else { return false; } } }; try (DirectoryStream<Path> paths = Files.newDirectoryStream(srcDir, filter)) { try { return Iterables.getOnlyElement(paths); } catch (NoSuchElementException e) { throw new IllegalStateException("Directory beginning with '" + pattern + "' not found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath()); } catch (IllegalArgumentException e) { throw new IllegalStateException( "More than 1 directory beginning with '" + pattern + "' found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath() + " -> " + paths); } } catch (IOException e) { throw new RuntimeIOException( "Exception finding unique child directory beginning with " + filter + "in " + srcDir); } }
From source file:com.cloudbees.clickstack.util.Files2.java
/** * Find a file matching {@code $artifactId*$type} in the given {@code srcDir}. * * @param srcDir/*w w w. j a va2s .co m*/ * @param artifactId * @param type * @return * @throws IllegalStateException More or less than 1 matching artifact found * @throws RuntimeIOException */ @Nonnull public static Path findArtifact(@Nonnull Path srcDir, @Nonnull final String artifactId, @Nonnull final String type) throws RuntimeIOException, IllegalStateException { Preconditions.checkArgument(Files.isDirectory(srcDir), "Source %s is not a directory", srcDir.toAbsolutePath()); DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() { @Override public boolean accept(Path entry) throws IOException { String fileName = entry.getFileName().toString(); if (fileName.startsWith(artifactId) && fileName.endsWith("." + type)) { return true; } else { return false; } } }; try (DirectoryStream<Path> paths = Files.newDirectoryStream(srcDir, filter)) { try { return Iterables.getOnlyElement(paths); } catch (NoSuchElementException e) { throw new IllegalStateException("Artifact '" + artifactId + ":" + type + "' not found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath()); } catch (IllegalArgumentException e) { throw new IllegalStateException( "More than 1 version of artifact '" + artifactId + ":" + type + "' found in path: " + srcDir + ", absolutePath: " + srcDir.toAbsolutePath() + " -> " + paths); } } catch (IOException e) { throw new RuntimeIOException("Exception finding artifact " + artifactId + "@" + type + " in " + srcDir); } }