All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.barchart.feed.ddf.message.provider.BaseMarket Maven / Gradle / Ivy
/**
* Copyright (C) 2011-2012 Barchart, Inc.
*
* All rights reserved. Licensed under the OSI BSD License.
*
* http://www.opensource.org/licenses/bsd-license.php
*/
package com.barchart.feed.ddf.message.provider;
import static com.barchart.feed.ddf.message.provider.CodecHelper.*;
import static com.barchart.util.common.ascii.ASCII.*;
import java.nio.ByteBuffer;
import org.joda.time.DateTimeZone;
import com.barchart.feed.api.model.meta.Exchange;
import com.barchart.feed.api.model.meta.Instrument;
import com.barchart.feed.api.model.meta.id.InstrumentID;
import com.barchart.feed.base.provider.Symbology;
import com.barchart.feed.base.provider.ValueConverter;
import com.barchart.feed.base.values.api.TextValue;
import com.barchart.feed.base.values.api.TimeValue;
import com.barchart.feed.base.values.provider.ValueBuilder;
import com.barchart.feed.ddf.instrument.provider.DDF_FeedInstProvider;
import com.barchart.feed.ddf.message.api.DDF_MarketBase;
import com.barchart.feed.ddf.message.enums.DDF_MessageType;
import com.barchart.feed.ddf.message.enums.DDF_Session;
import com.barchart.feed.ddf.message.enums.DDF_TradeDay;
import com.barchart.feed.ddf.symbol.api.DDF_Symbol;
import com.barchart.feed.ddf.symbol.enums.DDF_Exchange;
import com.barchart.feed.ddf.symbol.enums.DDF_SpreadType;
import com.barchart.feed.ddf.symbol.provider.DDF_SymbolService;
import com.barchart.feed.ddf.symbol.provider.DDF_Symbology;
import com.barchart.feed.ddf.util.ClockDDF;
import com.barchart.feed.ddf.util.enums.DDF_Fraction;
import com.barchart.feed.meta.instrument.DefaultInstrument;
import com.barchart.util.common.ascii.ASCII;
import com.barchart.util.value.api.Fraction;
// TODO: Auto-generated Javadoc
abstract class BaseMarket extends Base implements DDF_MarketBase {
BaseMarket() {
//
}
BaseMarket(final DDF_MessageType messageType) {
setMessageType(messageType);
}
// //////////////////////////////////////
private byte ordExchange = DDF_Exchange.UNKNOWN.ord;
private byte ordFraction = DDF_Fraction.UNKNOWN.ord;
private byte ordTradeDay = DDF_TradeDay.UNKNOWN.ord;
private byte ordSession = DDF_Session.UNKNOWN.ord;
private byte ordSpread = DDF_SpreadType.UNKNOWN.ord;
protected byte delay = DDF_NO_DELAY;
protected byte[][] symbolArray;
// //////////////////////////////////////
// NOTE: invokes resolver
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getInstrument()
*/
@Override
public Instrument getInstrument() {
return DDF_FeedInstProvider.fromMessage(stub);
}
/*
* Lazy eval instrument stub
*/
private final Instrument stub = new DefaultInstrument(InstrumentID.NULL) {
/* GUID is symbol for now */
@Override
public String marketGUID() {
return Symbology.formatSymbol(getId().toString());
}
@Override
public SecurityType securityType() {
return getExchange().kind.asSecType();
}
@Override
public String symbol() {
return Symbology.formatSymbol(getId().toString());
}
@Override
public Exchange exchange() {
return getExchange().asExchange();
}
@Override
public String exchangeCode() {
return new String(new byte[] {
getExchange().code
});
}
@Override
public Fraction displayFraction() {
return ValueConverter.fraction(getFraction().fraction);
}
};
@Override
public final TimeValue getTime() {
return ValueBuilder.newTime(millisUTC);
}
// NOTE: invokes parser
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getSymbol()
*/
@Override
public final DDF_Symbol getSymbol() {
return DDF_SymbolService.find(getId());
}
//
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getDelay()
*/
@Override
public final int getDelay() {
return delay;
}
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getFraction()
*/
@Override
public final DDF_Fraction getFraction() {
return DDF_Fraction.fromOrd(ordFraction);
}
protected final void setFraction(final DDF_Fraction frac) {
ordFraction = frac.ord;
}
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getExchange()
*/
@Override
public DDF_Exchange getExchange() {
return DDF_Exchange.fromOrd(ordExchange);
}
protected final void setExchange(final DDF_Exchange exchange) {
ordExchange = exchange.ord;
}
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getSession()
*/
@Override
public final DDF_Session getSession() {
return DDF_Session.fromOrd(ordSession);
}
protected final void setSession(final DDF_Session session) {
ordSession = session.ord;
}
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getTradeDay()
*/
@Override
public final DDF_TradeDay getTradeDay() {
return DDF_TradeDay.fromOrd(ordTradeDay);
}
protected final void setTradeDay(final DDF_TradeDay day) {
ordTradeDay = day.ord;
}
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getSpreadType()
*/
@Override
public final DDF_SpreadType getSpreadType() {
return DDF_SpreadType.fromOrd(ordSpread);
}
protected final void setSpread(final DDF_SpreadType spread) {
ordSpread = spread.ord;
}
//
protected final byte[] getSymbolFull() {
return DDF_Symbology.byteArrayFromSymbolArray(symbolArray);
}
protected final byte[] getSymbolMain() {
if (symbolArray == null || symbolArray.length == 0) {
return DDF_Symbology.DDF_NO_NAME;
}
return symbolArray[0];
}
/*
* non spread:
*
* or
*
* for spread: __..._
*/
/*
* (non-Javadoc)
*
* @see com.barchart.feed.ddf.message.api.DDF_MarketBase#getId()
*/
@Override
public TextValue getId() {
return ValueBuilder.newText(getSymbolFull());
}
// ######################
/*
* ,()(,)()||
*
* ... body ...
*
* ||||
*/
/*
* (non-Javadoc)
*
* @see
* com.barchart.feed.ddf.message.provider.Base#encodeDDF(java.nio.ByteBuffer)
*/
@Override
public final void encodeDDF(final ByteBuffer buffer) {
encodeHead(buffer);
encodeBody(buffer);
encodeTail(buffer);
}
/*
* ,()(,)()||
*/
protected void encodeHead(final ByteBuffer buffer) {
final DDF_MessageType type = getMessageType();
//
buffer.put(SOH); //
buffer.put(type.record); //
buffer.put(getSymbolMain()); //
buffer.put(COMMA); // ','
buffer.put(type.subRecord); //
buffer.put(STX); //
buffer.put(getFraction().baseCode); //
buffer.put(getExchange().code); //
encodeDelay(buffer); // optional ()(,)
encodeSpread(buffer); // optional ()
}
/*
* ... body ...
*/
protected void encodeBody(final ByteBuffer buffer) {
throw new UnsupportedOperationException();
}
/*
* ||||
*/
protected void encodeTail(final ByteBuffer buffer) {
final DateTimeZone zone = getExchange().kind.time.zone;
//
buffer.put(getTradeDay().code); //
buffer.put(getSession().code); //
buffer.put(ETX); //
encodeFeedTimeStamp(millisUTC, zone, buffer);//
}
// ######################
/*
* (non-Javadoc)
*
* @see
* com.barchart.feed.ddf.message.provider.Base#decodeDDF(java.nio.ByteBuffer)
*/
@Override
public final void decodeDDF(final ByteBuffer buffer) {
decodeHead(buffer);
decodeBody(buffer);
decodeTail(buffer);
//log.debug(new String(buffer.array()) + getTime().toString());
}
/*
* ,()(,)()||
*/
protected void decodeHead(final ByteBuffer buffer) {
check(buffer.get(), SOH); //
final byte record = buffer.get(); //
final byte[] symbolMain = read(buffer, COMMA); // ,
final byte subRecord = buffer.get(); //
check(buffer.get(), STX); //
final byte baseCode = buffer.get(); //
final byte exchCode = buffer.get(); //
decodeDelay(buffer); // optional ()(,)
// final byte[][] symbolLegs = decodeSpread(buffer);
// setSymbol(symbolMain, symbolLegs);
setSymbol(symbolMain, null);
setMessageType(DDF_MessageType.fromPair(record, subRecord));
ordFraction = DDF_Fraction.fromBaseCode(baseCode).ord;
ordExchange = DDF_Exchange.fromCode(exchCode).ord;
}
protected final void setSymbol(final byte[] symbolMain, final byte[][] symbolLegs) {
if (symbolMain == null) {
symbolArray = null;
return;
}
if (symbolLegs == null) {
symbolArray = new byte[1][];
symbolArray[0] = symbolMain;
return;
}
final int legCount = symbolLegs.length;
symbolArray = new byte[1 + legCount][];
symbolArray[0] = symbolMain;
System.arraycopy(symbolLegs, 0, symbolArray, 1, legCount);
}
/*
* ... body ...
*/
protected void decodeBody(final ByteBuffer buffer) {
throw new UnsupportedOperationException();
}
/*
* ||||
*/
protected void decodeTail(final ByteBuffer buffer) {
DateTimeZone zone;
if (getInstrument().timeZoneName() == null
|| "Null Time Zone".equals(getInstrument().timeZoneName())) {
zone = getExchange().kind.time.zone;
} else {
zone = DateTimeZone.forID(getInstrument().timeZoneName());
}
//
final byte dayCode = buffer.get(); //
final byte sessCode = buffer.get(); //
check(buffer.get(), ETX); //
millisUTC = decodeFeedTimeStamp(zone, buffer); //
//
ordTradeDay = DDF_TradeDay.fromCode(dayCode).ord;
ordSession = DDF_Session.fromPair(ordExchange, sessCode).ord;
}
// ######################
/* , , , */
protected final void encodeSpread(final ByteBuffer buffer) {
final DDF_SpreadType spread = getSpreadType();
if (!spread.isKnown() || symbolArray == null || symbolArray.length == 1) {
return;
}
final int legCount = symbolArray.length - 1;
buffer.putChar(spread.code); //
encodeUnsigned_1(legCount, buffer); //
for (int k = 1; k <= legCount; k++) {
buffer.put(symbolArray[k]); //
buffer.put(ASCII.COMMA); // ,
}
}
protected final byte[][] decodeSpread(final ByteBuffer buffer) {
buffer.mark();
final char spreadCode = buffer.getChar(); //
final DDF_SpreadType spread = DDF_SpreadType.fromCode(spreadCode);
setSpread(spread);
if (spread.isKnown()) {
final int legCount = decodeUnsigned_1(buffer); //
final byte[][] symbolLegs = new byte[legCount - 1][];
for (int k = 0; k < symbolLegs.length; k++) {
symbolLegs[k] = read(buffer, COMMA); // ,
}
return symbolLegs;
} else {
buffer.reset();
return null;
}
}
protected final void updateSpread() {
if (symbolArray == null) {
return;
}
if (symbolArray.length == 1) {
setSpread(DDF_SpreadType.UNKNOWN);
} else {
setSpread(DDF_SpreadType.DEFAULT);
}
}
// ######################
protected void encodeDelay(final ByteBuffer buffer) {
encodeUnsigned_2(delay, buffer);
}
protected void decodeDelay(final ByteBuffer buffer) {
delay = decodeUnsigned_2(buffer);
}
// ######################
protected final void setDecodeDefaults() {
setDecodeDefaults(ClockDDF.clock.millis());
}
protected final void setDecodeDefaults(final long millisUTC) {
this.millisUTC = millisUTC;
setTradeDay(DDF_TradeDay.fromMillisUTC(millisUTC));
setSession(DDF_Session.FUT_COMBO);
}
// ######################
@Override
protected void appedFields(final StringBuilder text) {
super.appedFields(text);
text.append("symbol : ");
text.append(getId());
text.append("\n");
text.append("exchange : ");
text.append(getExchange());
text.append("\n");
text.append("fraction : ");
text.append(getFraction());
text.append("\n");
text.append("tradeDay : ");
text.append(getTradeDay());
text.append("\n");
text.append("session : ");
text.append(getSession());
text.append("\n");
text.append("spread : ");
text.append(getSpreadType());
text.append("\n");
text.append("delay : ");
text.append(getDelay());
text.append("\n");
}
}