KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * ################################################################
3  *
4  * ProActive: The Java(TM) library for Parallel, Distributed,
5  * Concurrent computing with Security and Mobility
6  *
7  * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8  * Contact: proactive-support@inria.fr
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  *
25  * Initial developer(s): The ProActive Team
26  * http://www.inria.fr/oasis/ProActive/contacts.html
27  * Contributor(s):
28  *
29  * ################################################################
30  */

31 /*
32  * Created on Mar 16, 2004
33  */

34 package org.objectweb.proactive.core.group.topology;
35
36 import org.objectweb.proactive.core.group.Group;
37 import org.objectweb.proactive.core.group.ProxyForGroup;
38 import org.objectweb.proactive.core.mop.ConstructionOfReifiedObjectFailedException;
39
40
41 /**
42  * This class represents a group by a cycling two-dimensional topology.
43  *
44  * @author Laurent Baduel
45  */

46 public class Torus extends Ring { // implements Topology2D {
47

48     /** height of the two-dimensional topology group */
49     protected int height; // => Y => number of Rings
50

51     /**
52      * Construtor. The members of <code>g</code> are used to fill the topology group.
53      * @param g - the group used a base for the new group (topology)
54      * @param height - the heigth of the two-dimensional topology group
55      * @param width - the width of the two-dimensional topology group
56      * @throws ConstructionOfReifiedObjectFailedException
57      */

58     public Torus(Group g, int height, int width)
59         throws ConstructionOfReifiedObjectFailedException {
60         super(g, height * width);
61         this.height = height;
62         this.width = width;
63     }
64
65     /**
66      * Construtor. The members of <code>g</code> are used to fill the topology group.
67      * @param g - the group used a base for the new group (topology)
68      * @param height - the heigth of the two-dimensional topology group
69      * @param width - the width of the two-dimensional topology group
70      * @throws ConstructionOfReifiedObjectFailedException
71      */

72     protected Torus(Group g, int nbMembers)
73         throws ConstructionOfReifiedObjectFailedException {
74         super(g, nbMembers);
75     }
76
77     /**
78      * Return the max size of the Ring
79      * @return the max size of the one-dimensional topology group (i.e. the Ring)
80      */

81     public int getWidth() {
82         return this.width;
83     }
84
85     /**
86      * Returns the height of the two-dimensional topology group (number of Rings)
87      * @return the height of the two-dimensional topology group
88      */

89     public int getHeight() {
90         return this.height;
91     }
92
93     /**
94      * Returns the coordonate X for the specified position, according to the dimension
95      * @param position
96      * @return the coordonate X
97      */

98     private int getX(int position) {
99         return position % this.width;
100     }
101
102     /**
103      * Returns the coordonate Y for the specified position, according to the dimension
104      * @param position
105      * @return the coordonate Y
106      */

107     private int getY(int position) {
108         return position % this.height;
109     }
110
111     /**
112      * Returns the X-coordonate of the specified object
113      * @param o - the object
114      * @return the position (in X) of the object in the Torus
115      */

116     public int getX(Object JavaDoc o) {
117         return this.indexOf(o) % this.getWidth();
118     }
119
120     /**
121      * Returns the Y-coordonate of the specified object
122      * @param o - the object
123      * @return the position (in Y) of the object in the Torus
124      */

125     public int getY(Object JavaDoc o) {
126         return this.indexOf(o) / this.getWidth();
127     }
128
129     /**
130      * Returns the object at the left of the specified object in the two-dimensional topology group
131      * @param o - the specified object
132      * @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>
133      */

134     public Object JavaDoc left(Object JavaDoc o) {
135         int positionX = this.getX(this.indexOf(o));
136         if (positionX != 0) {
137             return this.get(positionX - 1);
138         } else {
139             return this.get(positionX + (this.getWidth() - 1));
140         }
141     }
142
143     /**
144      * Returns the object at the right of the specified object in the two-dimensional topology group
145      * @param o - the specified object
146      * @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>
147      */

148     public Object JavaDoc right(Object JavaDoc o) {
149         int positionX = this.getX(this.indexOf(o));
150         if (positionX != this.getWidth()) {
151             return this.get(positionX + 1);
152         } else {
153             return this.get(positionX - (this.getWidth() - 1));
154         }
155     }
156
157     /**
158      * Returns the object at the up of the specified object in the two-dimensional topology group
159      * @param o - the specified object
160      * @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>
161      */

162     public Object JavaDoc up(Object JavaDoc o) {
163         int positionY = this.getY(this.indexOf(o));
164         if (positionY != 0) {
165             return this.get(positionY - this.getWidth());
166         } else {
167             return this.get(positionY +
168                 (this.getWidth() * (this.getHeight() - 1)));
169         }
170     }
171
172     /**
173      * Returns the object at the down of the specified object in the two-dimensional topology group
174      * @param o - the specified object
175      * @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>
176      */

177     public Object JavaDoc down(Object JavaDoc o) {
178         int position = this.getY(this.indexOf(o));
179         if (position != this.getHeight()) {
180             return this.get(position + this.getWidth());
181         } else {
182             return this.get(position -
183                 (this.getWidth() * (this.getHeight() - 1)));
184         }
185     }
186
187     /**
188      * Returns the Ring (one-dimensional topology group) with the specified number
189      * @param Ring - the number of the Ring
190      * @return the one-dimensional topology group formed by the Ring in the two-dimensional topology group, return <code>null</code> if the the specified Ring is incorrect
191      */

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

220     public Ring Ring(Object JavaDoc o) {
221         return this.Ring(this.getY(this.indexOf(o)));
222     }
223
224     /**
225      * Returns the column (one-dimensional topology group) with the specified number
226      * @param column - the number of the Ring
227      * @return the one-dimensional topology group formed by the column in the two-dimensional topology group, return <code>null</code> if the the specified Ring is incorrect
228      */

229     public Ring column(int column) {
230         if ((column < 0) || (column > this.getHeight())) {
231             return null;
232         }
233         ProxyForGroup tmp = null;
234         try {
235             tmp = new ProxyForGroup(this.getTypeName());
236         } catch (ConstructionOfReifiedObjectFailedException e) {
237             e.printStackTrace();
238         }
239         int begining = column;
240         for (int i = 0; i < this.getHeight(); i++) {
241             tmp.add(this.get(column + (i * this.getWidth())));
242         }
243         Ring result = null;
244         try {
245             result = new Ring((Group) tmp, this.getWidth());
246         } catch (ConstructionOfReifiedObjectFailedException e) {
247             e.printStackTrace();
248         }
249         return result;
250     }
251
252     /**
253      * Returns the column that contains the specified object
254      * @param o - the object
255      * @return the one-dimensional topology group formed by the column of the object in the two-dimensional topology group
256      */

257     public Ring column(Object JavaDoc o) {
258         return this.column(this.getX(this.indexOf(o)));
259     }
260 }
261
Popular Tags