Use Java 8 APIs

This commit is contained in:
Gary Gregory 2022-10-05 22:02:57 -04:00
parent 341b578b4f
commit 32756ef89b
3 changed files with 18 additions and 27 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
@ -1468,9 +1469,7 @@ public class ClassUtils {
}
final Class<?>[] convertedClasses = new Class[classes.length];
for (int i = 0; i < classes.length; i++) {
convertedClasses[i] = primitiveToWrapper(classes[i]);
}
Arrays.setAll(convertedClasses, i -> primitiveToWrapper(classes[i]));
return convertedClasses;
}
@ -1497,28 +1496,28 @@ public class ClassUtils {
/**
* Converts a class name to a JLS style class name.
*
* @param className the class name
* @param canonicalName the class name
* @return the converted name
* @throws NullPointerException if the className is null
*/
private static String toCanonicalName(String className) {
className = StringUtils.deleteWhitespace(className);
Validate.notNull(className, "className");
if (className.endsWith("[]")) {
private static String toCanonicalName(final String className) {
String canonicalName = StringUtils.deleteWhitespace(className);
Validate.notNull(canonicalName, "className");
if (canonicalName.endsWith("[]")) {
final StringBuilder classNameBuffer = new StringBuilder();
while (className.endsWith("[]")) {
className = className.substring(0, className.length() - 2);
while (canonicalName.endsWith("[]")) {
canonicalName = canonicalName.substring(0, canonicalName.length() - 2);
classNameBuffer.append("[");
}
final String abbreviation = abbreviationMap.get(className);
final String abbreviation = abbreviationMap.get(canonicalName);
if (abbreviation != null) {
classNameBuffer.append(abbreviation);
} else {
classNameBuffer.append("L").append(className).append(";");
classNameBuffer.append("L").append(canonicalName).append(";");
}
className = classNameBuffer.toString();
canonicalName = classNameBuffer.toString();
}
return className;
return canonicalName;
}
/**
@ -1541,9 +1540,7 @@ public class ClassUtils {
return ArrayUtils.EMPTY_CLASS_ARRAY;
}
final Class<?>[] classes = new Class[array.length];
for (int i = 0; i < array.length; i++) {
classes[i] = array[i] == null ? null : array[i].getClass();
}
Arrays.setAll(classes, i -> array[i] == null ? null : array[i].getClass());
return classes;
}
@ -1595,9 +1592,7 @@ public class ClassUtils {
}
final Class<?>[] convertedClasses = new Class[classes.length];
for (int i = 0; i < classes.length; i++) {
convertedClasses[i] = wrapperToPrimitive(classes[i]);
}
Arrays.setAll(convertedClasses, i -> wrapperToPrimitive(classes[i]));
return convertedClasses;
}

View File

@ -19,6 +19,7 @@ package org.apache.commons.lang3;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.concurrent.Callable;
@ -631,10 +632,7 @@ public class Functions {
final FailableConsumer<Throwable, ? extends Throwable> errorHandler,
final FailableRunnable<? extends Throwable>... resources) {
org.apache.commons.lang3.function.FailableRunnable<?>[] fr = new org.apache.commons.lang3.function.FailableRunnable[resources.length];
for (int i = 0; i < resources.length; i++) {
final int fi = i;
fr[i] = () -> resources[fi].run();
}
Arrays.setAll(fr, i -> () -> resources[i].run());
Failable.tryWithResources(action::run, errorHandler != null ? errorHandler::accept : null, fr);
}

View File

@ -8194,9 +8194,7 @@ public class StringUtils {
return strs;
}
final String[] newArr = new String[strsLen];
for (int i = 0; i < strsLen; i++) {
newArr[i] = strip(strs[i], stripChars);
}
Arrays.setAll(newArr, i -> strip(strs[i], stripChars));
return newArr;
}