mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-09 19:45:01 +00:00
Lambdas.
This commit is contained in:
parent
8c1153d371
commit
36c4cc67a1
@ -3663,7 +3663,7 @@ public static boolean isSorted(final short[] array) {
|
|||||||
* @since 3.4
|
* @since 3.4
|
||||||
*/
|
*/
|
||||||
public static <T extends Comparable<? super T>> boolean isSorted(final T[] array) {
|
public static <T extends Comparable<? super T>> boolean isSorted(final T[] array) {
|
||||||
return isSorted(array, (o1, o2) -> o1.compareTo(o2));
|
return isSorted(array, Comparable::compareTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -445,7 +445,7 @@ public ArrayCollector(final Class<O> elementType) {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Supplier<List<O>> supplier() {
|
public Supplier<List<O>> supplier() {
|
||||||
return () -> new ArrayList<>();
|
return ArrayList::new;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -465,7 +465,7 @@ public BinaryOperator<List<O>> combiner() {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Function<List<O>, O[]> finisher() {
|
public Function<List<O>, O[]> finisher() {
|
||||||
return (list) -> {
|
return list -> {
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
final O[] array = (O[]) Array.newInstance(elementType, list.size());
|
final O[] array = (O[]) Array.newInstance(elementType, list.size());
|
||||||
return list.toArray(array);
|
return list.toArray(array);
|
||||||
|
@ -152,7 +152,7 @@ void testRunnable() {
|
|||||||
@Test
|
@Test
|
||||||
void testAsRunnable() {
|
void testAsRunnable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
final Runnable runnable = Functions.asRunnable(() -> new FailureOnOddInvocations());
|
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);
|
||||||
@ -178,9 +178,7 @@ void testCallable() {
|
|||||||
@Test
|
@Test
|
||||||
void testAsCallable() {
|
void testAsCallable() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = () -> {
|
final FailableCallable<FailureOnOddInvocations, SomeException> failableCallable = () -> new FailureOnOddInvocations();
|
||||||
return new FailureOnOddInvocations();
|
|
||||||
};
|
|
||||||
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();
|
||||||
@ -223,7 +221,7 @@ void testAcceptConsumer() {
|
|||||||
void testAsConsumer() {
|
void testAsConsumer() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
final Consumer<Testable> consumer = Functions.asConsumer((t) -> t.test());
|
final Consumer<Testable> consumer = Functions.asConsumer(t -> t.test());
|
||||||
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable));
|
Throwable e = assertThrows(IllegalStateException.class, () -> consumer.accept(testable));
|
||||||
assertSame(ise, e);
|
assertSame(ise, e);
|
||||||
|
|
||||||
@ -319,7 +317,7 @@ public void testApplyFunction() {
|
|||||||
public void testAsFunction() {
|
public void testAsFunction() {
|
||||||
final IllegalStateException ise = new IllegalStateException();
|
final IllegalStateException ise = new IllegalStateException();
|
||||||
final Testable testable = new Testable(ise);
|
final Testable testable = new Testable(ise);
|
||||||
final FailableFunction<Throwable, Integer, Throwable> failableFunction = (th) -> {
|
final FailableFunction<Throwable, Integer, Throwable> failableFunction = th -> {
|
||||||
testable.setThrowable(th);
|
testable.setThrowable(th);
|
||||||
return Integer.valueOf(testable.testInt());
|
return Integer.valueOf(testable.testInt());
|
||||||
};
|
};
|
||||||
@ -407,7 +405,7 @@ public void testGetFromSupplier() {
|
|||||||
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
@DisplayName("Test that asPredicate(FailablePredicate) is converted to -> Predicate ")
|
||||||
public void testAsPredicate() {
|
public void testAsPredicate() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
final Functions.FailablePredicate<Object, Throwable> failablePredicate = (t) -> FailureOnOddInvocations.failingBool();
|
final Functions.FailablePredicate<Object, Throwable> failablePredicate = t -> FailureOnOddInvocations.failingBool();
|
||||||
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
|
final Predicate<?> predicate = Functions.asPredicate(failablePredicate);
|
||||||
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
|
final UndeclaredThrowableException e = assertThrows(UndeclaredThrowableException.class, () -> predicate.test(null));
|
||||||
final Throwable cause = e.getCause();
|
final Throwable cause = e.getCause();
|
||||||
@ -436,7 +434,7 @@ public void testAsBiPredicate() {
|
|||||||
@Test
|
@Test
|
||||||
public void testAsSupplier() {
|
public void testAsSupplier() {
|
||||||
FailureOnOddInvocations.invocation = 0;
|
FailureOnOddInvocations.invocation = 0;
|
||||||
final FailableSupplier<FailureOnOddInvocations, Throwable> failableSupplier = () -> new FailureOnOddInvocations();
|
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();
|
||||||
|
@ -36,7 +36,7 @@ class StreamsTest {
|
|||||||
@Test
|
@Test
|
||||||
void testSimpleStreamMap() {
|
void testSimpleStreamMap() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList());
|
final List<Integer> output = Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
|
||||||
assertEquals(6, output.size());
|
assertEquals(6, output.size());
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
assertEquals(i+1, output.get(i).intValue());
|
assertEquals(i+1, output.get(i).intValue());
|
||||||
@ -47,7 +47,7 @@ void testSimpleStreamMap() {
|
|||||||
void testSimpleStreamMapFailing() {
|
void testSimpleStreamMapFailing() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4 ", "5", "6");
|
||||||
try {
|
try {
|
||||||
Functions.stream(input).map((s) -> Integer.valueOf(s)).collect(Collectors.toList());
|
Functions.stream(input).map(s -> Integer.valueOf(s)).collect(Collectors.toList());
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
} catch (final NumberFormatException nfe) {
|
} catch (final NumberFormatException nfe) {
|
||||||
assertEquals("For input string: \"4 \"", nfe.getMessage());
|
assertEquals("For input string: \"4 \"", nfe.getMessage());
|
||||||
@ -58,7 +58,7 @@ void testSimpleStreamMapFailing() {
|
|||||||
void testSimpleStreamForEach() {
|
void testSimpleStreamForEach() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = new ArrayList<>();
|
final List<Integer> output = new ArrayList<>();
|
||||||
Functions.stream(input).forEach((s) -> output.add(Integer.valueOf(s)));
|
Functions.stream(input).forEach(s -> output.add(Integer.valueOf(s)));
|
||||||
assertEquals(6, output.size());
|
assertEquals(6, output.size());
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
assertEquals(i+1, output.get(i).intValue());
|
assertEquals(i+1, output.get(i).intValue());
|
||||||
@ -76,7 +76,7 @@ void testToArray() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
|
protected <T extends Throwable> FailableConsumer<String, T> asIntConsumer(final T pThrowable) {
|
||||||
return (s) -> {
|
return s -> {
|
||||||
final Integer i = Integer.valueOf(s);
|
final Integer i = Integer.valueOf(s);
|
||||||
if (i.intValue() == 4) {
|
if (i.intValue() == 4) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
@ -117,8 +117,8 @@ void testSimpleStreamForEachFailing() {
|
|||||||
void testSimpleStreamFilter() {
|
void testSimpleStreamFilter() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input)
|
final List<Integer> output = Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map(s -> Integer.valueOf(s))
|
||||||
.filter((i) -> {
|
.filter(i -> {
|
||||||
return i.intValue() %2 == 0;
|
return i.intValue() %2 == 0;
|
||||||
})
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
@ -133,7 +133,7 @@ private void assertEvenNumbers(final List<Integer> output) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(final T pThrowable) {
|
protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(final T pThrowable) {
|
||||||
return (i) -> {
|
return i -> {
|
||||||
if (i.intValue() == 5) {
|
if (i.intValue() == 5) {
|
||||||
if (pThrowable != null) {
|
if (pThrowable != null) {
|
||||||
throw pThrowable;
|
throw pThrowable;
|
||||||
@ -147,7 +147,7 @@ protected <T extends Throwable> FailablePredicate<Integer, T> asIntPredicate(fin
|
|||||||
void testSimpleStreamFilterFailing() {
|
void testSimpleStreamFilterFailing() {
|
||||||
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
final List<String> input = Arrays.asList("1", "2", "3", "4", "5", "6");
|
||||||
final List<Integer> output = Functions.stream(input)
|
final List<Integer> output = Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map(s -> Integer.valueOf(s))
|
||||||
.filter(asIntPredicate(null))
|
.filter(asIntPredicate(null))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
assertEvenNumbers(output);
|
assertEvenNumbers(output);
|
||||||
@ -156,7 +156,7 @@ void testSimpleStreamFilterFailing() {
|
|||||||
final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
|
final IllegalArgumentException iae = new IllegalArgumentException("Invalid argument: " + 5);
|
||||||
try {
|
try {
|
||||||
Functions.stream(input)
|
Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map(s -> Integer.valueOf(s))
|
||||||
.filter(asIntPredicate(iae))
|
.filter(asIntPredicate(iae))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
@ -168,7 +168,7 @@ void testSimpleStreamFilterFailing() {
|
|||||||
final OutOfMemoryError oome = new OutOfMemoryError();
|
final OutOfMemoryError oome = new OutOfMemoryError();
|
||||||
try {
|
try {
|
||||||
Functions.stream(input)
|
Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map(s -> Integer.valueOf(s))
|
||||||
.filter(asIntPredicate(oome))
|
.filter(asIntPredicate(oome))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
@ -180,7 +180,7 @@ void testSimpleStreamFilterFailing() {
|
|||||||
final SAXException se = new SAXException();
|
final SAXException se = new SAXException();
|
||||||
try {
|
try {
|
||||||
Functions.stream(input)
|
Functions.stream(input)
|
||||||
.map((s) -> Integer.valueOf(s))
|
.map(s -> Integer.valueOf(s))
|
||||||
.filter(asIntPredicate(se))
|
.filter(asIntPredicate(se))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
fail("Expected Exception");
|
fail("Expected Exception");
|
||||||
|
@ -62,7 +62,7 @@ public void testEnforceExceptions() {
|
|||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> IEEE754rUtils.min(),
|
IEEE754rUtils::min,
|
||||||
"IllegalArgumentException expected for empty input");
|
"IllegalArgumentException expected for empty input");
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
@ -82,7 +82,7 @@ public void testEnforceExceptions() {
|
|||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> IEEE754rUtils.min(),
|
IEEE754rUtils::min,
|
||||||
"IllegalArgumentException expected for empty input");
|
"IllegalArgumentException expected for empty input");
|
||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
@ -92,7 +92,7 @@ public void testEnforceExceptions() {
|
|||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> IEEE754rUtils.max(),
|
IEEE754rUtils::max,
|
||||||
"IllegalArgumentException expected for empty input");
|
"IllegalArgumentException expected for empty input");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -691,7 +691,7 @@ public void testMinLong_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinLong_emptyArray() {
|
public void testMinLong_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.min());
|
assertThrows(IllegalArgumentException.class, NumberUtils::min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -710,7 +710,7 @@ public void testMinInt_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinInt_emptyArray() {
|
public void testMinInt_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.min());
|
assertThrows(IllegalArgumentException.class, NumberUtils::min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -729,7 +729,7 @@ public void testMinShort_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinShort_emptyArray() {
|
public void testMinShort_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.min());
|
assertThrows(IllegalArgumentException.class, NumberUtils::min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -767,7 +767,7 @@ public void testMinDouble_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinDouble_emptyArray() {
|
public void testMinDouble_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.min());
|
assertThrows(IllegalArgumentException.class, NumberUtils::min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -786,7 +786,7 @@ public void testMinFloat_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMinFloat_emptyArray() {
|
public void testMinFloat_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.min());
|
assertThrows(IllegalArgumentException.class, NumberUtils::min);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -805,7 +805,7 @@ public void testMaxLong_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxLong_emptyArray() {
|
public void testMaxLong_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.max());
|
assertThrows(IllegalArgumentException.class, NumberUtils::max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -824,7 +824,7 @@ public void testMaxInt_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxInt_emptyArray() {
|
public void testMaxInt_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.max());
|
assertThrows(IllegalArgumentException.class, NumberUtils::max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -843,7 +843,7 @@ public void testMaxShort_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxShort_emptyArray() {
|
public void testMaxShort_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.max());
|
assertThrows(IllegalArgumentException.class, NumberUtils::max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -881,7 +881,7 @@ public void testMaxDouble_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxDouble_emptyArray() {
|
public void testMaxDouble_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.max());
|
assertThrows(IllegalArgumentException.class, NumberUtils::max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -892,7 +892,7 @@ public void testMaxDouble() {
|
|||||||
|
|
||||||
assertThrows(
|
assertThrows(
|
||||||
IllegalArgumentException.class,
|
IllegalArgumentException.class,
|
||||||
() -> NumberUtils.max(),
|
NumberUtils::max,
|
||||||
"No exception was thrown for empty input.");
|
"No exception was thrown for empty input.");
|
||||||
|
|
||||||
assertEquals(5.1f, NumberUtils.max(5.1f), "max(double[]) failed for array length 1");
|
assertEquals(5.1f, NumberUtils.max(5.1f), "max(double[]) failed for array length 1");
|
||||||
@ -909,7 +909,7 @@ public void testMaxFloat_nullArray() {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMaxFloat_emptyArray() {
|
public void testMaxFloat_emptyArray() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> NumberUtils.max());
|
assertThrows(IllegalArgumentException.class, NumberUtils::max);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user