Method.java

package algorithmen.umleditor;

import java.util.*;

/**
 * Method with parameter list and return type.
 * Part of Class and Interface objects.
 */
public class Method extends Member implements Cloneable {
  
  protected Vector parameters;    // parameter list, contains Fields
  protected boolean abstractFlag;
  
  /**
   * Creates a new instance of Method with given name and return type.
   * The parameter list is empty.
   */
  public Method(String name, String type) {
    super(name, type);
    parameters = new Vector();
  }
  
  /**
   * Standard-Konstruktor mit Namen "NewMethod" und Typ void
   */
  public Method() {
    this("newMethod", "void");
  }
  
  /**
   * Fügt das Field f der Parameterliste (am Ende) hinzu.
   */
  public void addParameter(Field f) {
    parameters.add(f);
  }
  
  /**
   * Entfernt das Field an der Position index aus der Parameterliste.
   */
  public void removeParameter(int index) {
    parameters.remove(index);
  }
  
  /**
   * Holt die Liste aller Parameter.
   */
  public Vector getParameter() {
    return parameters;
  }
  
  /**
   * Setzt die Liste aller Parameter auf die neue Liste.
   */
  public void setParameter(Vector newList) {
    parameters = newList;
  }
  
  /**
   * Gets the abstract flag of the Class.
   */
  public boolean isAbstract() {
    return abstractFlag;
  }
  
  /**
   * Sets the abstract flag of the Class.
   */
  public void setAbstract(boolean flag) {
    abstractFlag = flag;
  }
  
  /**
   * Liefert eine String-Darstellung eines Method-Objekts in Java-Form.
   * Typische Beispiele:
   *    "static public double add(double d1=5,double d2=3)"
   */
  public String toString() {
    String s = "";

    if (abstractFlag) {
      s += "abstract ";
    }

    if (staticFlag) {
      s += "static ";
    }
    
    if (visibility != Visibility.DEFAULT) {
      s += visibility + " ";
    }
    
    s += type + " " + name + "(";
    
    // laufe durch die Parameterliste
    Iterator i = parameters.iterator();
    // erstes Feld ohne Komma am Anfang
    if (i.hasNext()) {   // Liste ist nicht leer
      Field f = (Field) i.next();
      s += f;
    }
    // übrige Elemente, mit Komma getrennt
    while (i.hasNext()) {
      Field f = (Field) i.next();
      s += ", " + f;
    }
    
    s += ")";
    return s;
  }
  
  /**
   * support for cloning.
   */
  public java.lang.Object clone() {
    java.lang.Object o = null;
    try {
      Method m = (Method) super.clone();
      for (int i = 0; i < m.parameters.size(); i++) {
        m.parameters.set(i, ((Field) m.parameters.get(i)).clone());
      }
      o = m;
    } catch(CloneNotSupportedException e) {
      System.err.println("Method kann nicht klonen.");
    }
    return o;
  }
}