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 14e02df  Refactor to avoid creating a new ArrayList on all API calls.
14e02df is described below

commit 14e02dfe7e33b780ed3ac521018446fda60aa1a8
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Thu Jan 28 13:05:31 2021 -0500

    Refactor to avoid creating a new ArrayList on all API calls.
---
 .../commons/io/output/FilterCollectionWriter.java  | 108 +++++++++++++--------
 1 file changed, 67 insertions(+), 41 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java 
b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
index aa830d2..1b8ab43 100644
--- a/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
+++ b/src/main/java/org/apache/commons/io/output/FilterCollectionWriter.java
@@ -73,21 +73,37 @@ public class FilterCollectionWriter extends Writer {
         this.writers = writers == null ? EMPTY_WRITERS : 
Arrays.asList(writers);
     }
 
+    /**
+     * Adds an indexed exception to the list.
+     *
+     * @param causeList The target list.
+     * @param i The index.
+     * @param e The cause.
+     * @return the given list or a new list on null input.
+     */
+    private List<Exception> add(List<Exception> causeList, int i, final 
IOException e) {
+        if (causeList == null) {
+            causeList = new ArrayList<>();
+        }
+        causeList.add(new IOIndexedException(i, e));
+        return causeList;
+    }
+
     @Override
     public Writer append(final char c) throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.append(c);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("append", causeList);
         }
         return this;
@@ -95,19 +111,19 @@ public class FilterCollectionWriter extends Writer {
 
     @Override
     public Writer append(final CharSequence csq) throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.append(csq);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("append", causeList);
         }
         return this;
@@ -116,19 +132,19 @@ public class FilterCollectionWriter extends Writer {
     @Override
     public Writer append(final CharSequence csq, final int start, final int 
end) throws IOException {
 
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.append(csq, start, end);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("append", causeList);
         }
         return this;
@@ -136,19 +152,19 @@ public class FilterCollectionWriter extends Writer {
 
     @Override
     public void close() throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.close();
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("close", causeList);
         }
 
@@ -161,67 +177,77 @@ public class FilterCollectionWriter extends Writer {
      */
     @Override
     public void flush() throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.flush();
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("flush", causeList);
         }
 
     }
 
     /**
-     * Writes a portion of an array of characters.
-     *
-     * @param cbuf Buffer of characters to be written
-     * @param off  Offset from which to start reading characters
-     * @param len  Number of characters to be written
-     *
-     * @exception IOException If an I/O error occurs
+     * Tests if the given list is empty in a null-safe manner.
+     * 
+     * @param causeList the list to test.
+     * @return true if empty or null.
      */
+    private boolean notEmpty(List<Exception> causeList) {
+        return causeList != null && !causeList.isEmpty();
+    }
+
     @Override
-    public void write(final char[] cbuf, final int off, final int len) throws 
IOException {
-        final List<Exception> causeList = new ArrayList<>();
+    public void write(final char[] cbuf) throws IOException {
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
-                    w.write(cbuf, off, len);
+                    w.write(cbuf);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("write", causeList);
         }
     }
 
+    /**
+     * Writes a portion of an array of characters.
+     *
+     * @param cbuf Buffer of characters to be written
+     * @param off  Offset from which to start reading characters
+     * @param len  Number of characters to be written
+     *
+     * @exception IOException If an I/O error occurs
+     */
     @Override
-    public void write(final char[] cbuf) throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+    public void write(final char[] cbuf, final int off, final int len) throws 
IOException {
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
-                    w.write(cbuf);
+                    w.write(cbuf, off, len);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("write", causeList);
         }
     }
@@ -233,38 +259,38 @@ public class FilterCollectionWriter extends Writer {
      */
     @Override
     public void write(final int c) throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.write(c);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("write", causeList);
         }
     }
 
     @Override
     public void write(final String str) throws IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.write(str);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("write", causeList);
         }
 
@@ -281,19 +307,19 @@ public class FilterCollectionWriter extends Writer {
      */
     @Override
     public void write(final String str, final int off, final int len) throws 
IOException {
-        final List<Exception> causeList = new ArrayList<>();
+        List<Exception> causeList = null;
         int i = 0;
         for (final Writer w : writers) {
             if (w != null) {
                 try {
                     w.write(str, off, len);
                 } catch (final IOException e) {
-                    causeList.add(new IOIndexedException(i, e));
+                    causeList = add(causeList, i, e);
                 }
             }
             i++;
         }
-        if (!causeList.isEmpty()) {
+        if (notEmpty(causeList)) {
             throw new IOExceptionList("write", causeList);
         }
 

Reply via email to