Skip to content

Commit

Permalink
Eat your own dog food for LANG-1634
Browse files Browse the repository at this point in the history
Update a few existing classes to use ObjectUtils#acceptIfNonNull(Object,
Consumer)
  • Loading branch information
bindul committed Jan 7, 2021
1 parent 0122581 commit 1813f18
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
20 changes: 5 additions & 15 deletions src/main/java/org/apache/commons/lang3/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4391,16 +4391,12 @@ public static String join(final Iterator<?> iterator, final char separator) {

// two or more elements
final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
ObjectUtils.acceptIfNonNull(first, buf::append);

while (iterator.hasNext()) {
buf.append(separator);
final Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
ObjectUtils.acceptIfNonNull(obj, buf::append);
}

return buf.toString();
Expand Down Expand Up @@ -4435,18 +4431,12 @@ public static String join(final Iterator<?> iterator, final String separator) {

// two or more elements
final StringBuilder buf = new StringBuilder(STRING_BUILDER_SIZE); // Java default is 16, probably too small
if (first != null) {
buf.append(first);
}
ObjectUtils.acceptIfNonNull(first, buf::append);

while (iterator.hasNext()) {
if (separator != null) {
buf.append(separator);
}
ObjectUtils.acceptIfNonNull(separator, buf::append);
final Object obj = iterator.next();
if (obj != null) {
buf.append(obj);
}
ObjectUtils.acceptIfNonNull(obj, buf::append);
}
return buf.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;

/**
Expand Down Expand Up @@ -413,9 +414,7 @@ static String[] toNoNullStringArray(final Collection<String> collection) {
static String[] toNoNullStringArray(final Object[] array) {
final List<String> list = new ArrayList<>(array.length);
for (final Object e : array) {
if (e != null) {
list.add(e.toString());
}
ObjectUtils.acceptIfNonNull(e, o -> list.add(e.toString()));
}
return list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;

/**
Expand Down Expand Up @@ -224,17 +225,11 @@ private void initializeThread(final Thread thread) {
thread.setName(String.format(getNamingPattern(), count));
}

if (getUncaughtExceptionHandler() != null) {
thread.setUncaughtExceptionHandler(getUncaughtExceptionHandler());
}
ObjectUtils.acceptIfNonNull(getUncaughtExceptionHandler(), thread::setUncaughtExceptionHandler);

if (getPriority() != null) {
thread.setPriority(getPriority().intValue());
}
ObjectUtils.acceptIfNonNull(getPriority(), p -> thread.setPriority(p.intValue()));

if (getDaemonFlag() != null) {
thread.setDaemon(getDaemonFlag().booleanValue());
}
ObjectUtils.acceptIfNonNull(getDaemonFlag(), d -> thread.setDaemon(d.booleanValue()));
}

/**
Expand Down

0 comments on commit 1813f18

Please sign in to comment.