Commit 3eea4d7f authored by Simeon Anfinrud's avatar Simeon Anfinrud Committed by Commit Bot

[Chromecast] Increase versatility of Both.adapt().

Now it's possible to e.g. create a
Function<Both<ADerived, BDerived>, CBase> from a
BiFunction<ABase, BBase, CDerived> if:

* ADerived extends ABase
* BDerived extends BBase
* CDerived extends CBase

This will help with some refactors.

Bug: None
Test: cast_base_junit_tests
Change-Id: I7cfa3b3cfb559510e318ac8a597a138c0e4b3e0e
Reviewed-on: https://chromium-review.googlesource.com/1119056Reviewed-by: default avatarLuke Halliwell <halliwell@chromium.org>
Commit-Queue: Simeon Anfinrud <sanfin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#572286}
parent 39ff0c71
...@@ -57,21 +57,22 @@ public class Both<A, B> { ...@@ -57,21 +57,22 @@ public class Both<A, B> {
/** /**
* Turns a function of two arguments into a function of a single Both argument. * Turns a function of two arguments into a function of a single Both argument.
*/ */
public static <A, B, R> Function<Both<A, B>, R> adapt(BiFunction<A, B, R> function) { public static <A, B, R> Function<Both<A, B>, R> adapt(
BiFunction<? super A, ? super B, ? extends R> function) {
return (Both<A, B> data) -> function.apply(data.first, data.second); return (Both<A, B> data) -> function.apply(data.first, data.second);
} }
/** /**
* Turns a consumer of two arguments into a consumer of a single Both argument. * Turns a consumer of two arguments into a consumer of a single Both argument.
*/ */
public static <A, B> Consumer<Both<A, B>> adapt(BiConsumer<A, B> consumer) { public static <A, B> Consumer<Both<A, B>> adapt(BiConsumer<? super A, ? super B> consumer) {
return (Both<A, B> data) -> consumer.accept(data.first, data.second); return (Both<A, B> data) -> consumer.accept(data.first, data.second);
} }
/** /**
* Turns a predicate of two arguments into a predicate of a single Both argument. * Turns a predicate of two arguments into a predicate of a single Both argument.
*/ */
public static <A, B> Predicate<Both<A, B>> adapt(BiPredicate<A, B> predicate) { public static <A, B> Predicate<Both<A, B>> adapt(BiPredicate<? super A, ? super B> predicate) {
return (Both<A, B> data) -> predicate.test(data.first, data.second); return (Both<A, B> data) -> predicate.test(data.first, data.second);
} }
} }
...@@ -14,6 +14,9 @@ import org.junit.Test; ...@@ -14,6 +14,9 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner; import org.junit.runners.BlockJUnit4ClassRunner;
import org.chromium.chromecast.base.Inheritance.Base;
import org.chromium.chromecast.base.Inheritance.Derived;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -66,6 +69,21 @@ public class BothTest { ...@@ -66,6 +69,21 @@ public class BothTest {
assertEquals(result, "ab"); assertEquals(result, "ab");
} }
@Test
public void testAdaptBiFunctionBaseArguments() {
// Compile error if generics are wrong.
Function<Both<Derived, Derived>, String> f = Both.adapt((Base a, Base b) -> "success");
assertEquals(f.apply(Both.both(new Derived(), new Derived())), "success");
}
@Test
public void testAdaptBiFunctionDerivedResult() {
// Compile error if generics are wrong.
Derived derived = new Derived();
Function<Both<String, String>, Base> f = Both.adapt((String a, String b) -> derived);
assertEquals(f.apply(Both.both("a", "b")), derived);
}
@Test @Test
public void testAdaptBiConsumer() { public void testAdaptBiConsumer() {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
...@@ -73,10 +91,32 @@ public class BothTest { ...@@ -73,10 +91,32 @@ public class BothTest {
assertThat(result, contains("AB")); assertThat(result, contains("AB"));
} }
@Test
public void testAdaptBiConsumerBaseArguments() {
// Compile error if generics are wrong.
List<String> result = new ArrayList<>();
Consumer<Both<Derived, Derived>> c =
Both.adapt((Base a, Base b) -> { result.add("success"); });
c.accept(Both.both(new Derived(), new Derived()));
assertThat(result, contains("success"));
}
@Test @Test
public void testAdaptBiPredicate() { public void testAdaptBiPredicate() {
Predicate<Both<String, String>> p = Both.adapt(String::equals); Predicate<Both<String, String>> p = Both.adapt(String::equals);
assertTrue(p.test(Both.both("a", "a"))); assertTrue(p.test(Both.both("a", "a")));
assertFalse(p.test(Both.both("a", "b"))); assertFalse(p.test(Both.both("a", "b")));
} }
@Test
public void testAdaptBiPredicateBaseArguments() {
// Compile error if generics are wrong.
Predicate<Both<Derived, Derived>> p = Both.adapt((Base a, Base b) -> a.equals(b));
Derived derived1 = new Derived();
Derived derived2 = new Derived();
assertTrue(p.test(Both.both(derived1, derived1)));
assertTrue(p.test(Both.both(derived2, derived2)));
assertFalse(p.test(Both.both(derived1, derived2)));
assertFalse(p.test(Both.both(derived2, derived1)));
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment