Write Nested interface with Direct Reference

35 views
Skip to first unread message

side cap

unread,
Jun 18, 2015, 11:18:55 AM6/18/15
to java-ch...@googlegroups.com
Hello,

I take a simple nested interface example:

interface MyInterface extends Byteable{
byte getByte();
void setByte(byte t);

MyNestedInterface getObject();
void setObject(MyNestedInterface t);

interface MyNestedInterface extends Byteable{
double getTrade();
void setTrade(double p);
}
}


How to: ?

Chronicle chronicle = ChronicleQueueBuilder.vanilla("C:\\").build();
ExcerptAppender appender = chronicle.createAppender();
final MyInterface event = DataValueClasses.newDirectReference(MyInterface.class);

appender.startExcerpt(event.maxSize());
event.bytes(appender, 0);

event.setByte((byte) 7);
event.setObject(?????????????); // How to put an object of type MyNestedInterface ?

appender.position(event.maxSize());
// if we didnt want to write event.setObject(...) then appender.position should be appender.position(size of one byte)
// bacause only used event.setByte((byte) 7); ???
appender.finish();
chronicle.close();


Best Regards,

Peter Lawrey

unread,
Jun 18, 2015, 12:13:35 PM6/18/15
to java-ch...@googlegroups.com
You can create another object to set it however a simpler approach is to do

MyNestedInterface mni = event.getObject();
mni.setTrade(price);

otherwise you can do the following if the interface didn't extend Byteable.

MyNestedInterface mni = DataValueClasses.newHeapInstance(MyNestedInterface.class);
mni.setTrade(price);
event.setObject(mni);

At the moment we don't guarentee that the one byte will be the first one.

If you are going to use the byte as a message type I suggest doing this seperately.

appender.startExcerpt();
appender.writeUnsignedByte(MyNestedInterface.CODE);
((Byteable) mni).bytes(appender, appender.position());
mni.setTrade(price);
appender.skip(8);
appender.finish();

You might it is simpler to not use the intermediate data type in this case.

public void writeTrade(double price) {
    appender.startExcerpt();
    appender.writeUnsignedByte(MessageTypes.TRADE);
    appender.writeDouble(price);
    appender.finish();
}



--
You received this message because you are subscribed to the Google Groups "Chronicle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-chronicl...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages