[LANG-1556] Use Java 8 lambdas and Map operations. (#541)

* java8

* refine

* rebase
This commit is contained in:
XenoAmess 2020-06-14 06:33:29 +08:00 committed by GitHub
parent 78c1c7f9ab
commit 797d550a92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 13 additions and 19 deletions

View File

@ -450,9 +450,7 @@ public class Streams {
@Override @Override
public BiConsumer<List<O>, O> accumulator() { public BiConsumer<List<O>, O> accumulator() {
return (list, o) -> { return List::add;
list.add(o);
};
} }
@Override @Override

View File

@ -417,7 +417,7 @@ public class TimedSemaphore {
* @return a future object representing the task scheduled * @return a future object representing the task scheduled
*/ */
protected ScheduledFuture<?> startTimer() { protected ScheduledFuture<?> startTimer() {
return getExecutorService().scheduleAtFixedRate(() -> endOfPeriod(), getPeriod(), getPeriod(), getUnit()); return getExecutorService().scheduleAtFixedRate(this::endOfPeriod, getPeriod(), getPeriod(), getUnit());
} }
/** /**

View File

@ -19,7 +19,6 @@ package org.apache.commons.lang3.exception;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -60,12 +59,7 @@ public class DefaultExceptionContext implements ExceptionContext, Serializable {
*/ */
@Override @Override
public DefaultExceptionContext setContextValue(final String label, final Object value) { public DefaultExceptionContext setContextValue(final String label, final Object value) {
for (final Iterator<Pair<String, Object>> iter = contextValues.iterator(); iter.hasNext();) { contextValues.removeIf(p -> StringUtils.equals(label, p.getKey()));
final Pair<String, Object> p = iter.next();
if (StringUtils.equals(label, p.getKey())) {
iter.remove();
}
}
addContextValue(label, value); addContextValue(label, value);
return this; return this;
} }

View File

@ -60,7 +60,7 @@ import org.apache.commons.lang3.Validate;
*/ */
public class MethodUtils { public class MethodUtils {
private static final Comparator<Method> METHOD_BY_SIGNATURE = (m1, m2) -> m1.toString().compareTo(m2.toString()); private static final Comparator<Method> METHOD_BY_SIGNATURE = Comparator.comparing(Method::toString);
/** /**
* <p>{@link MethodUtils} instances should NOT be constructed in standard programming. * <p>{@link MethodUtils} instances should NOT be constructed in standard programming.

View File

@ -875,8 +875,10 @@ public class TypeUtils {
// map the arguments to their respective type variables // map the arguments to their respective type variables
for (int i = 0; i < typeParams.length; i++) { for (int i = 0; i < typeParams.length; i++) {
final Type typeArg = typeArgs[i]; final Type typeArg = typeArgs[i];
typeVarAssigns.put(typeParams[i], typeVarAssigns.containsKey(typeArg) ? typeVarAssigns typeVarAssigns.put(
.get(typeArg) : typeArg); typeParams[i],
typeVarAssigns.getOrDefault(typeArg, typeArg)
);
} }
if (toClass.equals(cls)) { if (toClass.equals(cls)) {

View File

@ -94,7 +94,7 @@ public class FastDateParser implements DateParser, Serializable {
// comparator used to sort regex alternatives // comparator used to sort regex alternatives
// alternatives should be ordered longer first, and shorter last. ('february' before 'feb') // alternatives should be ordered longer first, and shorter last. ('february' before 'feb')
// all entries must be lowercase by locale. // all entries must be lowercase by locale.
private static final Comparator<String> LONGER_FIRST_LOWERCASE = (left, right) -> right.compareTo(left); private static final Comparator<String> LONGER_FIRST_LOWERCASE = Comparator.reverseOrder();
/** /**
* <p>Constructs a new FastDateParser.</p> * <p>Constructs a new FastDateParser.</p>

View File

@ -1499,7 +1499,7 @@ public class ArrayUtilsTest {
@Test @Test
public void testIsSortedComparator() { public void testIsSortedComparator() {
final Comparator<Integer> c = (o1, o2) -> o2.compareTo(o1); final Comparator<Integer> c = Comparator.reverseOrder();
Integer[] array = null; Integer[] array = null;
assertTrue(ArrayUtils.isSorted(array, c)); assertTrue(ArrayUtils.isSorted(array, c));

View File

@ -579,7 +579,7 @@ class FunctionsTest {
FailureOnOddInvocations.invocations = 0; FailureOnOddInvocations.invocations = 0;
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new; final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = FailureOnOddInvocations::new;
final Callable<FailureOnOddInvocations> callable = Functions.asCallable(failableCallable); final Callable<FailureOnOddInvocations> callable = Functions.asCallable(failableCallable);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> callable.call()); final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, callable::call);
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
assertNotNull(cause); assertNotNull(cause);
assertTrue(cause instanceof SomeException); assertTrue(cause instanceof SomeException);
@ -663,7 +663,7 @@ class FunctionsTest {
void testAsRunnable() { void testAsRunnable() {
FailureOnOddInvocations.invocations = 0; FailureOnOddInvocations.invocations = 0;
final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new); final Runnable runnable = Functions.asRunnable(FailureOnOddInvocations::new);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> runnable.run()); final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, runnable::run);
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
assertNotNull(cause); assertNotNull(cause);
assertTrue(cause instanceof SomeException); assertTrue(cause instanceof SomeException);
@ -678,7 +678,7 @@ class FunctionsTest {
FailureOnOddInvocations.invocations = 0; FailureOnOddInvocations.invocations = 0;
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new; final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = FailureOnOddInvocations::new;
final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier); final Supplier<FailureOnOddInvocations> supplier = Functions.asSupplier(failableSupplier);
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> supplier.get()); final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, supplier::get);
final Throwable cause = e.getCause(); final Throwable cause = e.getCause();
assertNotNull(cause); assertNotNull(cause);
assertTrue(cause instanceof SomeException); assertTrue(cause instanceof SomeException);