/** * Return a File object that's used to search a recursive * directory containing the complete works of William Shakespeare. */ public static File getRootFolderFile(String rootFolderName){ Function<String, File> getFile = ExceptionUtils .rethrowFunction(name -> new File(ClassLoader .getSystemResource(name) .toURI())); return getFile.apply(rootFolderName); }
/** * Return a File object that's used to search a recursive * directory containing the complete works of William Shakespeare. */ public static File getRootFolderFile(String rootFolderName) { Function<String, File> getFile = ExceptionUtils .rethrowFunction(name -> new File(ClassLoader .getSystemResource(name) .toURI())); return getFile.apply(rootFolderName); }
/** * Add a new @a entry to the appropriate list of futures. */ void addEntry(Path entry) { // This adapter simplifies exception handling. Function<Path, CompletableFuture<Dirent>> getFolder = ExceptionUtils // Asynchronously create a folder from a directory file. .rethrowFunction(Folder::fromDirectory); // This adapter simplifies exception handling. Function<Path, CompletableFuture<Dirent>> getDocument = ExceptionUtils // Asynchronously create a document from a path. .rethrowFunction(Document::fromPath); // Add entry to the appropriate list of futures. if (Files.isDirectory(entry)) mSubFolderFutures.add(getFolder.apply(entry)); else mDocumentFutures.add(getDocument.apply(entry)); } }
/** * Add a new @a entry to the appropriate list of futures. */ void addEntry(Path entry, boolean parallel) { // This adapter simplifies exception handling. Function<Path, Dirent> getFolder = ExceptionUtils .rethrowFunction(file // Create a folder from a directory file. -> Folder.fromDirectory(file, parallel)); // This adapter simplifies exception handling. // Create a document from a path. Function<Path, Dirent> getDocument = ExceptionUtils .rethrowFunction(Document::fromPath); // Add entry to the appropriate list. if (Files.isDirectory(entry)) mSubFolders.add(getFolder.apply(entry)); else mDocuments.add(getDocument.apply(entry)); }
/** * @return The URL of the @a image on the given @a pageUri */ public URL getImageUrl(Element image, String pageUri) { if (mIsLocal) { int splitPos = pageUri.lastIndexOf('/'); final String prefix = splitPos > 0 ? pageUri.substring(0, splitPos) + "/" : ""; // This function (1) gets a system resource and (2) // converts checked exceptions to runtime exceptions. Function<Element, URL> getUrl = ExceptionUtils.rethrowFunction (img -> ClassLoader.getSystemResource(prefix + img.attr("src"))); // Return the URL of the image on the given pageUri. return getUrl.apply(image); } else { // Create a function that (1) returns a new URL and (2) // converts checked URL exceptions into runtime // exceptions. Function<String, URL> urlFactory = ExceptionUtils.rethrowFunction(URL::new); // Return the URL of the image on the given pageUri. return urlFactory.apply(image.attr("abs:src")); } }
/** * Factory method that asynchronously creates a document from the * file at the given @a path. * * @param path The path of the document in the file system * @return An open document */ static Dirent fromPath(Path path) { // Create an exception adapter. Function<Path, byte[]> getBytes = ExceptionUtils .rethrowFunction(Files::readAllBytes); // Create and return a new document containing all bytes of // the file at the given path. return new Document(new String(getBytes.apply(path)), path); } }
/** * Create a new URL list from a @a stringOfUrls that contains the * sURL_PREFIX list of names separated by commas and add them to * the URL list that's returned. */ private Stream<URL> convertStringToUrls(String stringOfNames) { // Create a Function that returns a new URL object when // applied and which converts checked URL exceptions into // runtime exceptions. Function<String, URL> urlFactory = ExceptionUtils.rethrowFunction(URL::new); return Pattern // Create a regular expression for the "," separator. .compile(",") // Use regular expression to split stringOfNames into a // Stream<String>. .splitAsStream(stringOfNames) // Concatenate the url prefix with each name. .map(name -> sURL_PREFIX + name) // Convert each string in the stream to a URL. .map(urlFactory); }
/** * Factory method that asynchronously creates a document from the * file at the given @a path. * * @param path The path of the document in the file system * @return A future to the document that will be complete when the * contents of the document are available */ static CompletableFuture<Dirent> fromPath(Path path) { // Return a future that completes once the document's contents // are available. return CompletableFuture .supplyAsync(() -> { Function<Path, byte[]> getBytes = ExceptionUtils .rethrowFunction(Files::readAllBytes); // Create/return a new document (wrapped in a // future) containing file contents at the path. return new Document (new String(getBytes.apply(path)), path); }); } }
/** * A function that folds a Path into the mutable result container. * * @return a function which folds a Path into a mutable result container */ @Override public BiConsumer<Folder, Path> accumulator() { Function<Path, Folder> getFolders = ExceptionUtils.rethrowFunction(file -> Folder.fromDirectory(file, mParallel)); Function<Path, Document> getDocuments = ExceptionUtils.rethrowFunction(Document::fromPath); return (Folder folder, Path entry) -> { if (Files.isDirectory(entry)) folder.getSubFolders().add(getFolders.apply(entry)); else folder.getDocuments().add(getDocuments.apply(entry)); }; }
/** * A function that folds a Path into the mutable result container. * * @return a function which folds a Path into a mutable result container */ @Override public BiConsumer<Folder, Path> accumulator() { Function<Path, Folder> getFolders = ExceptionUtils.rethrowFunction(file -> Folder.fromDirectory(file, mParallel)); Function<Path, Document> getDocuments = ExceptionUtils.rethrowFunction(Document::fromPath); return (Folder folder, Path entry) -> { if (Files.isDirectory(entry)) folder.getSubFolders().add(getFolders.apply(entry)); else folder.getDocuments().add(getDocuments.apply(entry)); }; }
ExceptionUtils.rethrowFunction(uri -> ClassLoader.getSystemResource(uri) .toURI()); ExceptionUtils.rethrowFunction(rootFile -> Jsoup.parse(rootFile, "UTF-8")); ExceptionUtils.rethrowFunction(url -> Jsoup.connect(url).get());