org/objectweb/proactive/core/group/topology/Torus.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.group.topology;
00032 
00033 import org.objectweb.proactive.core.group.Group;
00034 import org.objectweb.proactive.core.group.ProxyForGroup;
00035 import org.objectweb.proactive.core.mop.ConstructionOfReifiedObjectFailedException;
00036 
00037 
00043 public class Torus extends Ring { // implements Topology2D {
00044 
00046     protected int height; //  => Y => number of Rings
00047 
00055     public Torus(Group g, int height, int width)
00056         throws ConstructionOfReifiedObjectFailedException {
00057         super(g, height * width);
00058         this.height = height;
00059         this.width = width;
00060     }
00061 
00068     protected Torus(Group g, int nbMembers)
00069         throws ConstructionOfReifiedObjectFailedException {
00070         super(g, nbMembers);
00071     }
00072 
00077     public int getWidth() {
00078         return this.width;
00079     }
00080 
00085     public int getHeight() {
00086         return this.height;
00087     }
00088 
00094     private int getX(int position) {
00095         return position % this.width;
00096     }
00097 
00103     private int getY(int position) {
00104         return position % this.height;
00105     }
00106 
00112     public int getX(Object o) {
00113         return this.indexOf(o) % this.getWidth();
00114     }
00115 
00121     public int getY(Object o) {
00122         return this.indexOf(o) / this.getWidth();
00123     }
00124 
00130     public Ring Ring(int Ring) {
00131         if ((Ring < 0) || (Ring > this.getWidth())) {
00132             return null;
00133         }
00134         ProxyForGroup tmp = null;
00135         try {
00136             tmp = new ProxyForGroup(this.getTypeName());
00137         } catch (ConstructionOfReifiedObjectFailedException e) {
00138             e.printStackTrace();
00139         }
00140         int begining = Ring * this.getWidth();
00141         for (int i = begining; i < (begining + this.getWidth()); i++) {
00142             tmp.add(this.get(i));
00143         }
00144         Ring result = null;
00145         try {
00146             result = new Ring((Group) tmp, this.getWidth());
00147         } catch (ConstructionOfReifiedObjectFailedException e) {
00148             e.printStackTrace();
00149         }
00150         return result;
00151     }
00152 
00158     public Ring Ring(Object o) {
00159         return this.Ring(this.getY(this.indexOf(o)));
00160     }
00161 
00167     public Ring column(int column) {
00168         if ((column < 0) || (column > this.getHeight())) {
00169             return null;
00170         }
00171         ProxyForGroup tmp = null;
00172         try {
00173             tmp = new ProxyForGroup(this.getTypeName());
00174         } catch (ConstructionOfReifiedObjectFailedException e) {
00175             e.printStackTrace();
00176         }
00177         int begining = column;
00178         for (int i = 0; i < this.getHeight(); i++) {
00179             tmp.add(this.get(column + (i * this.getWidth())));
00180         }
00181         Ring result = null;
00182         try {
00183             result = new Ring((Group) tmp, this.getWidth());
00184         } catch (ConstructionOfReifiedObjectFailedException e) {
00185             e.printStackTrace();
00186         }
00187         return result;
00188     }
00189 
00195     public Ring column(Object o) {
00196         return this.column(this.getX(this.indexOf(o)));
00197     }
00198 
00204     public Object left(Object o) {
00205         int pos = this.indexOf(o);
00206         if ((pos % this.getWidth()) == 0) {
00207             return this.get(pos + (this.width - 1));
00208         } else {
00209             return this.get(pos - 1);
00210         }
00211     }
00212 
00218     public Object right(Object o) {
00219         int pos = this.indexOf(o);
00220         if ((pos % this.getWidth()) == (this.getWidth() - 1)) {
00221             return this.get(pos - (this.width - 1));
00222         } else {
00223             return this.get(pos + 1);
00224         }
00225     }
00226 
00232     public Object up(Object o) {
00233         int pos = this.indexOf(o);
00234         if (pos < this.getWidth()) {
00235             return this.get(pos + ((this.height - 1) * this.width));
00236         } else {
00237             return this.get(pos - this.getWidth());
00238         }
00239     }
00240 
00246     public Object down(Object o) {
00247         int pos = this.indexOf(o);
00248         if (pos > (((this.getHeight() - 1) * this.getWidth()) - 1)) {
00249             return this.get(pos - ((this.height - 1) * this.width));
00250         } else {
00251             return this.get(pos + this.getWidth());
00252         }
00253     }
00254 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1