/** * Fuegt neues Item in die Liste ein, mit einem beliebigen Key * * <p>VB: key != null, element != null NB: true falls erfolgreich, false wenn nicht erfolgreich */ @Programmierer(autoren = "Alexander Tornoreanu") public boolean add(Object key, CollectionItem element) { Item current = this.root; // noch keine Element in der Liste, initialisiere root Element if (this.root == null) { this.root = new Item(key, element); } // mehrere Elemente in der Liste vorhanden else { while (current.next != null) { // wenn identisches Objekt bereits in Liste vorhanden ist, nichts einfuegen if (current.value == element) { return false; } current = current.next; } // nochmal ueberpruefen ob ein identisches Objekt bereits in Liste vorhanden ist, dann // nichts einfuegen if (current.value == element) { return false; } // Zeiger auf naechstes Element setzen current.next = new Item(key, element); } return true; }
/** * Loescht Objekt aus dem Set mithilfe eines Keys * * <p>VB: key != null NB: liefert true bei Erfolg, false bei nicht Erfolg */ @Programmierer(autoren = "Alexander Tornoreanu") public boolean removeValue(Object key) { Item help = list.root; Item before = help; if (key == null) return false; while (help != null) { if (help.key.toString().equals(key.toString())) { // remove root objekt if (help == before) { this.list.root = help.next; help = null; } else { // remove object before.next = help.next; } return true; } if (help != null) { before = help; help = help.next; } } return false; }
/** * Add an item to the end of the queue * * @param item the item to add */ public void add(E item) { Item i = new Item(item); if (head == null) head = tail = i; else { tail.next = i; tail = i; } size++; }
/** * Puts the given item in the constant pool's hash table. The hash table <i>must</i> not already * contains this item. * * @param i the item to be added to the constant pool's hash table. */ private void put(final Item i) { if (index > threshold) { Item[] newItems = new Item[items.length * 2 + 1]; for (int l = items.length - 1; l >= 0; --l) { Item j = items[l]; while (j != null) { int index = j.hashCode % newItems.length; Item k = j.next; j.next = newItems[index]; newItems[index] = j; j = k; } } items = newItems; threshold = (int) (items.length * 0.75); } int index = i.hashCode % items.length; i.next = items[index]; items[index] = i; }
@Test public void reverse() { Item elt3 = new Item(); elt3.id = 3; Item elt2 = new Item(); elt2.id = 2; elt2.next = elt3; Item head = new Item(); head.id = 1; head.next = elt2; Item newHead = Lists.reverse(head); Item tmp = newHead; int prev = 4; while (tmp != null) { assertTrue(tmp.id < prev); prev = tmp.id; tmp = tmp.next; } }
/** * Entfernt das aktuelle Element welches durchiteriert wird * * <p>NB: Entfernt ein Element */ @Override @Programmierer(autoren = "Alexander Tornoreanu") public void remove() { Item help = this.list.root; Item before = help; while (help != null) { if (help == this.lastValue) { if (help == before) { // Element am Listenanfang this.list.root = help.next; help = null; // aus Schleife raus } else { before.next = help.next; help = null; // aus Schleife raus } } if (help != null) { before = help; help = help.next; } } }
public void testRefresh() { ExtObjectContainer client1 = openNewSession(); ExtObjectContainer client2 = openNewSession(); Item item1 = retrieveInstance(client1); Item item2 = retrieveInstance(client2); Item next1 = item1; int value = 10; while (next1 != null) { Assert.areEqual(value, next1.getValue()); next1 = next1.next(); value--; } Item next2 = item2; value = 10; while (next2 != null) { Assert.areEqual(value, next2.getValue()); next2 = next2.next(); value--; } item1.setValue(100); item1.next().setValue(200); client1.store(item1, 2); client1.commit(); Assert.areEqual(100, item1.getValue()); Assert.areEqual(200, item1.next().getValue()); Assert.areEqual(10, item2.getValue()); Assert.areEqual(9, item2.next().getValue()); // refresh 0 client2.refresh(item2, 0); Assert.areEqual(10, item2.getValue()); Assert.areEqual(9, item2.next().getValue()); // refresh 1 client2.refresh(item2, 1); Assert.areEqual(100, item2.getValue()); Assert.areEqual(9, item2.next().getValue()); // refresh 2 client2.refresh(item2, 2); Assert.areEqual(100, item2.getValue()); // FIXME: maybe a bug // Assert.areEqual(200, item2.next().getValue()); next1 = item1; value = 1000; while (next1 != null) { next1.setValue(value); next1 = next1.next(); value++; } client1.store(item1, 5); client1.commit(); client2.refresh(item2, 5); next2 = item2; for (int i = 1000; i < 1005; i++) { Assert.areEqual(i, next2.getValue()); next2 = next2.next(); } client1.close(); client2.close(); }