KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > protoactive > BinaryTreeImpl


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

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   // implementation of the BinaryTree interface
47

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 JavaDoc 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   // implementation of the BindingController interface
89

90   public String JavaDoc[] listFc () {
91     return new String JavaDoc[] { "left", "right" };
92   }
93
94   public Object JavaDoc lookupFc (String JavaDoc 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 JavaDoc clientItfName, Object JavaDoc 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 JavaDoc clientItfName) {
112     if (clientItfName.equals("left")) {
113       leftTree = null;
114     } else if (clientItfName.equals("right")) {
115       rightTree = null;
116     }
117   }
118
119   // utility classes and methods
120

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 JavaDoc 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 JavaDoc value;
142
143     public ValueImpl (final Object JavaDoc value) {
144       this.value = value;
145     }
146
147     public String JavaDoc toString () {
148       return value.toString();
149     }
150   }
151 }
152
Popular Tags