Refactor commons code
MODIFY
src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java
src/main/java/org/apache/commons/io/filefilter/AbstractFileFilter.java
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.List;
import java.util.Objects;
import org.apache.commons.io.file.PathFilter;
@@ -97,6 +98,24 @@ public boolean accept(final File dir, final String name) {
return accept(new File(dir, name));
}
+ void append(final List<?> list, final StringBuilder buffer) {
+ for (int i = 0; i < list.size(); i++) {
+ if (i > 0) {
+ buffer.append(",");
+ }
+ buffer.append(list.get(i));
+ }
+ }
+
+ void append(final Object[] array, final StringBuilder buffer) {
+ for (int i = 0; i < array.length; i++) {
+ if (i > 0) {
+ buffer.append(",");
+ }
+ buffer.append(array[i]);
+ }
+ }
+
/**
* Handles exceptions caught while accepting.
*

MODIFY
src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java
src/main/java/org/apache/commons/io/filefilter/AndFileFilter.java
@@ -215,12 +215,7 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- for (int i = 0; i < fileFilters.size(); i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(fileFilters.get(i));
- }
+ append(fileFilters, buffer);
buffer.append(")");
return buffer.toString();
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
src/main/java/org/apache/commons/io/filefilter/NameFileFilter.java
@@ -207,14 +207,7 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- if (names != null) {
- for (int i = 0; i < names.length; i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(names[i]);
- }
- }
+ append(names, buffer);
buffer.append(")");
return buffer.toString();
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java
src/main/java/org/apache/commons/io/filefilter/OrFileFilter.java
@@ -196,14 +196,7 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- if (fileFilters != null) {
- for (int i = 0; i < fileFilters.size(); i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(fileFilters.get(i));
- }
- }
+ append(fileFilters, buffer);
buffer.append(")");
return buffer.toString();
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/PrefixFileFilter.java
@@ -209,14 +209,7 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- if (prefixes != null) {
- for (int i = 0; i < prefixes.length; i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(prefixes[i]);
- }
- }
+ append(prefixes, buffer);
buffer.append(")");
return buffer.toString();
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
src/main/java/org/apache/commons/io/filefilter/SuffixFileFilter.java
@@ -209,14 +209,7 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- if (suffixes != null) {
- for (int i = 0; i < suffixes.length; i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(suffixes[i]);
- }
- }
+ append(suffixes, buffer);
buffer.append(")");
return buffer.toString();
}

MODIFY
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
src/main/java/org/apache/commons/io/filefilter/WildcardFileFilter.java
@@ -209,14 +209,8 @@ public String toString() {
final StringBuilder buffer = new StringBuilder();
buffer.append(super.toString());
buffer.append("(");
- for (int i = 0; i < wildcards.length; i++) {
- if (i > 0) {
- buffer.append(",");
- }
- buffer.append(wildcards[i]);
- }
+ append(wildcards, buffer);
buffer.append(")");
return buffer.toString();
}
-
}

Refactor commons code