package edu.neu.ccs.demeterf.examples; /** Type Unifying Example Class. */ import edu.neu.ccs.demeterf.TUCombiner; // Example Binary Tree Representation... abstract class BTree{} class Leaf extends BTree{ int data; Leaf(int i){ data = i; } } class BNode extends BTree{ BTree left, right; BNode(BTree l, BTree r){ left = l; right = r; } } // Actual TU Class class CountLeafs extends TUCombiner{ public Integer apply(){ return 0; } public Integer fold(Integer i, Integer j){ return i+j; } int apply(Leaf l){ return 1; } static int leafs(BTree t){ return TUCombiner.traverse(t, new CountLeafs()); } } // Other classes unchanged... class BNode2 extends BNode{ int data; BNode2(int d, BTree l, BTree r){ super(l,r); data = d; } } // Modified TU Class class CountLeafs2 extends CountLeafs{ int combine(BTree t, int d, int l, int r){ return l+r; } static int leafs(BTree t){ return TUCombiner.traverse(t, new CountLeafs2()); } } // Return of the Traversal class MyInt{ int i; MyInt(int j){ i = j; } MyInt plus(MyInt j){ return new MyInt(i+j.i); } } // New TU Class class CountLeafs3 extends TUCombiner{ public MyInt apply(){ return new MyInt(0); } public MyInt fold(MyInt i, MyInt j){ return i.plus(j); } MyInt apply(Leaf l){ return new MyInt(1); } static int leafs(BTree t){ return TUCombiner.traverse(t, new CountLeafs3()).i; } } /** TUCombiner Example */ public class TUExample { static void p(String s){ System.out.println(s); } public static void main(String s[]){ BTree noDTree = new BNode(new BNode(new Leaf(4), new Leaf(5)), new BNode(new Leaf(6), new Leaf(8))); BTree dTree = new BNode2(6, new BNode2(4, new Leaf(3), new Leaf(5)), new BNode2(8, new Leaf(7), new Leaf(9))); p(" CountLeafs: "+CountLeafs.leafs(noDTree)); p(" !Bad CountLeafs: "+CountLeafs.leafs(dTree)); p(" CountLeafs2: "+CountLeafs2.leafs(dTree)); p(" w/o Node Data CountLeafs3: "+CountLeafs3.leafs(noDTree)); p(" with Node Data CountLeafs3: "+CountLeafs3.leafs(dTree)); } }