org.planx.io
Class PolymorphicStreamer<T>

java.lang.Object
  extended by org.planx.io.PolymorphicStreamer<T>
All Implemented Interfaces:
Streamer<T>

public class PolymorphicStreamer<T>
extends Object
implements Streamer<T>

A polymorphic Streamer that can handle multiple datatypes. A datatype is registered using addStreamer(byte, java.lang.Class, org.planx.io.Streamer). Each datatype must be given a unique id that is used to differentiate between the datatypes when reading data. When writing data the class of the object is examined. If multiple Streamers are registered which can all handle a specific subclass, it is unspecified which Streamer is used. The most specific Streamer is not chosen. Thus, in general, registering a Streamer with a super class of a class of another registered Streamer should be avoided. Use the most specific class possible when registering Streamers.

Streamers used with the PolymorphicStreamer must always return null in their toStream method.

Author:
Thomas Ambus

Constructor Summary
PolymorphicStreamer()
           
 
Method Summary
<E extends T>
void
addStreamer(byte id, Class<? extends E> cls, Streamer<E> streamer)
           
 T fromStream(DataInput in)
          Reads an object of class E from the data source.
 void toStream(DataOutput out, T obj)
          Writes the internal state of the object to the data source in a format that can later be read by the same Streamer using the fromStream method.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PolymorphicStreamer

public PolymorphicStreamer()
Method Detail

addStreamer

public <E extends T> void addStreamer(byte id,
                                      Class<? extends E> cls,
                                      Streamer<E> streamer)

toStream

public void toStream(DataOutput out,
                     T obj)
              throws IOException
Description copied from interface: Streamer
Writes the internal state of the object to the data source in a format that can later be read by the same Streamer using the fromStream method.

Specified by:
toStream in interface Streamer<T>
Throws:
IOException

fromStream

public T fromStream(DataInput in)
             throws IOException
Description copied from interface: Streamer
Reads an object of class E from the data source.

Specified by:
fromStream in interface Streamer<T>
Throws:
IOException


Copyright © 2004-2005 Plan-X. All Rights Reserved.