1 23 24 package protoactive; 25 26 import org.objectweb.fractal.api.Component; 27 import org.objectweb.fractal.api.NoSuchInterfaceException; 28 import org.objectweb.fractal.api.control.BindingController; 29 import org.objectweb.fractal.api.type.ComponentType; 30 import org.objectweb.fractal.api.type.InterfaceType; 31 import org.objectweb.fractal.api.type.TypeFactory; 32 33 import org.objectweb.fractal.util.Fractal; 34 35 36 public class BinaryTreeImpl implements BinaryTree, BindingController { 37 38 private int key; 39 private Value value; 40 41 private BinaryTree leftTree; 42 private BinaryTree rightTree; 43 44 private static ComponentType TYPE; 45 46 48 public Value get (int key) { 49 if (leftTree == null && rightTree == null) { 50 return null; 51 } 52 if (key == this.key) { 53 return value; 54 } else if (key < this.key) { 55 return leftTree.get(key); 56 } else { 57 return rightTree.get(key); 58 } 59 } 60 61 public void put (int key, Object value) { 62 if (leftTree == null && rightTree == null) { 63 this.key = key; 64 this.value = new ValueImpl(value); 65 66 Component leftComp = 67 ProtoActive.createActive(getType(), "protoactive.BinaryTreeImpl"); 68 Component rightComp = 69 ProtoActive.createActive(getType(), "protoactive.BinaryTreeImpl"); 70 71 try { 72 leftTree = (BinaryTree)leftComp.getFcInterface("server"); 73 rightTree = (BinaryTree)rightComp.getFcInterface("server"); 74 } catch (NoSuchInterfaceException e) { 75 e.printStackTrace(); 76 System.exit(0); 77 } 78 } 79 if (key == this.key) { 80 this.value = new ValueImpl(value); 81 } else if (key < this.key) { 82 leftTree.put(key, value); 83 } else { 84 rightTree.put(key, value); 85 } 86 } 87 88 90 public String [] listFc () { 91 return new String [] { "left", "right" }; 92 } 93 94 public Object lookupFc (String clientItfName) { 95 if (clientItfName.equals("left")) { 96 return leftTree; 97 } else if (clientItfName.equals("right")) { 98 return rightTree; 99 } 100 return null; 101 } 102 103 public void bindFc (String clientItfName, Object serverItf) { 104 if (clientItfName.equals("left")) { 105 leftTree = (BinaryTree)serverItf; 106 } else if (clientItfName.equals("right")) { 107 rightTree = (BinaryTree)serverItf; 108 } 109 } 110 111 public void unbindFc (String clientItfName) { 112 if (clientItfName.equals("left")) { 113 leftTree = null; 114 } else if (clientItfName.equals("right")) { 115 rightTree = null; 116 } 117 } 118 119 121 static ComponentType getType () { 122 if (TYPE == null) { 123 try { 124 Component boot = Fractal.getBootstrapComponent(); 125 TypeFactory tf = Fractal.getTypeFactory(boot); 126 TYPE = tf.createFcType(new InterfaceType[] { 127 tf.createFcItfType("server", "protoactive.BinaryTree", false, false, false), 128 tf.createFcItfType("left", "protoactive.BinaryTree", true, false, false), 129 tf.createFcItfType("right", "protoactive.BinaryTree", true, false, false) 130 }); 131 } catch (Exception e) { 132 e.printStackTrace(); 133 System.exit(0); 134 } 135 } 136 return TYPE; 137 } 138 139 static class ValueImpl implements Value { 140 141 private Object value; 142 143 public ValueImpl (final Object value) { 144 this.value = value; 145 } 146 147 public String toString () { 148 return value.toString(); 149 } 150 } 151 } 152 | Popular Tags |