001/*
002# Licensed Materials - Property of IBM
003# Copyright IBM Corp. 2015  
004 */
005package vwap;
006
007import java.math.BigDecimal;
008
009import com.ibm.streams.operator.Tuple;
010import com.ibm.streamsx.topology.TStream;
011import com.ibm.streamsx.topology.spl.SPLStream;
012
013public class Trade extends Ticker {
014    
015    /**
016     * 
017     */
018    private static final long serialVersionUID = -5415277911844071545L;
019
020    static BigDecimal getNumber(String v) {
021        if (v.isEmpty())
022            return BigDecimal.ZERO;
023        return new BigDecimal(v);
024    }
025
026    BigDecimal price;
027    BigDecimal volume;
028
029    public Trade(String ticker, BigDecimal price, BigDecimal volume) {
030        super(ticker);
031        this.price = price;
032        this.volume = volume;
033    }
034
035    public Trade(Tuple tuple) {
036        super(tuple);
037        price = tuple.getBigDecimal("price");
038        volume = tuple.getBigDecimal("volume");
039    }
040
041    public String toString() {
042        return "TRADE: " + getTicker() + " price=" + price + " volume="
043                + volume;
044    }
045    
046    /**
047     * Convert a trade SPL tuple to a Trade object.
048     */
049    public static Trade convertToTrade(Tuple tuple) {
050        if ("Trade".equals(tuple.getString("ttype")))
051            return new Trade(tuple);
052        return null;
053
054    }
055
056    /**
057     * Get the stream of trades from the SPL stream.
058     * The stream is keyed by the ticker symbol.
059     */
060    public static TStream<Trade> getTrades(SPLStream tradeQuotes) {
061        return tradeQuotes.transform(Trade::convertToTrade);
062    }
063}