00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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 {
00044
00046 protected int height;
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 }