1 23 24 package org.objectweb.medor.tuple.lib; 25 26 27 import org.objectweb.medor.clone.api.Cloneable; 28 import org.objectweb.medor.clone.lib.BasicCloneable; 29 import org.objectweb.medor.api.MedorException; 30 import org.objectweb.medor.api.TupleStructure; 31 import org.objectweb.medor.tuple.api.Tuple; 32 import org.objectweb.medor.tuple.api.TupleCollection; 33 34 import java.util.Collection ; 35 import java.util.Date ; 36 import java.util.Iterator ; 37 import java.util.LinkedList ; 38 import java.util.List ; 39 import java.util.Map ; 40 41 45 46 public class ExplicitTupleCollection 47 extends BasicCloneable 48 implements TupleCollection { 49 50 private TupleStructure schema; 51 private List tuples; 52 private int cursor; 53 boolean top = true; 54 boolean end = false; 55 boolean closed = false; 56 57 public ExplicitTupleCollection() { 58 } 59 60 public ExplicitTupleCollection(TupleStructure schema) throws MedorException { 61 this.schema = schema; 62 tuples = new LinkedList (); 63 cursor = 0; 64 } 65 66 public Object clone(Object clone, 67 Map obj2clone) throws CloneNotSupportedException { 68 clone = super.clone(clone, obj2clone); 69 ExplicitTupleCollection tc = (ExplicitTupleCollection) clone; 70 tc.closed = closed; 71 tc.end = end; 72 tc.top = top; 73 tc.cursor = cursor; 74 tc.schema = (TupleStructure) getClone(schema, obj2clone); 75 if (tuples != null) { 76 tuples = new LinkedList (); 77 for(Iterator it = tuples.iterator(); it.hasNext();) { 78 tc.tuples.add(getClone((Cloneable ) it.next(), obj2clone)); 79 } 80 } 81 return clone; 82 } 83 84 public void close() throws MedorException { 85 closed = true; 86 } 87 88 public synchronized TupleStructure getMetaData() throws MedorException { 89 if (closed) { 90 throw new MedorException("Impossible to use a closed TupleCollection"); 91 } 92 return schema; 93 } 94 95 public synchronized boolean isEmpty() throws MedorException { 96 if (closed) { 97 throw new MedorException("Impossible to use a closed TupleCollection"); 98 } 99 return (tuples.size() == 0); 100 } 101 102 103 public synchronized boolean isLast() throws MedorException { 104 if (closed) { 105 throw new MedorException("Impossible to use a closed TupleCollection"); 106 } 107 if ((cursor == -1) || (tuples.size() == 0)) 109 throw new MedorException(" No elements fetched in this tupleCollection"); 110 else 111 return (tuples.size() == getRow()); 112 } 113 114 115 public synchronized int size() { 116 return tuples.size(); 117 } 118 119 public synchronized boolean next() throws MedorException { 120 if (closed) { 121 throw new MedorException("Impossible to use a closed TupleCollection"); 122 } 123 if (!(tuples.size() == 0)) { if (cursor == tuples.size()) { end = true; return false; 127 } else if (top) { 128 cursor = 1; top = false; 130 return true; 131 } else { 132 cursor++; 133 return true; 134 } 135 } else 136 return false; 137 } 138 139 public synchronized boolean previous() throws MedorException { 140 boolean prev = true; 141 if (!isEmpty()) { 142 if (end) { 143 end = false; 144 prev = true; 145 } else if (getRow() == 1) { 146 top = true; 147 prev = false; 148 } else { 149 cursor--; 150 prev = true; 151 } 152 } else { 153 prev = false; 154 } 155 return prev; 156 } 157 158 public synchronized boolean row(int row) throws MedorException { 159 if (closed) { 160 throw new MedorException("Impossible to use a closed TupleCollection"); 161 } 162 if ((row >= 1) && (row <= tuples.size())) { 163 cursor = row; 164 return true; 165 } else 166 return false; 167 } 168 169 public synchronized void first() throws MedorException { 170 if (closed) { 171 throw new MedorException("Impossible to use a closed TupleCollection"); 172 } 173 if (!isEmpty()) { 174 end = false; 175 top = false; 176 cursor = 1; 177 } 178 } 179 180 public synchronized int getRow() throws MedorException { 181 if (closed) { 182 throw new MedorException("Impossible to use a closed TupleCollection"); 183 } 184 if (isEmpty() || top || end) 185 return -1; 186 else 187 return cursor; 188 } 189 190 196 public synchronized void insertTuple(int i, Tuple T) throws MedorException { 197 if ((i >= 1) && (i <= size())) 198 tuples.add((i - 1), T); 199 else 200 throw new MedorException("its impossible to insert in this cursor: " + i); 201 } 202 203 208 public synchronized void insertTuple(Tuple T) throws MedorException { 209 tuples.add(T); 210 } 211 212 217 public synchronized void removeTuple(int row) throws MedorException { 218 if ((row >= 1) && (row <= size())) { 219 tuples.remove(row - 1); 220 if (tuples.size() == 0) { 221 end = false; 222 cursor = -1; 223 } else 224 cursor = row - 1; 225 } else 226 throw new MedorException("its impossible to remove from this cursor"); 227 } 228 229 236 public synchronized Tuple getTuple(int i) throws MedorException { 237 if (closed) { 238 throw new MedorException("Impossible to use a closed TupleCollection"); 239 } 240 if (i > 0 && i <= tuples.size()) { 241 return (Tuple) tuples.get(i - 1); 242 } else 243 throw new MedorException("its impossible de get tuple from this cursor " + i); 244 245 } 246 247 public synchronized Tuple getTuple() throws MedorException { 248 if (closed) { 249 throw new MedorException("Impossible to use a closed TupleCollection"); 250 } 251 if ((!isEmpty()) && (!top) && (!end)) { 252 return (Tuple) tuples.get(cursor - 1); 253 254 } else 255 throw new MedorException("No elements fetched in this tupleCollection"); 256 } 257 258 265 public synchronized boolean getBoolean(int i) throws MedorException { 266 if (!isEmpty() && !top && !end) { 267 return this.getTuple().getBoolean(i); 268 } else 269 throw new MedorException("No elements fetched in this tupleCollection"); 270 } 271 272 279 public synchronized byte getByte(int i) throws MedorException { 280 if (!isEmpty() && !top && !end) { 281 return this.getTuple().getByte(i); 282 } else 283 throw new MedorException("No elements fetched in this tupleCollection"); 284 } 285 286 293 public synchronized Date getDate(int i) throws MedorException { 294 if (!isEmpty() && !top && !end) { 295 return this.getTuple().getDate(i); 296 } else 297 throw new MedorException("No elements fetched in this tupleCollection"); 298 } 299 300 307 public synchronized double getDouble(int i) throws MedorException { 308 if (!isEmpty() && !top && !end) { 309 return this.getTuple().getInt(i); 310 } else 311 throw new MedorException("No elements fetched in this tupleCollection"); 312 } 313 314 321 public synchronized float getFloat(int i) throws MedorException { 322 if (!isEmpty() && !top && !end) { 323 return this.getTuple().getFloat(i); 324 } else 325 throw new MedorException("No elements fetched in this tupleCollection"); 326 } 327 328 335 public synchronized int getInt(int i) throws MedorException { 336 if (!isEmpty() && !top && !end) { 337 return this.getTuple().getInt(i); 338 } else 339 throw new MedorException("No elements fetched in this tupleCollection"); 340 } 341 342 349 public synchronized char getChar(int i) throws MedorException { 350 if (!isEmpty() && !top && !end) { 351 return this.getTuple().getChar(i); 352 } else 353 throw new MedorException("No elements fetched in this tupleCollection"); 354 } 355 356 363 public synchronized long getLong(int i) throws MedorException { 364 if (!isEmpty() && !top && !end) { 365 return this.getTuple().getLong(i); 366 } else 367 throw new MedorException("No elements fetched in this tupleCollection"); 368 } 369 370 377 public synchronized short getShort(int i) throws MedorException { 378 if (!isEmpty() && !top && !end) { 379 return this.getTuple().getShort(i); 380 } else 381 throw new MedorException("No elements fetched in this tupleCollection"); 382 } 383 384 391 public synchronized String getString(int i) throws MedorException { 392 if (!isEmpty() && !top && !end) { 393 return this.getTuple().getString(i); 394 } else 395 throw new MedorException("No elements fetched in this tupleCollection"); 396 } 397 398 public synchronized void setBoolean(boolean x, int i) throws MedorException { 399 if (!isEmpty() && !top && !end) { 401 Tuple t = this.getTuple(); 402 MemoryTuple upletI = (MemoryTuple) t; 403 upletI.setBoolean(x, i); 404 } else 405 throw new MedorException("No elements fetched in this tupleCollection"); 406 } 407 408 409 public synchronized void setByte(byte x, int i) throws MedorException { 410 if (!isEmpty() && !top && !end) { 411 Tuple t = this.getTuple(); 412 MemoryTuple upletI = (MemoryTuple) t; 413 upletI.setByte(x, i); 414 } else 415 throw new MedorException("No elements fetched in this tupleCollection"); 416 } 417 418 public synchronized void setDate(Date x, int i) throws MedorException { 419 if (!isEmpty() && !top && !end) { 420 Tuple t = this.getTuple(); 421 MemoryTuple upletI = (MemoryTuple) t; 422 upletI.setDate(x, i); 423 } else 424 throw new MedorException("No elements fetched in this tupleCollection"); 425 } 426 427 public synchronized void setDouble(double x, int i) throws MedorException { 428 if (!isEmpty() && !top && !end) { 429 Tuple t = this.getTuple(); 430 MemoryTuple upletI = (MemoryTuple) t; 431 upletI.setDouble(x, i); 432 tuples.add(i, upletI); 433 } else 434 throw new MedorException("No elements fetched in this tupleCollection"); 435 } 436 437 public synchronized void setFloat(float x, int i) throws MedorException { 438 if (!isEmpty() && !top && !end) { 439 Tuple t = this.getTuple(); 440 MemoryTuple upletI = (MemoryTuple) t; 441 upletI.setFloat(x, i); 442 } else 443 throw new MedorException("No elements fetched in this tupleCollection"); 444 } 445 446 447 public synchronized void setInt(int x, int i) throws MedorException { 448 if (!isEmpty() && !top && !end) { 449 Tuple t = this.getTuple(); 450 MemoryTuple upletI = (MemoryTuple) t; 451 upletI.setInt(x, i); 452 } else 453 throw new MedorException("No elements fetched in this tupleCollection"); 454 } 455 456 public synchronized void setShort(short x, int i) throws MedorException { 457 if (!isEmpty() && !top && !end) { 458 Tuple t = this.getTuple(); 459 MemoryTuple upletI = (MemoryTuple) t; 460 upletI.setShort(x, i); 461 } else 462 throw new MedorException("No elements fetched in this tupleCollection"); 463 } 464 465 public synchronized void setString(String x, int i) throws MedorException { 466 if (!isEmpty() && !top && !end) { 467 Tuple t = this.getTuple(); 468 MemoryTuple upletI = (MemoryTuple) t; 469 upletI.setString(x, i); 470 } else 471 throw new MedorException("No elements fetched in this tupleCollection"); 472 } 473 474 477 478 public synchronized boolean contains(Tuple T) { 479 return tuples.contains(T); 480 } 481 482 public synchronized void display() throws MedorException { 483 TupleCollectionView frame = new TupleCollectionView(this); 484 frame.pack(); 485 frame.setVisible(true); 486 } 487 488 public synchronized Iterator iteratorOf(int i) throws MedorException { 489 Object o = getTuple().getObject(i); 490 if (o instanceof Collection) { 491 Collection c = (Collection) o; 492 return c.iterator(); 493 } else { 494 throw new MedorException("the Attribute number " + i + " is not a collection Object"); 495 } 496 } 497 498 public synchronized Iterator iteratorAll() throws MedorException { 499 return tuples.iterator(); 500 } 501 } | Popular Tags |