// Carnegie Mellon University
//   Information Networking Institute and
//   School of Computer Science
//
// Master Thesis: A Monitoring Tool for Overlay Network
// By: TungFai Chan and Annie Cheng
//
// File: SpanningTreeManager.java
// Path: eventbase/routing
// Description:

package eventbase.routing;
import java.text.*;
import eventbase.*;

public class SpanningTreeTableEntry {

  double time;
  int srcID;
  int targetID;

  // currently, getBytes will only be calculated once.  If value of private
  // member changes, byteVal needs to be recalculated.
  private byte [] byteVal;

  public SpanningTreeTableEntry(double t, int from, int to) {
    srcID = from;
    targetID = to;
    time = t;
    byteVal = null;
  }

  public double getTime() {
    return time;
  }

  public int getSourceID() {
    return srcID;
  }

  public int getTargetID() {
    return targetID;
  }

  public void print() {
    DecimalFormat numformat = new DecimalFormat();
    numformat.setMaximumFractionDigits(6);
    System.out.println (numformat.format(time) + " " + srcID + " " + targetID);
  }

  // Caution: this function needs to be modified if set functions are allowed
  // currently, getBytes will only be calculated once.  If value of private
  // member changes, byteVal needs to be recalculated.
  public byte[] getBytes() {
    if (byteVal == null){
      byteVal = new byte[MemoryManager.getBytesPerRecord()];
      MemoryManager.insertDouble(byteVal, 0, this.time);
      MemoryManager.insertInteger(byteVal, 8, this.srcID);
      MemoryManager.insertInteger(byteVal, 12, this.targetID);
    }
    return byteVal;
  }

  public static SpanningTreeTableEntry extractFromBytes(byte[] buffer) {
    if (buffer == null) {
      System.err.println("ERROR: LinkInfoTableEntry() Unable to extract bytes");
      return null;
    }
    double t = MemoryManager.bytesToDouble(buffer, 0);
    int sid = MemoryManager.bytesToInteger(buffer, 8);
    int tid = MemoryManager.bytesToInteger(buffer, 12);
    return new SpanningTreeTableEntry(t, sid, tid);
  }

}
