FileDocCategorySizeDatePackage
OpNode.javaAPI DocExample3888Mon Nov 09 12:45:50 GMT 1998None

OpNode

public class OpNode extends Object

Fields Summary
public static final String
MULTIPLY
public static final String
DIVIDE
public static final String
ADD
public static final String
SUBTRACT
String
operator
Object[]
children
boolean
expanded
Constructors Summary
public OpNode()

  
     this(ADD); 
public OpNode(String op)

 operator = op; 
Methods Summary
protected java.lang.ObjectbuildChild(java.lang.Object o)

    if ((o instanceof Integer) || (o instanceof OpNode)) {
      return o;
    }
    String op = null;
    if (o instanceof String) {
      op = ((String)o).trim();
      if (op.equals(ADD)) { return new OpNode(ADD); }
      else if (op.equals(SUBTRACT)) { return new OpNode(SUBTRACT); }
      else if (op.equals(MULTIPLY)) { return new OpNode(MULTIPLY); }
      else if (op.equals(DIVIDE)) { return new OpNode(DIVIDE); }
      else {
        try { return Integer.valueOf(op); }
        catch (NumberFormatException nfe) { return new Integer(0); }
      }
    }
    System.err.println("Fell through new child: " + op);
    return new Integer(0);
  
protected voidbuildCollapsedString(java.lang.StringBuffer buf, java.lang.Object node)

    if (node == null) { return; }
    if (node instanceof OpNode) {
      buf.append("(");
      buildCollapsedString(buf, ((OpNode)node).getChild(0));
      buf.append(" ");
      buf.append(((OpNode)node).getOperator());
      buf.append(" ");
      buildCollapsedString(buf, ((OpNode)node).getChild(1));
      buf.append(")");
    }
    else { buf.append(node); }
  
public java.lang.ObjectgetChild(int index)

    if (index == 0) { return children[0]; }
    else if (index == 1) { return children[1]; }
    throw new IllegalArgumentException("child index must be 0 or 1");
  
public intgetChildCount()

    int count = 0;
    if (children[0] != null) { count++; }
    if (children[1] != null) { count++; }
    return count;
  
public intgetIndexOfChild(java.lang.Object child)

    if (child.equals(children[0])) { return 0;  }
    else if (child.equals(children[1])) { return 1; }
    return -1;
  
public java.lang.StringgetOperator()

 return operator; 
public voidsetChild(int index, java.lang.Object child)

    if (index == 0) { children[0] = buildChild(child); }
    else if (index == 1) { children[1] = buildChild(child); } 
    else {
      throw new IllegalArgumentException("child index " + index 
                                         + " must be 0 or 1");
    }
  
public voidsetExpanded(boolean b)

 expanded = b; 
public voidsetOperator(java.lang.Object op)

    if (op instanceof String) { operator = (String)op; }
    else {
      throw new IllegalArgumentException("operators must be strings");
    }
  
public java.lang.StringtoString()

    if (expanded) { return operator; }
    else {
      StringBuffer buf = new StringBuffer(40);
      buildCollapsedString(buf, this);
      return buf.toString();
    }