1 31 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 46 public class Torus extends Ring { 48 49 protected int height; 51 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 72 protected Torus(Group g, int nbMembers) 73 throws ConstructionOfReifiedObjectFailedException { 74 super(g, nbMembers); 75 } 76 77 81 public int getWidth() { 82 return this.width; 83 } 84 85 89 public int getHeight() { 90 return this.height; 91 } 92 93 98 private int getX(int position) { 99 return position % this.width; 100 } 101 102 107 private int getY(int position) { 108 return position % this.height; 109 } 110 111 116 public int getX(Object o) { 117 return this.indexOf(o) % this.getWidth(); 118 } 119 120 125 public int getY(Object o) { 126 return this.indexOf(o) / this.getWidth(); 127 } 128 129 134 public Object left(Object 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 148 public Object right(Object 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 162 public Object up(Object 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 177 public Object down(Object 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 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 220 public Ring Ring(Object o) { 221 return this.Ring(this.getY(this.indexOf(o))); 222 } 223 224 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 257 public Ring column(Object o) { 258 return this.column(this.getX(this.indexOf(o))); 259 } 260 } 261 | Popular Tags |