ArrowType.java

package algorithmen.umleditor;

import java.io.*;

/**
 * Enumeration type for the possible arrow types
 */
public class ArrowType implements Serializable {
  
  private String name;
  
  /**
   * Creates a new instance of ArrowType with a given String.
   * Since it is private, only the predefined values can be used.
   */
  private ArrowType(String s) {
    name = s;
  }
  
  public String toString() {
    return name;
  }
  
  /**
   * Ist true bei vertikalen Beziehungen (Vererbung, Implementierung),
   * false bei den anderen (horizontalen).
   */
  public boolean isVertical() {
    if (equals(EXTENSION) || equals(IMPLEMENTATION)) {
      return true;
    } else {
      return false;
    }
  }
  
  /**
   * Liefert ein Array mit allen Zugriffswerten
   */
  public static ArrowType[] getArrowTypes() {
    return list;
  }
  
  /**
   * Liste der möglichen Werte.
   * Im Gegensatz zum Enumeration-Pattern nicht final, da sie durch
   *   Serialisierung neu erzeugt werden sollen.
   */
  public static  ArrowType ASSOCIATION = new ArrowType("association");
  public static  ArrowType REFERENCE = new ArrowType("reference");
  public static  ArrowType RELATION = new ArrowType("relation");
  public static  ArrowType AGGREGATION = new ArrowType("aggregation");
  public static  ArrowType EXTENSION = new ArrowType("extension");
  public static  ArrowType IMPLEMENTATION = new ArrowType("implementation");
  
  protected static ArrowType[] list =
  {ASSOCIATION, REFERENCE, RELATION, AGGREGATION, EXTENSION, IMPLEMENTATION};
  
  
  /**
   * Serialisierung ist nicht automatisch für static Felder.
   * Daher müssen die Methoden writeObject und readObject geschrieben werden.
   */
  private void writeObject(ObjectOutputStream out) throws IOException {
    out.defaultWriteObject();
    out.writeObject(ASSOCIATION);
    out.writeObject(REFERENCE);
    out.writeObject(RELATION);
    out.writeObject(AGGREGATION);
    out.writeObject(EXTENSION);
    out.writeObject(IMPLEMENTATION);
  }
  
  private void readObject(ObjectInputStream in)
  throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    ASSOCIATION = (ArrowType) in.readObject();
    REFERENCE = (ArrowType) in.readObject();
    RELATION = (ArrowType) in.readObject();
    AGGREGATION = (ArrowType) in.readObject();
    EXTENSION = (ArrowType) in.readObject();
    IMPLEMENTATION = (ArrowType) in.readObject();
  }
  
  
  /*
   * Vergleicht zwei ArrowType-Objekte.
   * Dies ist (im Gegensatz zum normalen Enumeration-Pattern) nötig,
   *   da die Objekte durch Serialisierung neu erzeugt werden können,
   *   was zu neuen Adressen führt, also zum Nicht-Funktionieren von ==
   */
  public boolean equals(ArrowType type) {
    return name.equals(type.toString());
  }
  
  /*
   * Wird equals geändert, muss auch hashCode angepasst werden, damit gleiche 
   * Elemente auch denselben Hashcode produzieren.
   */
  
  public int hashCode() {
    return name.hashCode();
  }
}