/*
 * $Log: Switch.java,v $
# Revision 1.13  1996/07/11  04:37:27  hma
# added setLocation()
#
# Revision 1.12  1996/07/11  01:24:55  hma
# intermediate working version
#
# Revision 1.11  1996/06/17  15:23:57  hma
# now getAlpha and getOmega returns alpha and omega of the switch,
# not alpha and omega of the place.
#
# Revision 1.10  1996/06/05  20:42:20  hma
# change member variables x, y, alpha, omega to double from Double
#
# Revision 1.9  1996/05/12  09:48:24  inder
# added constructor from place. removed other constructors.
#
# Revision 1.8  1996/04/22  17:01:11  rlj1
# replaced beta with omega; made the constructor assign default values
# of 0 to alpha and omega.
#
# Revision 1.7  1996/04/22  06:15:30  inder
# Changed order of parameter name in Switch constructor.
#
# Revision 1.6  1996/04/19  04:21:32  inder
# defined a new constructor which initialises Name.
# initialising name,prop in constructors.
#
# Revision 1.5  1996/04/15  05:13:24  inder
# included import statements for classes in general package.
#
# Revision 1.4  1996/04/15  04:37:03  rlj1
# added alpha and beta vars
#
# Revision 1.3  1996/04/14  16:39:05  rlj1
# made streamable
#
# Revision 1.2  1996/04/08  03:26:25  rlj1
# Added readValues and writeValues; made x and y Doubles rather than
# doubles.
#
# Revision 1.1  1996/03/18  18:50:08  rlj1
# Initial revision
#
 */
//////////////////////////////////////////////////
package network;

// Standard Packages.
import java.io.*;
import java.util.*;
// Packages specific to Network Design Tool. 
import general.GenericException;
import general.GeneralProperties;
import physical.Place;
//////////////////////////////////////////////////
public class Switch extends Observable{

  public final static String CHANGED = "changed";
  public final static String DESTROYED = "destroyed";
  private String name;
  //private Place p;
  private double x, y;
  private double alpha = 1.0, omega = 1.0;
  //private GeneralProperties prop;

  /**
   * create a new switch with the given name.
   */
  public Switch(String name) {
    this(name, 0, 0);
  }

//----------------------------------------------------
public Switch(String name, double x, double y) {
  this(name, x, y, 1.0, 1.0);
}

//--------------------------------------------------
public Switch(String name, double x, double y, double alpha, double omega) {
  this.name = name;
  this.x = x;
  this.y = y;
  this.alpha = alpha;
  this.omega = omega;
}

//--------------------------------------------------
public Switch(StreamTokenizer stk) throws IOException {
  try {
    int tokenType, i;

    // read the starting '<'
    while ( stk.nextToken() != '<' );

    // read the first word, it should be the switch name 
    while ( stk.nextToken() != StreamTokenizer.TT_WORD );
    name = stk.sval;

    // get the first number, it would be the x coordinate
    while ( stk.nextToken() != StreamTokenizer.TT_NUMBER );
    x = stk.nval;
    
    // get the second number, it would be the y coordinate
    while ( stk.nextToken() != StreamTokenizer.TT_NUMBER );
    y = stk.nval;

    // p = new Place(name, x, y);

    // get the optional alpha and omega
    while ( (tokenType = stk.nextToken()) != '>' 
	   && tokenType != StreamTokenizer.TT_NUMBER);
    if (tokenType == '>') {
      alpha = Double.MAX_VALUE;
      omega = Double.MAX_VALUE;
    }
    else {
      // first, get alpha
      alpha = stk.nval;
      while (stk.nextToken() != StreamTokenizer.TT_NUMBER);
      // then, get omega
      omega = stk.nval;
      // get the closing '>'
      while (stk.nextToken() != '>');
    }
  }
  catch (IOException e) {
    System.out.println("error reading Switch");
    throw(e);
  }
}

//--------------------------------------------------
public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
  setChanged();
  notifyObservers(this.CHANGED);
}

//--------------------------------------------------
public double getAlpha() {
  return alpha;
}
//************************************************
public double getOmega() {
  return omega;
}
//************************************************
public double getX() {
  return x;
}
//--------------------------------------------------
public double getY() {
  return y;
}


//************************************************
public void setX(double val) {
  x = val;
  setChanged();
  notifyObservers(this.CHANGED);
}
//************************************************
public void setY(double val) {
  y = val;
  setChanged();
  notifyObservers(this.CHANGED);
}

public void setAlpha(double alpha) {
  this.alpha = alpha;
}

public void setOmega(double omega) {
  this.omega = omega;
}

public void setLocation(double x, double y) {
  this.x = x;
  this.y = y;
  setChanged();
  notifyObservers(this.CHANGED);
}

//************************************************
public void print() {
  System.out.println(this);
}

//************************************************
public String toString() {
  return ("<" + name + "," + x + "," + y + 
	  "," + alpha + "," + omega + ">");
}
//************************************************
public void inStream(DataInputStream d) throws GenericException {
  try {
    x = d.readDouble();
    y = d.readDouble();
    alpha = d.readDouble();
    omega = d.readDouble();
  }
  catch (Exception e) {
    throw new GenericException("Switch, inStream--Exception caught: " + e.getMessage());
  }    
}
//************************************************
public void outStream(DataOutputStream d) {
  try {
    d.writeDouble(x);
    d.writeDouble(y);
    d.writeDouble(alpha);
    d.writeDouble(omega);
  } catch (Exception e) {
    System.err.println("Switch, outStream--Exception caught: " +
		       e.getMessage()); 
  }    
}
//************************************************
public boolean readValues(DataInputStream d) throws EOFException,
  IOException {
  boolean success = true ;
  String line;
  if ((line = d.readLine()) == null) success = false ; 
  x = Double.valueOf(line).doubleValue() ; 
      
  if ((line = d.readLine()) == null) success = false ; 
  y = Double.valueOf(line).doubleValue() ;
 
  if ((line = d.readLine()) == null) success = false ; 
  alpha = Double.valueOf(line).doubleValue() ;
 
  if ((line = d.readLine()) == null) success = false ; 
  omega = Double.valueOf(line).doubleValue() ;
 
  line = d.readLine();
  return success ;
}
//************************************************
public void writeValues(DataOutputStream o) {
  try {
    String line = x + "\n";
    o.writeBytes(line) ;
    line = y + "\n";
    o.writeBytes(line) ;
    o.writeBytes("\n") ;
  } catch (Exception e ){};
}

/**
 * get the distance between two switches
 */
public static double distance(Switch a, Switch b) {
  double dx = a.getX() - b.getX();
  double dy = a.getY() - b.getY();
  return Math.sqrt( dx*dx + dy*dy );
}

}	
//////////////////////////////////////////////////
