This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-io.git
The following commit(s) were added to refs/heads/master by this push: new 166dea37 Add and reuse IOConsumer.forEach(*) and forEachIndexed(*). 166dea37 is described below commit 166dea37e810fcfa9c72cf1f107fd38cfb432486 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Jul 19 12:10:54 2022 -0400 Add and reuse IOConsumer.forEach(*) and forEachIndexed(*). Use forEach and streams. --- src/changes/changes.xml | 2 +- .../org/apache/commons/io/FileSystemUtils.java | 20 +-- .../java/org/apache/commons/io/FilenameUtils.java | 8 +- src/main/java/org/apache/commons/io/IOUtils.java | 134 ++++++++++++--------- .../java/org/apache/commons/io/file/PathUtils.java | 2 +- .../commons/io/file/StandardDeleteOption.java | 9 +- .../commons/io/filefilter/AndFileFilter.java | 5 +- .../commons/io/filefilter/FileFilterUtils.java | 8 +- .../apache/commons/io/filefilter/OrFileFilter.java | 5 +- .../org/apache/commons/io/function/IOConsumer.java | 31 ++++- .../org/apache/commons/io/function/IOStreams.java | 40 ++++-- .../commons/io/input/ObservableInputStream.java | 5 +- .../apache/commons/io/input/XmlStreamReader.java | 9 +- .../commons/io/monitor/FileAlterationMonitor.java | 5 +- .../commons/io/monitor/FileAlterationObserver.java | 5 +- .../serialization/ValidatingObjectInputStream.java | 17 +-- 16 files changed, 166 insertions(+), 139 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 83760b2f..cf8400d3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -357,7 +357,7 @@ The <action> type attribute can be add,update,fix,remove. Add IOBiConsumer. </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> - Add and reuse IOConsumer.forEach(T[], IOConsumer) and forEachIndexed(Stream, IOConsumer). + Add and reuse IOConsumer.forEach(*) and forEachIndexed(*). </action> <action dev="ggregory" type="add" due-to="Gary Gregory"> Add CharsetEncoders. diff --git a/src/main/java/org/apache/commons/io/FileSystemUtils.java b/src/main/java/org/apache/commons/io/FileSystemUtils.java index 85763ecd..884368d7 100644 --- a/src/main/java/org/apache/commons/io/FileSystemUtils.java +++ b/src/main/java/org/apache/commons/io/FileSystemUtils.java @@ -23,12 +23,12 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.nio.charset.Charset; import java.time.Duration; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Locale; import java.util.Objects; import java.util.StringTokenizer; +import java.util.stream.Collectors; /** * General File System utilities. @@ -487,7 +487,7 @@ public class FileSystemUtils { // however, it's still not perfect as the JDK support is so poor // (see commons-exec or Ant for a better multithreaded multi-os solution) - final List<String> lines = new ArrayList<>(20); + final List<String> lines; Process proc = null; InputStream in = null; OutputStream out = null; @@ -503,12 +503,8 @@ public class FileSystemUtils { err = proc.getErrorStream(); // default charset is most likely appropriate here inr = new BufferedReader(new InputStreamReader(in, Charset.defaultCharset())); - String line = inr.readLine(); - while (line != null && lines.size() < max) { - line = line.toLowerCase(Locale.ENGLISH).trim(); - lines.add(line); - line = inr.readLine(); - } + + lines = inr.lines().limit(max).map(line -> line.toLowerCase(Locale.ENGLISH).trim()).collect(Collectors.toList()); proc.waitFor(); @@ -516,15 +512,11 @@ public class FileSystemUtils { if (proc.exitValue() != 0) { // OS command problem, throw exception - throw new IOException( - "Command line returned OS error code '" + proc.exitValue() + - "' for command " + Arrays.asList(cmdAttribs)); + throw new IOException("Command line returned OS error code '" + proc.exitValue() + "' for command " + Arrays.asList(cmdAttribs)); } if (lines.isEmpty()) { // unknown problem, throw exception - throw new IOException( - "Command line did not return any info " + - "for command " + Arrays.asList(cmdAttribs)); + throw new IOException("Command line did not return any info " + "for command " + Arrays.asList(cmdAttribs)); } inr.close(); diff --git a/src/main/java/org/apache/commons/io/FilenameUtils.java b/src/main/java/org/apache/commons/io/FilenameUtils.java index c23384df..88775013 100644 --- a/src/main/java/org/apache/commons/io/FilenameUtils.java +++ b/src/main/java/org/apache/commons/io/FilenameUtils.java @@ -25,6 +25,7 @@ import java.util.Deque; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; /** * General file name and file path manipulation utilities. @@ -1054,12 +1055,7 @@ public class FilenameUtils { return indexOfExtension(fileName) == NOT_FOUND; } final String fileExt = getExtension(fileName); - for (final String extension : extensions) { - if (fileExt.equals(extension)) { - return true; - } - } - return false; + return Stream.of(extensions).anyMatch(fileExt::equals); } /** diff --git a/src/main/java/org/apache/commons/io/IOUtils.java b/src/main/java/org/apache/commons/io/IOUtils.java index a798a75f..fde29d42 100644 --- a/src/main/java/org/apache/commons/io/IOUtils.java +++ b/src/main/java/org/apache/commons/io/IOUtils.java @@ -31,6 +31,7 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; +import java.io.UncheckedIOException; import java.io.Writer; import java.net.HttpURLConnection; import java.net.ServerSocket; @@ -45,12 +46,13 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.Selector; import java.nio.charset.Charset; import java.nio.file.Files; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.function.Consumer; +import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.io.function.IOConsumer; @@ -428,6 +430,15 @@ public class IOUtils { } } + /** + * Avoids the need to type cast. + * + * @param closeable the object to close, may be null + */ + private static void closeQ(final Closeable closeable) { + closeQuietly(closeable, null); + } + /** * Closes a {@link Closeable} unconditionally. * @@ -473,15 +484,6 @@ public class IOUtils { closeQuietly(closeable, null); } - /** - * Avoids the need to type cast. - * - * @param closeable the object to close, may be null - */ - private static void closeQ(final Closeable closeable) { - closeQuietly(closeable, null); - } - /** * Closes a {@link Closeable} unconditionally. * <p> @@ -602,22 +604,6 @@ public class IOUtils { } } - /** - * Closes a stream of {@link Closeable} unconditionally. - * <p> - * Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored. - * </p> - * - * @param closeables the objects to close, may be null or already closed - * @see #closeQuietly(Closeable) - * @since 2.12.0 - */ - public static void closeQuietly(final Stream<Closeable> closeables) { - if (closeables != null) { - closeables.forEach(IOUtils::closeQuietly); - } - } - /** * Closes an {@link OutputStream} unconditionally. * <p> @@ -784,6 +770,22 @@ public class IOUtils { closeQ(socket); } + /** + * Closes a stream of {@link Closeable} unconditionally. + * <p> + * Equivalent calling {@link Closeable#close()} on each element, except any exceptions will be ignored. + * </p> + * + * @param closeables the objects to close, may be null or already closed + * @see #closeQuietly(Closeable) + * @since 2.12.0 + */ + public static void closeQuietly(final Stream<Closeable> closeables) { + if (closeables != null) { + closeables.forEach(IOUtils::closeQuietly); + } + } + /** * Closes an {@link Writer} unconditionally. * <p> @@ -895,6 +897,19 @@ public class IOUtils { } } + // TODO Consider making public + private static boolean contentEquals(final Iterator<?> iterator1, final Iterator<?> iterator2) { + while (iterator1.hasNext()) { + if (!iterator2.hasNext()) { + return false; + } + if (!Objects.equals(iterator1.next(), iterator2.next())) { + return false; + } + } + return !iterator2.hasNext(); + } + /** * Compares the contents of two Readers to determine if they are equal or not. * <p> @@ -953,6 +968,28 @@ public class IOUtils { } } + // TODO Consider making public + private static boolean contentEquals(final Stream<?> stream1, final Stream<?> stream2) { + if (stream1 == stream2) { + return true; + } + if (stream1 == null || stream2 == null) { + return false; + } + return contentEquals(stream1.iterator(), stream2.iterator()); + } + + // TODO Consider making public + private static boolean contentEqualsIgnoreEOL(final BufferedReader reader1, final BufferedReader reader2) { + if (reader1 == reader2) { + return true; + } + if (reader1 == null || reader2 == null) { + return false; + } + return contentEquals(reader1.lines(), reader2.lines()); + } + /** * Compares the contents of two Readers to determine if they are equal or * not, ignoring EOL characters. @@ -965,28 +1002,18 @@ public class IOUtils { * @param reader2 the second reader * @return true if the content of the readers are equal (ignoring EOL differences), false otherwise * @throws NullPointerException if either input is null - * @throws IOException if an I/O error occurs + * @throws UncheckedIOException if an I/O error occurs * @since 2.2 */ @SuppressWarnings("resource") - public static boolean contentEqualsIgnoreEOL(final Reader reader1, final Reader reader2) - throws IOException { + public static boolean contentEqualsIgnoreEOL(final Reader reader1, final Reader reader2) throws UncheckedIOException { if (reader1 == reader2) { return true; } - if (reader1 == null ^ reader2 == null) { + if (reader1 == null || reader2 == null) { return false; } - final BufferedReader br1 = toBufferedReader(reader1); - final BufferedReader br2 = toBufferedReader(reader2); - - String line1 = br1.readLine(); - String line2 = br2.readLine(); - while (line1 != null && line1.equals(line2)) { - line1 = br1.readLine(); - line2 = br2.readLine(); - } - return Objects.equals(line1, line2); + return contentEqualsIgnoreEOL(toBufferedReader(reader1), toBufferedReader(reader2)); } /** @@ -2042,12 +2069,12 @@ public class IOUtils { * @param input the {@link InputStream} to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null - * @throws IOException if an I/O error occurs + * @throws UncheckedIOException if an I/O error occurs * @since 1.1 * @deprecated 2.5 use {@link #readLines(InputStream, Charset)} instead */ @Deprecated - public static List<String> readLines(final InputStream input) throws IOException { + public static List<String> readLines(final InputStream input) throws UncheckedIOException { return readLines(input, Charset.defaultCharset()); } @@ -2063,12 +2090,11 @@ public class IOUtils { * @param charset the charset to use, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null - * @throws IOException if an I/O error occurs + * @throws UncheckedIOException if an I/O error occurs * @since 2.3 */ - public static List<String> readLines(final InputStream input, final Charset charset) throws IOException { - final InputStreamReader reader = new InputStreamReader(input, Charsets.toCharset(charset)); - return readLines(reader); + public static List<String> readLines(final InputStream input, final Charset charset) throws UncheckedIOException { + return readLines(new InputStreamReader(input, Charsets.toCharset(charset))); } /** @@ -2087,13 +2113,13 @@ public class IOUtils { * @param charsetName the name of the requested charset, null means platform default * @return the list of Strings, never null * @throws NullPointerException if the input is null - * @throws IOException if an I/O error occurs + * @throws UncheckedIOException if an I/O error occurs * @throws java.nio.charset.UnsupportedCharsetException thrown instead of {@link java.io * .UnsupportedEncodingException} in version 2.2 if the * encoding is not supported. * @since 1.1 */ - public static List<String> readLines(final InputStream input, final String charsetName) throws IOException { + public static List<String> readLines(final InputStream input, final String charsetName) throws UncheckedIOException { return readLines(input, Charsets.toCharset(charsetName)); } @@ -2108,18 +2134,12 @@ public class IOUtils { * @param reader the {@link Reader} to read from, not null * @return the list of Strings, never null * @throws NullPointerException if the input is null - * @throws IOException if an I/O error occurs + * @throws UncheckedIOException if an I/O error occurs * @since 1.1 */ @SuppressWarnings("resource") // reader wraps input and is the responsibility of the caller. - public static List<String> readLines(final Reader reader) throws IOException { - final BufferedReader bufReader = toBufferedReader(reader); - final List<String> list = new ArrayList<>(); - String line; - while ((line = bufReader.readLine()) != null) { - list.add(line); - } - return list; + public static List<String> readLines(final Reader reader) throws UncheckedIOException { + return toBufferedReader(reader).lines().collect(Collectors.toList()); } /** diff --git a/src/main/java/org/apache/commons/io/file/PathUtils.java b/src/main/java/org/apache/commons/io/file/PathUtils.java index 85b1d5be..f8b2d470 100644 --- a/src/main/java/org/apache/commons/io/file/PathUtils.java +++ b/src/main/java/org/apache/commons/io/file/PathUtils.java @@ -820,7 +820,7 @@ public final class PathUtils { * where {@link File#lastModified()} looses milliseconds and always ends in 000. This bug is in OpenJDK 8 and 9, and * fixed in 11. * </p> - * + * * @param file the file to query. * @return the file's last modified time. * @throws IOException Thrown if an I/O error occurs. diff --git a/src/main/java/org/apache/commons/io/file/StandardDeleteOption.java b/src/main/java/org/apache/commons/io/file/StandardDeleteOption.java index 4c31b7f4..c5c2af96 100644 --- a/src/main/java/org/apache/commons/io/file/StandardDeleteOption.java +++ b/src/main/java/org/apache/commons/io/file/StandardDeleteOption.java @@ -17,6 +17,8 @@ package org.apache.commons.io.file; +import java.util.stream.Stream; + import org.apache.commons.io.IOUtils; /** @@ -43,12 +45,7 @@ public enum StandardDeleteOption implements DeleteOption { if (IOUtils.length(options) == 0) { return false; } - for (final DeleteOption deleteOption : options) { - if (deleteOption == StandardDeleteOption.OVERRIDE_READ_ONLY) { - return true; - } - } - return false; + return Stream.of(options).anyMatch(e -> StandardDeleteOption.OVERRIDE_READ_ONLY == e); } } diff --git a/src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java index 6afb3fc9..e29f780e 100644 --- a/src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Stream; /** * A {@link java.io.FileFilter} providing conditional AND logic across a list of @@ -148,9 +149,7 @@ public class AndFileFilter * @since 2.9.0 */ public void addFileFilter(final IOFileFilter... fileFilters) { - for (final IOFileFilter fileFilter : Objects.requireNonNull(fileFilters, "fileFilters")) { - addFileFilter(fileFilter); - } + Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter); } /** diff --git a/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java b/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java index e47655dd..c1c205ee 100644 --- a/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java +++ b/src/main/java/org/apache/commons/io/filefilter/FileFilterUtils.java @@ -19,7 +19,6 @@ package org.apache.commons.io.filefilter; import java.io.File; import java.io.FileFilter; import java.io.FilenameFilter; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Date; @@ -723,12 +722,7 @@ public class FileFilterUtils { * @since 2.0 */ public static List<IOFileFilter> toList(final IOFileFilter... filters) { - Objects.requireNonNull(filters, "filters"); - final List<IOFileFilter> list = new ArrayList<>(filters.length); - for (final IOFileFilter filter : filters) { - list.add(Objects.requireNonNull(filter, "filters[i]")); - } - return list; + return Stream.of(Objects.requireNonNull(filters, "filters")).map(Objects::requireNonNull).collect(Collectors.toList()); } /** diff --git a/src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java b/src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java index 18016ed5..d8a6b19c 100644 --- a/src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java +++ b/src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; +import java.util.stream.Stream; /** * A {@link java.io.FileFilter} providing conditional OR logic across a list of file filters. This filter returns @@ -141,9 +142,7 @@ public class OrFileFilter extends AbstractFileFilter implements ConditionalFileF * @since 2.9.0 */ public void addFileFilter(final IOFileFilter... fileFilters) { - for (final IOFileFilter fileFilter : Objects.requireNonNull(fileFilters, "fileFilters")) { - addFileFilter(fileFilter); - } + Stream.of(Objects.requireNonNull(fileFilters, "fileFilters")).forEach(this::addFileFilter); } /** diff --git a/src/main/java/org/apache/commons/io/function/IOConsumer.java b/src/main/java/org/apache/commons/io/function/IOConsumer.java index bd52b607..a8492d2b 100644 --- a/src/main/java/org/apache/commons/io/function/IOConsumer.java +++ b/src/main/java/org/apache/commons/io/function/IOConsumer.java @@ -18,6 +18,7 @@ package org.apache.commons.io.function; import java.io.IOException; +import java.util.Collection; import java.util.Objects; import java.util.function.Consumer; import java.util.stream.Stream; @@ -40,7 +41,33 @@ public interface IOConsumer<T> { IOConsumer<?> NOOP_IO_CONSUMER = t -> {/* noop */}; /** - * Performs an action for each element of this stream. + * Performs an action for each element of the collection. + * + * @param <T> The element type. + * @param collection The input to stream. + * @param action The action to apply to each input element. + * @throws IOException if an I/O error occurs. + * @since 2.12.0 + */ + static <T> void forEach(final Collection<T> collection, final IOConsumer<T> action) throws IOException { + IOStreams.forEach(IOStreams.of(collection), action); + } + + /** + * Performs an action for each element of the stream. + * + * @param <T> The element type. + * @param stream The input to stream. + * @param action The action to apply to each input element. + * @throws IOException if an I/O error occurs. + * @since 2.12.0 + */ + static <T> void forEach(final Stream<T> stream, final IOConsumer<T> action) throws IOException { + IOStreams.forEach(stream, action); + } + + /** + * Performs an action for each element of this array. * * @param <T> The element type. * @param array The input to stream. @@ -53,7 +80,7 @@ public interface IOConsumer<T> { } /** - * Performs an action for each element of this stream. + * Performs an action for each element of the stream. * * @param <T> The element type. * @param stream The input to stream. diff --git a/src/main/java/org/apache/commons/io/function/IOStreams.java b/src/main/java/org/apache/commons/io/function/IOStreams.java index bbcf78c8..21f344bd 100644 --- a/src/main/java/org/apache/commons/io/function/IOStreams.java +++ b/src/main/java/org/apache/commons/io/function/IOStreams.java @@ -19,6 +19,7 @@ package org.apache.commons.io.function; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -40,18 +41,35 @@ class IOStreams { throws IOExceptionList { final AtomicReference<List<IOException>> causeList = new AtomicReference<>(); final AtomicInteger index = new AtomicInteger(); - stream.forEach(e -> { - try { - action.accept(e); - } catch (final IOException ioex) { - if (causeList.get() == null) { - causeList.set(new ArrayList<>()); + final IOConsumer<T> actualAction = action != null ? action : IOConsumer.noop(); + if (stream != null) { + stream.forEach(e -> { + try { + actualAction.accept(e); + } catch (final IOException ioex) { + if (causeList.get() == null) { + causeList.set(new ArrayList<>()); + } + if (exSupplier != null) { + causeList.get().add(exSupplier.apply(index.get(), ioex)); + } } - causeList.get().add(exSupplier.apply(index.get(), ioex)); - } - index.incrementAndGet(); - }); - IOExceptionList.checkEmpty(causeList.get(), null); + index.incrementAndGet(); + }); + IOExceptionList.checkEmpty(causeList.get(), null); + }} + + /** + * Null-safe version of {@link Collection#stream()}. + * + * Copied from Apache Commons Lang. + * + * @param <T> the type of stream elements. + * @param values the elements of the new stream, may be {@code null}. + * @return the new stream on {@code values} or {@link Stream#empty()}. + */ + static <T> Stream<T> of(final Collection<T> values) { + return values == null ? Stream.empty() : values.stream(); } /** diff --git a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java index d83ce9c0..3acf9938 100644 --- a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java +++ b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java @@ -175,10 +175,7 @@ public class ObservableInputStream extends ProxyInputStream { } private void forEachObserver(final IOConsumer<Observer> action) throws IOException { - Objects.requireNonNull(action); - for (final Observer t : observers) { - action.accept(t); - } + IOConsumer.forEach(observers, Objects.requireNonNull(action)); } /** diff --git a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java index 2efeb39b..7ca6c19b 100644 --- a/src/main/java/org/apache/commons/io/input/XmlStreamReader.java +++ b/src/main/java/org/apache/commons/io/input/XmlStreamReader.java @@ -38,6 +38,7 @@ import java.util.regex.Pattern; import org.apache.commons.io.ByteOrderMark; import org.apache.commons.io.IOUtils; +import org.apache.commons.io.function.IOConsumer; /** * Character stream that handles all the necessary Voodoo to figure out the @@ -214,13 +215,9 @@ public class XmlStreamReader extends Reader { final int bytesRead = offset; if (bytesRead > 0) { inputStream.reset(); - final BufferedReader bReader = new BufferedReader(new StringReader( - xmlProlog.substring(0, firstGT + 1))); + final BufferedReader bReader = new BufferedReader(new StringReader(xmlProlog.substring(0, firstGT + 1))); final StringBuilder prolog = new StringBuilder(); - String line; - while ((line = bReader.readLine()) != null) { - prolog.append(line); - } + IOConsumer.forEach(bReader.lines(), prolog::append); final Matcher m = ENCODING_PATTERN.matcher(prolog); if (m.find()) { encoding = m.group(1).toUpperCase(Locale.ROOT); diff --git a/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java b/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java index 4384b3b6..d2b418e1 100644 --- a/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java +++ b/src/main/java/org/apache/commons/io/monitor/FileAlterationMonitor.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ThreadFactory; +import java.util.stream.Stream; /** * A runnable that spawns a monitoring thread triggering any @@ -86,9 +87,7 @@ public final class FileAlterationMonitor implements Runnable { public FileAlterationMonitor(final long interval, final FileAlterationObserver... observers) { this(interval); if (observers != null) { - for (final FileAlterationObserver observer : observers) { - addObserver(observer); - } + Stream.of(observers).forEach(this::addObserver); } } diff --git a/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java b/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java index 9e9687ca..c05c18f5 100644 --- a/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java +++ b/src/main/java/org/apache/commons/io/monitor/FileAlterationObserver.java @@ -24,6 +24,7 @@ import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.stream.Stream; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOCase; @@ -315,9 +316,7 @@ public class FileAlterationObserver implements Serializable { listener.onFileCreate(entry.getFile()); } }); - for (final FileEntry aChildren : entry.getChildren()) { - doCreate(aChildren); - } + Stream.of(entry.getChildren()).forEach(this::doCreate); } /** diff --git a/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java b/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java index a27d72cd..bde163c7 100644 --- a/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java +++ b/src/main/java/org/apache/commons/io/serialization/ValidatingObjectInputStream.java @@ -26,6 +26,7 @@ import java.io.ObjectStreamClass; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import java.util.stream.Stream; /** * An {@link ObjectInputStream} that's restricted to deserialize @@ -67,9 +68,7 @@ public class ValidatingObjectInputStream extends ObjectInputStream { * @return this object */ public ValidatingObjectInputStream accept(final Class<?>... classes) { - for (final Class<?> c : classes) { - acceptMatchers.add(new FullClassNameMatcher(c.getName())); - } + Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(acceptMatchers::add); return this; } @@ -106,9 +105,7 @@ public class ValidatingObjectInputStream extends ObjectInputStream { * @return this object */ public ValidatingObjectInputStream accept(final String... patterns) { - for (final String pattern : patterns) { - acceptMatchers.add(new WildcardClassNameMatcher(pattern)); - } + Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(acceptMatchers::add); return this; } @@ -132,9 +129,7 @@ public class ValidatingObjectInputStream extends ObjectInputStream { * @return this object */ public ValidatingObjectInputStream reject(final Class<?>... classes) { - for (final Class<?> c : classes) { - rejectMatchers.add(new FullClassNameMatcher(c.getName())); - } + Stream.of(classes).map(c -> new FullClassNameMatcher(c.getName())).forEach(rejectMatchers::add); return this; } @@ -171,9 +166,7 @@ public class ValidatingObjectInputStream extends ObjectInputStream { * @return this object */ public ValidatingObjectInputStream reject(final String... patterns) { - for (final String pattern : patterns) { - rejectMatchers.add(new WildcardClassNameMatcher(pattern)); - } + Stream.of(patterns).map(WildcardClassNameMatcher::new).forEach(rejectMatchers::add); return this; }