TWiki . Doc . ExtendedTreeVisitorIntermediateExample

// compilation unit MultiInterior.nice
package openclassdispatch;

public class MultiInterior<T> extends Tree {
   private Tree<T>[] children ;

   public Tree<T>[] getChildren() = children;

   prettyPrint(prefix) {
      let result = new StringBuffer( prefix + this.value() + "\n" );
      let newPrefix = prefix + "| ";
      for (each : children) 
         result.append( each.prettyPrint( newPrefix ) );
      return result.toString;
   }

   dispatchTest() {
      for (each : children) each.dispatchTest;
   }

   size(){
      var count = 1;
      for (each : this.children) count += each.size; 
      return count;
   }
}

// compilation unit dispatchTest.nice
package openclassdispatch;

<T> void dispatchTest(Tree<T> t); 

dispatchTest(this@Tree) { }

dispatchTest(this@Interior) {
   this.getLeft().dispatchTest();
   this.getRight().dispatchTest();
}

// compilation unit size.nice
package openclassdispatch;

<T> int size(Tree<T> t);

size(t@Tree) = 1;

size(t@Interior) = 1 + t.getLeft().size() + t.getRight().size();

/ compilation unit prettyPrint.nice
package openclassdispatch;

public <T> String prettyPrint(Tree<T> t);

prettyPrint(t@Tree) = t.prettyPrint( "" );


public <T> String prettyPrint(Tree<T> t, String prefix);

prettyPrint(t@Tree, prefix) = prefix + t.value() + "\n";

prettyPrint(t@Interior, prefix) {
   let result = new StringBuffer( prefix + t.value() + "\n" );
   let newPrefix = prefix + "| ";
   result.append( t.left().prettyPrint( newPrefix ) );
   result.append( t.right().prettyPrint( newPrefix ) );
   return result.toString();
}

-- IsaacGouy - 05 Sep 2003

----- Revision r1.1 - 05 Sep 2003 - 17:20 GMT - IsaacGouy
Copyright © 1999-2003 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback.