Use forEach()

This commit is contained in:
Gary Gregory 2022-07-15 10:31:16 -04:00
parent 46b15a2ecb
commit 2c16052f94
9 changed files with 36 additions and 71 deletions

View File

@ -231,14 +231,9 @@ public class CharSet implements Serializable {
* @return {@code true} if the set contains the characters
*/
public boolean contains(final char ch) {
synchronized(set) {
for (final CharRange range : set) {
if (range.contains(ch)) {
return true;
}
}
synchronized (set) {
return set.stream().anyMatch(range -> range.contains(ch));
}
return false;
}
// Basics

View File

@ -127,13 +127,11 @@ public class ClassUtils {
private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<>();
static {
for (final Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperMap.entrySet()) {
final Class<?> primitiveClass = entry.getKey();
final Class<?> wrapperClass = entry.getValue();
primitiveWrapperMap.forEach((primitiveClass, wrapperClass) -> {
if (!primitiveClass.equals(wrapperClass)) {
wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
}
}
});
}
/**
@ -148,21 +146,17 @@ public class ClassUtils {
/** Feed abbreviation maps. */
static {
final Map<String, String> m = new HashMap<>();
m.put("int", "I");
m.put("boolean", "Z");
m.put("float", "F");
m.put("long", "J");
m.put("short", "S");
m.put("byte", "B");
m.put("double", "D");
m.put("char", "C");
final Map<String, String> r = new HashMap<>();
for (final Map.Entry<String, String> e : m.entrySet()) {
r.put(e.getValue(), e.getKey());
}
abbreviationMap = Collections.unmodifiableMap(m);
reverseAbbreviationMap = Collections.unmodifiableMap(r);
final Map<String, String> map = new HashMap<>();
map.put("int", "I");
map.put("boolean", "Z");
map.put("float", "F");
map.put("long", "J");
map.put("short", "S");
map.put("byte", "B");
map.put("double", "D");
map.put("char", "C");
abbreviationMap = Collections.unmodifiableMap(map);
reverseAbbreviationMap = Collections.unmodifiableMap(map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)));
}
/**
@ -211,13 +205,13 @@ public class ClassUtils {
return null;
}
final List<Class<?>> classes = new ArrayList<>(classNames.size());
for (final String className : classNames) {
classNames.forEach(className -> {
try {
classes.add(Class.forName(className));
} catch (final Exception ex) {
classes.add(null);
}
}
});
return classes;
}

View File

@ -175,10 +175,7 @@ public class EnumUtils {
asEnum(enumClass);
Objects.requireNonNull(values, "values");
final EnumSet<E> condensed = EnumSet.noneOf(enumClass);
for (final E constant : values) {
Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED);
condensed.add(constant);
}
values.forEach(constant -> condensed.add(Validate.notNull(constant, NULL_ELEMENTS_NOT_PERMITTED)));
final long[] result = new long[(enumClass.getEnumConstants().length - 1) / Long.SIZE + 1];
for (final E value : condensed) {
result[value.ordinal() / Long.SIZE] |= 1L << (value.ordinal() % Long.SIZE);

View File

@ -365,9 +365,7 @@ public class ThreadUtils {
final Collection<Thread> result = new ArrayList<>();
final NamePredicate threadNamePredicate = new NamePredicate(threadName);
for (final ThreadGroup group : threadGroups) {
result.addAll(findThreads(group, false, threadNamePredicate));
}
threadGroups.forEach(group -> result.addAll(findThreads(group, false, threadNamePredicate)));
return Collections.unmodifiableCollection(result);
}

View File

@ -22,6 +22,7 @@ import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
@ -247,14 +248,7 @@ public class FieldUtils {
*/
public static List<Field> getFieldsListWithAnnotation(final Class<?> cls, final Class<? extends Annotation> annotationCls) {
Validate.notNull(annotationCls, "annotationCls");
final List<Field> allFields = getAllFieldsList(cls);
final List<Field> annotatedFields = new ArrayList<>();
for (final Field field : allFields) {
if (field.getAnnotation(annotationCls) != null) {
annotatedFields.add(field);
}
}
return annotatedFields;
return getAllFieldsList(cls).stream().filter(field -> field.getAnnotation(annotationCls) != null).collect(Collectors.toList());
}
/**

View File

@ -924,10 +924,10 @@ public class MethodUtils {
final List<Class<?>> classes = searchSupers ? getAllSuperclassesAndInterfaces(cls) : new ArrayList<>();
classes.add(0, cls);
final List<Method> annotatedMethods = new ArrayList<>();
for (final Class<?> acls : classes) {
classes.forEach(acls -> {
final Method[] methods = ignoreAccess ? acls.getDeclaredMethods() : acls.getMethods();
Stream.of(methods).filter(method -> method.isAnnotationPresent(annotationCls)).forEachOrdered(annotatedMethods::add);
}
});
return annotatedMethods;
}

View File

@ -523,11 +523,6 @@ public class ExtendedMessageFormat extends MessageFormat {
if (coll == null || coll.isEmpty()) {
return false;
}
for (final Object name : coll) {
if (name != null) {
return true;
}
}
return false;
return coll.stream().anyMatch(Objects::nonNull);
}
}

View File

@ -1222,9 +1222,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build
*/
public StrBuilder appendAll(final Iterable<?> iterable) {
if (iterable != null) {
for (final Object o : iterable) {
append(o);
}
iterable.forEach(this::append);
}
return this;
}
@ -1240,9 +1238,7 @@ public class StrBuilder implements CharSequence, Appendable, Serializable, Build
*/
public StrBuilder appendAll(final Iterator<?> it) {
if (it != null) {
while (it.hasNext()) {
append(it.next());
}
it.forEachRemaining(this::append);
}
return this;
}

View File

@ -486,20 +486,18 @@ public class FastDateParser implements DateParser, Serializable {
* @return The map of string display names to field values
*/
private static Map<String, Integer> appendDisplayNames(final Calendar calendar, Locale locale, final int field,
final StringBuilder regex) {
final StringBuilder regex) {
final Map<String, Integer> values = new HashMap<>();
locale = LocaleUtils.toLocale(locale);
final Map<String, Integer> displayNames = calendar.getDisplayNames(field, Calendar.ALL_STYLES, locale);
final Locale actualLocale = LocaleUtils.toLocale(locale);
final Map<String, Integer> displayNames = calendar.getDisplayNames(field, Calendar.ALL_STYLES, actualLocale);
final TreeSet<String> sorted = new TreeSet<>(LONGER_FIRST_LOWERCASE);
for (final Map.Entry<String, Integer> displayName : displayNames.entrySet()) {
final String key = displayName.getKey().toLowerCase(locale);
if (sorted.add(key)) {
values.put(key, displayName.getValue());
displayNames.forEach((k, v) -> {
final String keyLc = k.toLowerCase(actualLocale);
if (sorted.add(keyLc)) {
values.put(keyLc, v);
}
}
for (final String symbol : sorted) {
simpleQuote(regex, symbol).append('|');
}
});
sorted.forEach(symbol -> simpleQuote(regex, symbol).append('|'));
return values;
}
@ -953,9 +951,7 @@ public class FastDateParser implements DateParser, Serializable {
}
// order the regex alternatives with longer strings first, greedy
// match will ensure the longest string will be consumed
for (final String zoneName : sorted) {
simpleQuote(sb.append('|'), zoneName);
}
sorted.forEach(zoneName -> simpleQuote(sb.append('|'), zoneName));
sb.append(")");
createPattern(sb);
}