Skip to content

Commit

Permalink
[#101] Implemented rightOuterJoin
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaseder committed Aug 9, 2015
1 parent 8e5f82c commit effad83
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/main/java/org/jooq/lambda/Seq.java
Expand Up @@ -135,6 +135,20 @@ default <U> Seq<Tuple2<T, U>> leftOuterJoin(Stream<U> other, BiPredicate<T, U> p
.map(u -> tuple(t, u)));
}

/**
* Right outer join 2 streams into one.
* <p>
* <code><pre>
* // (tuple(1, 1), tuple(2, 2), tuple(null, 3))
* Seq.of(1, 2).rightOuterJoin(Seq.of(1, 2, 3), t -> Objects.equals(t.v1, t.v2))
* </pre></code>
*/
default <U> Seq<Tuple2<T, U>> rightOuterJoin(Stream<U> other, BiPredicate<T, U> predicate) {
return seq(other)
.leftOuterJoin(this, (u, t) -> predicate.test(t, u))
.map(t -> tuple(t.v2, t.v1));
}

/**
* Produce this stream, or an alternative stream from the
* <code>value</code>, in case this stream is empty.
Expand Down
38 changes: 37 additions & 1 deletion src/test/java/org/jooq/lambda/SeqTest.java
Expand Up @@ -458,7 +458,7 @@ public void testInnerJoin() {
}

@Test
public void testLeftJoin() {
public void testLeftOuterJoin() {
BiPredicate<Object, Object> TRUE = (t, u) -> true;

assertEquals(asList(),
Expand Down Expand Up @@ -489,6 +489,42 @@ public void testLeftJoin() {
Seq.<Object>of(1).leftOuterJoin(Seq.of(1, 2), TRUE).toList());
}

@Test
public void testRightOuterJoin() {
BiPredicate<Object, Object> TRUE = (t, u) -> true;

assertEquals(asList(),
Seq.of().rightOuterJoin(Seq.of(), TRUE).toList());
assertEquals(asList(
tuple(null, 1)),
Seq.of().rightOuterJoin(Seq.of(1), TRUE).toList());
assertEquals(asList(
tuple(null, 1),
tuple(null, 2)),
Seq.of().rightOuterJoin(Seq.of(1, 2), TRUE).toList());

assertEquals(asList(),
Seq.<Object>of(1).rightOuterJoin(Seq.of(), TRUE).toList());
assertEquals(asList(
tuple(null, 2)),
Seq.of(1).rightOuterJoin(Seq.of(2), (t, u) -> t == u).toList());
assertEquals(asList(
tuple(1, 2)),
Seq.of(1).rightOuterJoin(Seq.of(2), (t, u) -> t * 2 == u).toList());
assertEquals(asList(
tuple(1, 1),
tuple(null, 2)),
Seq.of(1).rightOuterJoin(Seq.of(1, 2), (t, u) -> t == u).toList());
assertEquals(asList(
tuple(null, 1),
tuple(1, 2)),
Seq.of(1).rightOuterJoin(Seq.of(1, 2), (t, u) -> t * 2 == u).toList());
assertEquals(asList(
tuple(1, 1),
tuple(1, 2)),
Seq.<Object>of(1).rightOuterJoin(Seq.of(1, 2), TRUE).toList());
}

@Test
public void testOnEmpty() throws X {
assertEquals(asList(1), Seq.of().onEmpty(1).toList());
Expand Down

0 comments on commit effad83

Please sign in to comment.