org/objectweb/proactive/core/group/topology/Plan.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 Plan extends Line { // implements Topology2D {
00044 
00046     protected int height; //  => Y => number of lines
00047 
00055     public Plan(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 Plan(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 Object left(Object o) {
00113         int pos = this.indexOf(o);
00114         if ((pos % this.getWidth()) == 0) {
00115             return null;
00116         } else {
00117             return this.get(pos - 1);
00118         }
00119     }
00120 
00126     public int getX(Object o) {
00127         return this.indexOf(o) % this.getWidth();
00128     }
00129 
00135     public int getY(Object o) {
00136         return this.indexOf(o) / this.getWidth();
00137     }
00138 
00144     public Object right(Object o) {
00145         int pos = this.indexOf(o);
00146         if ((pos % this.getWidth()) == (this.getWidth() - 1)) {
00147             return null;
00148         } else {
00149             return this.get(pos + 1);
00150         }
00151     }
00152 
00158     public Object up(Object o) {
00159         int pos = this.indexOf(o);
00160         if (pos < this.getWidth()) {
00161             return null;
00162         } else {
00163             return this.get(pos - this.getWidth());
00164         }
00165     }
00166 
00172     public Object down(Object o) {
00173         int pos = this.indexOf(o);
00174         if (pos > (((this.getHeight() - 1) * this.getWidth()) - 1)) {
00175             return null;
00176         } else {
00177             return this.get(pos + this.getWidth());
00178         }
00179     }
00180 
00186     public Line line(int line) {
00187         if ((line < 0) || (line > this.getWidth())) {
00188             return null;
00189         }
00190         ProxyForGroup tmp = null;
00191         try {
00192             tmp = new ProxyForGroup(this.getTypeName());
00193         } catch (ConstructionOfReifiedObjectFailedException e) {
00194             e.printStackTrace();
00195         }
00196         int begining = line * this.getWidth();
00197         for (int i = begining; i < (begining + this.getWidth()); i++) {
00198             tmp.add(this.get(i));
00199         }
00200         Line result = null;
00201         try {
00202             result = new Line((Group) tmp, this.getWidth());
00203         } catch (ConstructionOfReifiedObjectFailedException e) {
00204             e.printStackTrace();
00205         }
00206         return result;
00207     }
00208 
00214     public Line line(Object o) {
00215         return this.line(this.getY(this.indexOf(o)));
00216     }
00217 
00223     public Line column(int column) {
00224         if ((column < 0) || (column > this.getHeight())) {
00225             return null;
00226         }
00227         ProxyForGroup tmp = null;
00228         try {
00229             tmp = new ProxyForGroup(this.getTypeName());
00230         } catch (ConstructionOfReifiedObjectFailedException e) {
00231             e.printStackTrace();
00232         }
00233         for (int i = 0; i < this.getHeight(); i++) {
00234             tmp.add(this.get(column + (i * this.getWidth())));
00235         }
00236         Line result = null;
00237         try {
00238             result = new Line((Group) tmp, this.getHeight());
00239         } catch (ConstructionOfReifiedObjectFailedException e) {
00240             e.printStackTrace();
00241         }
00242         return result;
00243     }
00244 
00250     public Line column(Object o) {
00251         return this.column(this.getX(this.indexOf(o)));
00252     }
00253 }

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