Class DefaultTripStream

java.lang.Object
org.tentackle.trip.impl.DefaultTripStream
All Implemented Interfaces:
TripStream

public class DefaultTripStream extends Object implements TripStream
Default implementation of a TripStream.

As it doesn't make sense that multiple threads read from or write to the same stream concurrently, streams are not thread-safe and attaching streams to threads must be synchronized externally.

  • Constructor Details

    • DefaultTripStream

      public DefaultTripStream(Dictionary dictionary, Serializer serializer, Deserializer deserializer)
      Creates a new stream.
      Parameters:
      dictionary - the dictionary to use
      serializer - the serializer to use
      deserializer - the deserializer to use
  • Method Details

    • getDictionary

      public Dictionary getDictionary()
      Description copied from interface: TripStream
      Gets the dictionary.
      Specified by:
      getDictionary in interface TripStream
      Returns:
      the dictionary
    • getDictionaryEntry

      public DictionaryEntry getDictionaryEntry(int ordinal)
      Description copied from interface: TripStream
      Returns the dictionary entry for the given ordinal.
      Throws a TripRuntimeException if the ordinal is invalid.
      Specified by:
      getDictionaryEntry in interface TripStream
      Parameters:
      ordinal - the ordinal of the entry
      Returns:
      the entry
    • getDictionaryEntry

      public DictionaryEntry getDictionaryEntry(Class<?> clazz)
      Description copied from interface: TripStream
      Returns the entry for the given class.
      Specified by:
      getDictionaryEntry in interface TripStream
      Parameters:
      clazz - the class of the entry
      Returns:
      the entry, null if no such entry exists
    • getType

      public <T> TripType<T> getType(Class<? extends T> clazz)
      Description copied from interface: TripStream
      Gets the TripType for the given class.
      If there is a predefined or application-specific TripType, that is returned. Otherwise, the type will be created dynamically by the best matching TripTypeBuilder.

      Throws TripRuntimeException if no matching TripType could be found.

      Specified by:
      getType in interface TripStream
      Type Parameters:
      T - the data type
      Parameters:
      clazz - the class of the data type
      Returns:
      the trip type, never null
    • getSerializer

      public Serializer getSerializer()
      Description copied from interface: TripStream
      Gets the serializer.
      Specified by:
      getSerializer in interface TripStream
      Returns:
      the serializer
    • getDeserializer

      public Deserializer getDeserializer()
      Description copied from interface: TripStream
      Gets the deserializer.
      Specified by:
      getDeserializer in interface TripStream
      Returns:
      the deserializer
    • getReference

      public <T> Reference<T> getReference(T object, boolean deduplicate)
      Description copied from interface: TripStream
      Gets an existing or creates a new reference for an object.
      If de-duplicatable, the reference will be mapped by the object's equals() method. Otherwise, the reference will be mapped by an IdentityHashMap.
      Specified by:
      getReference in interface TripStream
      Parameters:
      object - the object to check
      deduplicate - true if the object is de-duplicatable, false otherwise
      Returns:
      the reference, never null
      See Also:
    • getReference

      public <T> Reference<T> getReference(Class<T> clazz, int referenceId)
      Description copied from interface: TripStream
      Gets the reference for the given reference id.

      Throws a TripRuntimeException if the reference id is invalid.

      Specified by:
      getReference in interface TripStream
      Type Parameters:
      T - the reference type
      Parameters:
      clazz - the referenced object's class
      referenceId - the reference id
      Returns:
      the reference, never null
    • addReference

      public <T> void addReference(Reference<T> reference, T object, boolean deduplicate)
      Description copied from interface: TripStream
      Adds a reference to the stream.
      The method is invoked by the receiver to add an object received from the sender.
      Specified by:
      addReference in interface TripStream
      Parameters:
      reference - the reference to add
      object - the object the reference belongs to
      deduplicate - true if the object is de-duplicatable, false otherwise
    • getClass

      public Class<?> getClass(String className)
      Description copied from interface: TripStream
      Gets the class for the given class name.
      Specified by:
      getClass in interface TripStream
      Parameters:
      className - the class name
      Returns:
      the class, never null
    • clear

      public void clear()
      Description copied from interface: TripStream
      Clears the stream's state.
      The sender and receiver should invoke this method to mark the boundary between messages.
      Specified by:
      clear in interface TripStream
    • clearModifiable

      public void clearModifiable()
      Description copied from interface: TripStream
      Clears all references that belong to modifiable objects.
      Invoked in the middle of a roundtrip, i.e., at the client side, after the request was sent to the server and at the server side, before the response is sent back to the client.
      This keeps the references of immutable objects on both sides and reduces the size of the response since for those objects, only the reference number is sent back without the referenced value.
      Specified by:
      clearModifiable in interface TripStream
    • writeComponentOrdinal

      public void writeComponentOrdinal(int ordinal)
      Description copied from interface: TripStream
      Writes a component ordinal to the stream.
      Specified by:
      writeComponentOrdinal in interface TripStream
      Parameters:
      ordinal - the ordinal to write
    • readComponentOrdinal

      public int readComponentOrdinal(OpCode opCode)
      Description copied from interface: TripStream
      Reads a component ordinal from the stream.
      Specified by:
      readComponentOrdinal in interface TripStream
      Parameters:
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • writeNextComponentOrdinal

      public int writeNextComponentOrdinal(int lastOrdinal, TripComponent<?,?> component)
      Description copied from interface: TripStream
      Writes the next ordinal to the stream, using optimization.
      Specified by:
      writeNextComponentOrdinal in interface TripStream
      Parameters:
      lastOrdinal - the last ordinal written, -1 for none
      component - the component to write the ordinal for
      Returns:
      the component ordinal
    • readNextComponentOrdinal

      public int readNextComponentOrdinal(int lastOrdinal, OpCode opCode)
      Description copied from interface: TripStream
      Reads the next ordinal from the stream, using optimization.
      Specified by:
      readNextComponentOrdinal in interface TripStream
      Parameters:
      lastOrdinal - the last ordinal read, -1 for none
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • writeDictionaryOrdinal

      public void writeDictionaryOrdinal(int ordinal)
      Description copied from interface: TripStream
      Writes a dictionary ordinal to the stream.
      Specified by:
      writeDictionaryOrdinal in interface TripStream
      Parameters:
      ordinal - the ordinal to write
    • readDictionaryOrdinal

      public int readDictionaryOrdinal(OpCode opCode)
      Description copied from interface: TripStream
      Reads a dictionary ordinal from the stream.
      Specified by:
      readDictionaryOrdinal in interface TripStream
      Parameters:
      opCode - the ordinal OpCode
      Returns:
      the ordinal
    • sendDictionary

      public void sendDictionary(Class<?> clazz)
      Description copied from interface: TripStream
      Updates the dictionary entry if the class is new for the stream and sends the class definition.
      Otherwise, sends the ordinal of the dictionary entry.
      Specified by:
      sendDictionary in interface TripStream
      Parameters:
      clazz - the object's class
    • receiveDictionary

      public DictionaryEntry receiveDictionary(OpCode opCode)
      Description copied from interface: TripStream
      Receives the dictionary entry and adds it to the dictionary.
      Specified by:
      receiveDictionary in interface TripStream
      Parameters:
      opCode - the opcode to read (already read from the stream)
      Returns:
      the dictionary entry
    • receiveDictionary

      public DictionaryEntry receiveDictionary()
      Description copied from interface: TripStream
      Receives the dictionary entry and adds it to the dictionary.
      Same as TripStream.receiveDictionary(OpCode) but reads the opcode.
      Specified by:
      receiveDictionary in interface TripStream
      Returns:
      the dictionary entry
    • deriveType

      public TripType<?> deriveType(OpCode opCode)
      Description copied from interface: TripStream
      Derives the concrete type from the given OpCode.
      If the returned derived type is dictionary-based, the next opcode must be read from the stream, which is either a reference or an ordinal.
      Specified by:
      deriveType in interface TripStream
      Parameters:
      opCode - the opcode (already read from the stream)
      Returns:
      the type
    • deriveType

      public TripType<?> deriveType()
      Description copied from interface: TripStream
      Derives the concrete type from the given OpCode.
      Same as TripStream.deriveType(OpCode) but reads the opcode.
      Specified by:
      deriveType in interface TripStream
      Returns:
      the type
    • receiveOpCode

      public OpCode receiveOpCode()
      Description copied from interface: TripStream
      Receives the next OpCode from the stream.

      Checks for the

      Specified by:
      receiveOpCode in interface TripStream
      Returns:
      the OpCode
    • sendError

      public void sendError(OpCode errorOpCode, Throwable t)
      Description copied from interface: TripStream
      Sends a TRIP error.
      The error opcode is sent first, followed by the throwable's stack trace.
      Specified by:
      sendError in interface TripStream
      Parameters:
      errorOpCode - the error opcode
      t - the throwable
    • getClassCount

      public Map<Class<?>, Holder<Integer>> getClassCount()
      Gets the number of object references per class.
      Provided for debugging purposes only!
      Returns:
      the map of class to reference count