jOOL icon indicating copy to clipboard operation
jOOL copied to clipboard

Add Seq.fullOuterJoin()

Open lukaseder opened this issue 9 years ago • 2 comments

lukaseder avatar Dec 14 '16 21:12 lukaseder

Below is a rough example of a workaround:


import static java.util.Optional.*;
import static org.jooq.lambda.Seq.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.jooq.lambda.Seq;
import org.jooq.lambda.tuple.Tuple2;
import org.junit.Test;

import lombok.Value;

public class JoolTest {

    @Test
    public void matchPositions() {

        List<Position> client1Positions = Stream.of(Position.of("ticker1", "client1", 0.0),
                Position.of("ticker2", "client1", 0.0), Position.of("ticker4", "client1", 0.0))
            .collect(Collectors.toList());

        List<Position> client2Positions = Stream.of(Position.of("ticker1", "client2", 0.0),
                Position.of("ticker3", "client2", 0.0), Position.of("ticker4", "client2", 0.0))
            .collect(Collectors.toList());

        Seq<Tuple2<Position, Position>> leftJoinClient1ToClient2 = seq(client1Positions).leftOuterJoin(client2Positions,
                Position::sameTicker);

        Seq<Tuple2<Position, Position>> rightJoinClient1ToClient2 = seq(client1Positions)
            .rightOuterJoin(client2Positions, Position::sameTicker);

        // Expected Output:
        //
        // (JoolTest.Position(ticker=ticker1, client=client1, notional=0.0), JoolTest.Position(ticker=ticker1,
        // client=client2, notional=0.0))
        // (JoolTest.Position(ticker=ticker2, client=client1, notional=0.0), null)
        // (JoolTest.Position(ticker=ticker4, client=client1, notional=0.0), JoolTest.Position(ticker=ticker4,
        // client=client2, notional=0.0))
        // (null, JoolTest.Position(ticker=ticker3, client=client2, notional=0.0))
        leftJoinClient1ToClient2.concat(rightJoinClient1ToClient2)
            .distinct()
            .stream()
            .forEach(System.out::println);

    }

    @Value(staticConstructor = "of")
    static class Position {

        String ticker;

        String client;

        double notional;

        public boolean sameTicker(Position that) {

            return ofNullable(that).filter(t -> getTicker().equals(t.getTicker()))
                .isPresent();

        }
    }

jmax01 avatar Dec 14 '16 22:12 jmax01

Thanks, @jmax01

lukaseder avatar Dec 18 '16 19:12 lukaseder