// (c) MIT 2003.  All rights reserved.

// -*-Mode: Java-*- 
// $Id: TranscriptItem.java,v 1.2 2003/03/06 00:40:43 moltmans Exp $
// TranscriptItem.java -- 
// Author: Michael Oltmans <moltmans@ai.mit.edu> 
// Maintainer: Michael Oltmans <moltmans@ai.mit.edu> 
// Copyright: Copyright (C) 1999 by Michael Oltmans 
// Created: <Tue Jul 20 13:22:41 1999> 
// Time-stamp: <1999-08-23 15:55:01 moltmans> 
// Keywords: 
package rec.annotate.transcript;

import java.util.*;
import java.io.*;
import rec.util.*;

public class TranscriptItem implements Serializable
{
  private Object object;
  private Object oldObject;
  private long timeStamp;
  private int action;

  public static final int ADDED = 0;
  public static final int REMOVED = 1;
  public static final int REPLACED = 2;
  public static final int ALTERED = 3;

  public TranscriptItem( Object object, int action ) 
  {
    this( object, getCurrentTime(), action );
  }

  public TranscriptItem( Object oldobject, Object newobject, int action )
  {
    this( oldobject, newobject, getCurrentTime(), action );
  }
   
  public TranscriptItem( Object object, long timeStamp, int action ) 
  {
    this.object = object;
    this.timeStamp = timeStamp;
    this.action = action;
    this.oldObject = null;
  }

  public TranscriptItem( Object oldobject, Object newobject,
			 long timeStamp, int action )
  {
    this.object = newobject;
    this.oldObject = oldobject;
    this.timeStamp = timeStamp;
    this.action = action;
    Assert.check( action == REPLACED, "Improper use of TranscriptItem constructor for non-replacement."  );
  }
  

  public Object getObject( )
  {
    return object;
  }

  public Object getOldObject( )
  {
    return oldObject;
  }

  public long getTimeStamp( )
  {
    return timeStamp;
  }

  public int getAction( )
  {
    return action;
  }

  public String toString( )
  {
    StringBuffer result = new StringBuffer( "TranscriptItem: " );
    
    if ( action == REPLACED )
      result.append( object + " replaces " + oldObject + " " + timeStamp );
    else
      result.append( object + " " + actionToString( action ) + " " + timeStamp );
    return result.toString();
  }

  static public String actionToString( int action )
  {
    switch ( action ) {
    case ADDED:
      return "added";
    case REMOVED:
      return "removed";
    case REPLACED:
      return "replaced";
    case ALTERED:
      return "altered";
    default:
      Assert.error( "Invalid state for a TranscriptItem: " + action );
    }

    return "";
  }
    
  static private long getCurrentTime( )
  {
    Date d = new Date();
    return d.getTime();
  }

}
