mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-06 10:08:32 +00:00
Flip args on new API
This commit is contained in:
parent
a37e8de046
commit
4c1cbdbb80
@ -34,13 +34,13 @@ public class Consumers {
|
||||
/**
|
||||
* Applies the given {@link Consumer} action to the object if the consumer is not {@code null}. Otherwise, does
|
||||
* nothing.
|
||||
*
|
||||
* @param object the object to be consumed.
|
||||
* @param consumer the consumer to consume.
|
||||
* @param object the object to be consumed.
|
||||
*
|
||||
* @param <T> the type of the argument the consumer accepts.
|
||||
* @since 3.15.0
|
||||
*/
|
||||
public static <T> void accept(final T object, final Consumer<T> consumer) {
|
||||
public static <T> void accept(final Consumer<T> consumer, final T object) {
|
||||
if (consumer != null) {
|
||||
consumer.accept(object);
|
||||
}
|
||||
|
@ -29,15 +29,15 @@ public final class Functions {
|
||||
/**
|
||||
* Applies the {@link Function} on the object if the function is not {@code null}. Otherwise, does nothing and
|
||||
* returns {@code null}.
|
||||
*
|
||||
* @param object the object to apply the function.
|
||||
* @param function the function to apply.
|
||||
* @param object the object to apply the function.
|
||||
*
|
||||
* @param <T> the type of the argument the function applies.
|
||||
* @param <R> the type of the result the function returns.
|
||||
* @return the value the function returns if the function is not {@code null}; {@code null} otherwise.
|
||||
* @since 3.15.0
|
||||
*/
|
||||
public static <T, R> R apply(final T object, final Function<T, R> function) {
|
||||
public static <T, R> R apply(final Function<T, R> function, final T object) {
|
||||
return function != null ? function.apply(object) : null;
|
||||
}
|
||||
|
||||
|
@ -41,20 +41,20 @@ public void accept(T t) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Consumers#accept(Object, Consumer)}.
|
||||
* Tests {@link Consumers#accept(Consumer, Object)}.
|
||||
*/
|
||||
@Test
|
||||
public void testAccept() {
|
||||
final StringBuilder builder = new StringBuilder("foo");
|
||||
Consumers.accept(builder, sb -> sb.append("-bar"));
|
||||
Consumers.accept(sb -> sb.append("-bar"), builder);
|
||||
assertEquals("foo-bar", builder.toString());
|
||||
|
||||
final TestConsumer<String> consumer = new TestConsumer<>();
|
||||
Consumers.accept(null, consumer);
|
||||
Consumers.accept(consumer, null);
|
||||
assertTrue(consumer.isCalled);
|
||||
|
||||
final StringBuilder builder2 = new StringBuilder("foo");
|
||||
Consumers.accept(builder2, null);
|
||||
Consumers.accept(null, builder2);
|
||||
assertEquals("foo", builder2.toString());
|
||||
}
|
||||
|
||||
|
@ -30,13 +30,13 @@
|
||||
public class FunctionsTest {
|
||||
|
||||
/**
|
||||
* Tests {@link Functions#apply(Object, Function)}.
|
||||
* Tests {@link Functions#apply(Function, Object)}.
|
||||
*/
|
||||
@Test
|
||||
public void testApply() {
|
||||
assertEquals("foo-bar", Functions.apply("foo", string -> string.concat("-bar")));
|
||||
assertEquals("foo-bar", Functions.apply(null, object -> "foo-bar"));
|
||||
assertNull(Functions.apply("foo", null));
|
||||
assertEquals("foo-bar", Functions.apply(string -> string.concat("-bar"), "foo"));
|
||||
assertEquals("foo-bar", Functions.apply(object -> "foo-bar", null));
|
||||
assertNull(Functions.apply(null, "foo"));
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user