org.jplate.foundation.io
Interface ExternalizableStrategyIfc<T>

Type Parameters:
T - The type to be externalized.

public interface ExternalizableStrategyIfc<T>

This interface defines a strategy to serialize/deserialize an object. This interface is useful in scenarios where one may not want to subclass to ensure a class is serializable (that isn't already serializable) or the class to serialize cannot be subclassed (either abstract or final).

The following example illustrates a trivial implementation of a person and a an ExternablizableStrategyIfc that can externalize the person. Note: The person is declared final.

public final class Person
{
    private String _name;
    private int    _age;

    public Person ()
    {
    }

    public String getName () { return _name; }

    public void setName ( final String name ) { _name = name; }

    public int getAge () { return _age; }

    public void setAge ( final String age ) { _age = age; }
}


public final PersonExternalizableStrategy
    implements ExternablizableStrategyIfc <Person>
{
    public void writeExternal
        ( final Person person, final  ObjectOutput objectOutput )
            throws IOException
    {
        objectOutput.writeObject ( person.getName () );
        objectOutput.writInt     ( person.getAge  () );
    }

    public void readExternal
        ( final Person person, final ObjectInput objectInput )
            throws IOException, ClassNotFoundException
    {
        person.setName ( ( String ) objectInput.readObject () );
        person.setAge  (            objectInput.readInt    () );
    }
}


public final class PeopleCollection implements Externalizable
{
    private final List  _personList; = new LinkedList  ();

    private final ExternalizableStrategyIfc  _strategy;

    public PeopleCollection ( final ExternalizableStrategyIfc  strategy )
    {
        _strategy = strategy;
    }

    public PeopleCollection ()
    {
        this ( new PersonExternalizableStrategy () );
    }

    public void writeExternal ( final ObjectOutput out ) throws IOException
    {
        out.write
        for ( final Person person : _personList )
        {
            _strategy.writeExternal ( person, out );
        }
    }

    public void readExternal ( final ObjectInput in )
        throws IOException, ClassNotFoundException
    {

    }
}
    
Modifications:
    $Date: 2008-12-02 12:32:45 -0500 (Tue, 02 Dec 2008) $
    $Revision: 479 $
    $Author: sfloess $
    $HeadURL: https://jplate.svn.sourceforge.net/svnroot/jplate/trunk/src/dev/java/org/jplate/foundation/io/ExternalizableStrategyIfc.java $
    


Method Summary
 void readExternal(T externalizable, java.io.ObjectInput objectInput)
          Deserialize externalizable from objectInput.
 void writeExternal(T externalizable, java.io.ObjectOutput objectOutput)
          Serialize externalizable to objectOutput.
 

Method Detail

writeExternal

void writeExternal(T externalizable,
                   java.io.ObjectOutput objectOutput)
                   throws java.io.IOException
Serialize externalizable to objectOutput.

Parameters:
externalizable - The object to serialize to objectOutput.
objectOutput - The stream to write externalizable to.
Throws:
java.io.IOException - If any I/O exceptions arise.

readExternal

void readExternal(T externalizable,
                  java.io.ObjectInput objectInput)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Deserialize externalizable from objectInput.

Parameters:
externalizable - The object to deserialize from objectInput.
objectInput - The stream to read externalizable from.
Throws:
java.io.IOException - If any I/O exceptions arise.
java.lang.ClassNotFoundException