1.1.1

Table Of Contents
import java.io.*;
import java.math.*;
import java.sql.*;
public class Price implements Externalizable
{
// initial version id
private static final int FIRST_VERSION = 0;
private static final int TIMESTAMPED_VERSION = FIRST_VERSION + 1;
private static final Timestamp DEFAULT_TIMESTAMP = new Timestamp( 0L );
public String currencyCode;
public BigDecimal amount;
public Timestamp timeInstant;
// 0-arg constructor needed by Externalizable machinery
public Price() {}
public Price( String currencyCode, BigDecimal amount,
Timestamp timeInstant )
{
this.currencyCode = currencyCode;
this.amount = amount;
this.timeInstant = timeInstant;
}
// Externalizable implementation
public void writeExternal(ObjectOutput out) throws IOException
{
// first write the version id
out.writeInt( TIMESTAMPED_VERSION );
// now write the state
out.writeObject( currencyCode );
out.writeObject( amount );
out.writeObject( timeInstant );
}
public void readExternal(ObjectInput in)
throws IOException, ClassNotFoundException
{
// read the version id
int oldVersion = in.readInt();
if ( oldVersion < FIRST_VERSION ) {
throw new IOException( "Corrupt data stream." );
}
if ( oldVersion > TIMESTAMPED_VERSION ) {
throw new IOException( "Can't deserialize from the future." );
}
currencyCode = (String) in.readObject();
amount = (BigDecimal) in.readObject();
if ( oldVersion >= TIMESTAMPED_VERSION ) {
timeInstant = (Timestamp) in.readObject();
}
else {
timeInstant = DEFAULT_TIMESTAMP;
}
173
Programming User-Defined Types