import java.util.*;
import java.io.*;
class TreeNode implements Serializable {
  Vector children;
  TreeNode parent;
  String name;
  transient Date date;

  public TreeNode(String s) {
    children = new Vector(5);
    name = s;
    initClass();
  }
  private void initClass () {
    date = new Date();
  }
  public synchronized void setChildren (TreeNode[] list) {
    children.removeAllElements();
    int size = list.length;
    for (int i=0;i<size;i++) {
      addChild (list[i]);
    }
  }
  public void setChildren (TreeNode element, int position) {
    children.setElementAt (element, position);
    element.parent = this;
  }
  public synchronized TreeNode[] getChildren () {
    int size = children.size();
    TreeNode tree[] = new TreeNode[size];
    for (int i=0;i<size;i++) {
      tree[i] = (TreeNode)children.elementAt (i);
    }
    return tree;
  }
  public TreeNode getChildren (int position) {
    return (TreeNode)children.elementAt (position);
  }
  public void addChild (TreeNode n) {
    children.addElement(n);
    n.parent = this;
  }

  public String toString() {
    Enumeration e = children.elements();
    StringBuffer buff = new StringBuffer(100);
    buff.append("[ " +  name + " : ");
    while(e.hasMoreElements()) {
      buff.append(e.nextElement().toString());
    }
    buff.append(" ] ");
    buff.append (date.toString());
    return buff.toString();
  }
  public static void main (String args[]) {
    TreeNode top = new TreeNode("top");
    TreeNode left, right, both;
    top.addChild(left = new TreeNode("left child"));
    top.addChild(right = new TreeNode("right child"));
    both = new TreeNode ("problem child");
    left.addChild (both);
    right.addChild (both);
    System.out.println (top);
    try {
      FileOutputStream fOut = new FileOutputStream("test.out");
      ObjectOutput out = new ObjectOutputStream(fOut);
      out.writeObject(top);
      out.flush();
      out.close();
      FileInputStream fIn = new FileInputStream("test.out");
      ObjectInputStream in = new ObjectInputStream(fIn);
      TreeNode n = (TreeNode)in.readObject();
      System.out.println ("After: " + n);
    } catch (Exception e) {
      System.out.println ("Whoops");
      e.printStackTrace();
    }
  }
  private void writeObject(ObjectOutputStream s)
      throws IOException {
    s.defaultWriteObject();
  }
  private void readObject(ObjectInputStream s)
      throws ClassNotFoundException, IOException {
    s.defaultReadObject();
    initClass();
  }
}