KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > group > topology > Plan


1 /*
2  * Created on Mar 16, 2004
3  */

4 package org.objectweb.proactive.core.group.topology;
5
6 import org.objectweb.proactive.core.group.Group;
7 import org.objectweb.proactive.core.group.ProxyForGroup;
8 import org.objectweb.proactive.core.mop.ConstructionOfReifiedObjectFailedException;
9
10
11 /**
12  * This class represents a group by a two-dimensional topology.
13  *
14  * @author Laurent Baduel
15  */

16 public class Plan extends Line { // implements Topology2D {
17

18     /** height of the two-dimensional topology group */
19     protected int height; // => Y => number of lines
20

21     /**
22      * Construtor. The members of <code>g</code> are used to fill the topology group.
23      * @param g - the group used a base for the new group (topology)
24      * @param height - the heigth of the two-dimensional topology group
25      * @param width - the width of the two-dimensional topology group
26      * @throws ConstructionOfReifiedObjectFailedException
27      */

28     public Plan(Group g, int height, int width)
29         throws ConstructionOfReifiedObjectFailedException {
30         super(g, height * width);
31         this.height = height;
32         this.width = width;
33     }
34
35     /**
36      * Construtor. The members of <code>g</code> are used to fill the topology group.
37      * @param g - the group used a base for the new group (topology)
38      * @param height - the heigth of the two-dimensional topology group
39      * @param width - the width of the two-dimensional topology group
40      * @throws ConstructionOfReifiedObjectFailedException
41      */

42     protected Plan(Group g, int nbMembers)
43         throws ConstructionOfReifiedObjectFailedException {
44         super(g, nbMembers);
45     }
46
47     /**
48      * Return the max size of the line
49      * @return the max size of the one-dimensional topology group (i.e. the line)
50      */

51     public int getWidth() {
52         return this.width;
53     }
54
55     /**
56      * Returns the height of the two-dimensional topology group (number of lines)
57      * @return the height of the two-dimensional topology group
58      */

59     public int getHeight() {
60         return this.height;
61     }
62
63     /**
64      * Returns the coordonate X for the specified position, according to the dimension
65      * @param position
66      * @return the coordonate X
67      */

68     private int getX(int position) {
69         return position % this.width;
70     }
71
72     /**
73      * Returns the coordonate Y for the specified position, according to the dimension
74      * @param position
75      * @return the coordonate Y
76      */

77     private int getY(int position) {
78         return position % this.height;
79     }
80
81     /**
82      * Returns the object at the left of the specified object in the two-dimensional topology group
83      * @param o - the specified object
84      * @return the object at the left of <code>o<code>. If there is no object at the left of <code>o</code>, return <code>null</code>
85      */

86     public Object JavaDoc left(Object JavaDoc o) {
87         int positionX = this.getX(this.indexOf(o));
88         if (positionX != 0) {
89             return this.get(positionX - 1);
90         } else {
91             return null;
92         }
93     }
94
95     /**
96      * Returns the X-coordonate of the specified object
97      * @param o - the object
98      * @return the position (in X) of the object in the Plan
99      */

100     public int getX(Object JavaDoc o) {
101         return this.indexOf(o) % this.getWidth();
102     }
103
104     /**
105      * Returns the Y-coordonate of the specified object
106      * @param o - the object
107      * @return the position (in Y) of the object in the Plan
108      */

109     public int getY(Object JavaDoc o) {
110         return this.indexOf(o) / this.getWidth();
111     }
112
113     /**
114      * Returns the object at the right of the specified object in the two-dimensional topology group
115      * @param o - the specified object
116      * @return the object at the right of <code>o<code>. If there is no object at the right of <code>o</code>, return <code>null</code>
117      */

118     public Object JavaDoc right(Object JavaDoc o) {
119         int positionX = this.getX(this.indexOf(o));
120         if (positionX != this.getWidth()) {
121             return this.get(positionX + 1);
122         } else {
123             return null;
124         }
125     }
126
127     /**
128      * Returns the object at the up of the specified object in the two-dimensional topology group
129      * @param o - the specified object
130      * @return the object at the up of <code>o<code>. If there is no object at the up of <code>o</code>, return <code>null</code>
131      */

132     public Object JavaDoc up(Object JavaDoc o) {
133         int positionY = this.getY(this.indexOf(o));
134         if (positionY != 0) {
135             return this.get(positionY - this.getWidth());
136         } else {
137             return null;
138         }
139     }
140
141     /**
142      * Returns the object at the down of the specified object in the two-dimensional topology group
143      * @param o - the specified object
144      * @return the object at the down of <code>o<code>. If there is no object at the down of <code>o</code>, return <code>null</code>
145      */

146     public Object JavaDoc down(Object JavaDoc o) {
147         int position = this.getY(this.indexOf(o));
148         if (position != this.getHeight()) {
149             return this.get(position + this.getWidth());
150         } else {
151             return null;
152         }
153     }
154
155     /**
156      * Returns the line (one-dimensional topology group) with the specified number
157      * @param line - the number of the line
158      * @return the one-dimensional topology group formed by the line in the two-dimensional topology group, return <code>null</code> if the the specified line is incorrect
159      */

160     public Line line(int line) {
161         if ((line < 0) || (line > this.getWidth())) {
162             return null;
163         }
164         ProxyForGroup tmp = null;
165         try {
166             tmp = new ProxyForGroup(this.getTypeName());
167         } catch (ConstructionOfReifiedObjectFailedException e) {
168             e.printStackTrace();
169         }
170         int begining = line * this.getWidth();
171         for (int i = begining; i < (begining + this.getWidth()); i++) {
172             tmp.add(this.get(i));
173         }
174         Line result = null;
175         try {
176             result = new Line((Group) tmp, this.getWidth());
177         } catch (ConstructionOfReifiedObjectFailedException e) {
178             e.printStackTrace();
179         }
180         return result;
181     }
182
183     /**
184      * Returns the line that contains the specified object
185      * @param o - the object
186      * @return the one-dimensional topology group formed by the line of the object in the two-dimensional topology group
187      */

188     public Line line(Object JavaDoc o) {
189         return this.line(this.getY(this.indexOf(o)));
190     }
191
192     /**
193      * Returns the column (one-dimensional topology group) with the specified number
194      * @param column - the number of the line
195      * @return the one-dimensional topology group formed by the column in the two-dimensional topology group, return <code>null</code> if the the specified line is incorrect
196      */

197     public Line column(int column) {
198         if ((column < 0) || (column > this.getHeight())) {
199             return null;
200         }
201         ProxyForGroup tmp = null;
202         try {
203             tmp = new ProxyForGroup(this.getTypeName());
204         } catch (ConstructionOfReifiedObjectFailedException e) {
205             e.printStackTrace();
206         }
207         int begining = column;
208         for (int i = 0; i < this.getHeight(); i++) {
209             tmp.add(this.get(column + (i * this.getWidth())));
210         }
211         Line result = null;
212         try {
213             result = new Line((Group) tmp, this.getWidth());
214         } catch (ConstructionOfReifiedObjectFailedException e) {
215             e.printStackTrace();
216         }
217         return result;
218     }
219
220     /**
221      * Returns the column that contains the specified object
222      * @param o - the object
223      * @return the one-dimensional topology group formed by the column of the object in the two-dimensional topology group
224      */

225     public Line column(Object JavaDoc o) {
226         return this.column(this.getX(this.indexOf(o)));
227     }
228 }
229
Popular Tags