Source code

    001/*
    002# Licensed Materials - Property of IBM
    003# Copyright IBM Corp. 2015
    004 */
    005package vwap;
    006
    007import java.util.List;
    008
    009import com.ibm.streamsx.topology.TStream;
    010import com.ibm.streamsx.topology.TWindow;
    011import com.ibm.streamsx.topology.function.BiFunction;
    012import com.ibm.streamsx.topology.function.Function;
    013
    014public class VwapProcessing {
    015
    016    @SuppressWarnings("serial")
    017    public static TStream<Bargain> bargains(TStream<Trade> trades,
    018            TStream<Quote> quotes) {
    019
    020        final Function<Ticker,String> tickerKey = Ticker::getTicker;
    021
    022        TWindow<Trade,String> tradesWindow = trades.last(4).key(tickerKey);
    023
    024        TStream<VWapT> vwap = tradesWindow.aggregate(
    025                new Function<List<Trade>, VWapT>() {
    026
    027                    @Override
    028                    public VWapT apply(List<Trade> tuples) {
    029                        VWapT vwap = null;
    030                        for (Trade trade : tuples) {
    031                            if (vwap == null)
    032                                vwap = new VWapT(trade);
    033                            vwap.newTrade(trade);
    034                        }
    035                        return vwap == null ? null : vwap.complete();
    036                    }
    037                });
    038
    039        TStream<Bargain> bargainIndex = quotes.joinLast(
    040                tickerKey,
    041                vwap,
    042                tickerKey,
    043                new BiFunction<Quote, VWapT, Bargain>() {
    044
    045                    @Override
    046                    public Bargain apply(Quote v1, VWapT v2) {
    047                        if (v2 == null) // window is empty!
    048                            return null;
    049                        return new Bargain(v1, v2);
    050                    }
    051                });
    052
    053        return bargainIndex;
    054    }
    055}