Hallo,
ich habe wahrscheinlich eine etwas ungewöhnliche Implementatin von ListIterator, aber wollte einfach mal wissen, ob man das so machen kann oder wie es besser ist.
Also ich habe eine Klasse die hat ein Element _conditions als ArrayList. Die Klasse ArrayList stellt keine Methoden wie hasNext(), etc. bereit, daher wollte ich ListIterator implementieren und damit das Element _conditions durchlaufen. Gute Idee???
Jetzt habe ich noch ein Problem bei der Implementation. Und zwar arbeite ich mit einem _index Element. Das ist bestimmt auch nur suboptimal, aber naja. Jedenfalls, wenn ich mit add() Elemente hinzufüge, wird der index inkrementiert. Das mache ich, um die Java Spezifikation laut API zu ListIterator::add() einzuhalten.
Das Ding ist jetzt, habe ich erstmal eine Liste erstellt, also x-mal add() aufgerufen, dann bringt mir hasNext() natürlich false zurück, d.h. wiederum für mich, dass ich eine erstellte Liste jedesmal mit hasPrevious() beim ersten mal durchlaufen muss!
Macht das Sinn? Für mich nicht. Also wie implementiert man sowas korrekt? Hat jemand ein Beispiel?
PS: Eigentlich funktioniert meine Implementation, denke ich. Nur ist das Verhalten eben nicht allgemein erwartungsgemäß.
ich habe wahrscheinlich eine etwas ungewöhnliche Implementatin von ListIterator, aber wollte einfach mal wissen, ob man das so machen kann oder wie es besser ist.
Also ich habe eine Klasse die hat ein Element _conditions als ArrayList. Die Klasse ArrayList stellt keine Methoden wie hasNext(), etc. bereit, daher wollte ich ListIterator implementieren und damit das Element _conditions durchlaufen. Gute Idee???
Jetzt habe ich noch ein Problem bei der Implementation. Und zwar arbeite ich mit einem _index Element. Das ist bestimmt auch nur suboptimal, aber naja. Jedenfalls, wenn ich mit add() Elemente hinzufüge, wird der index inkrementiert. Das mache ich, um die Java Spezifikation laut API zu ListIterator::add() einzuhalten.
Das Ding ist jetzt, habe ich erstmal eine Liste erstellt, also x-mal add() aufgerufen, dann bringt mir hasNext() natürlich false zurück, d.h. wiederum für mich, dass ich eine erstellte Liste jedesmal mit hasPrevious() beim ersten mal durchlaufen muss!
Macht das Sinn? Für mich nicht. Also wie implementiert man sowas korrekt? Hat jemand ein Beispiel?
PS: Eigentlich funktioniert meine Implementation, denke ich. Nur ist das Verhalten eben nicht allgemein erwartungsgemäß.
Code:
private ArrayList<Condition> _conditions = new ArrayList<Condition>();
private int _index = 0;
// .....
/**
* Check if next() would return a {@see Condition}.
*
* @return
*/
public boolean hasNext() {
return this.getSize() > this._index ? true : false;
}
/**
* Next {@see Condition}.
*
* @return
* @throws java.util.NoSuchElementException
*/
public Object next() throws NoSuchElementException {
if (this.hasNext()) {
Condition returnCond = (Condition) this._conditions.get(this._index);
this._index++;
return returnCond;
} else {
throw new NoSuchElementException();
}
}
/**
* Check if previous() would return a {@see Condition}.
*
* @return
*/
public boolean hasPrevious() {
return 1 <= this._index ? true : false;
}
/**
* Previous {@see Condition}.
*
* @return
* @throws java.util.NoSuchElementException
*/
public Object previous() throws NoSuchElementException {
if (this.hasPrevious()) {
Condition returnCond = (Condition) this._conditions.get(--this._index);
return returnCond;
} else {
throw new NoSuchElementException();
}
}
/**
* Index of {@see Condition} would be returned by next(). Or list size if there is no next element.
*
* @return Index or list size if cursor is at end of list.
*/
public int nextIndex() {
if (this.hasNext()) {
return this._index;
} else {
return this.getSize();
}
}
/**
* Index of {@see Condition} would be returned by previous(). Or -1 if there is no previous element.
*
* @return Index or -1 if cursor is at beginning of the list.
*/
public int previousIndex() {
if (this._index == 0) {
return -1;
} else {
return this._index-1;
}
}
/**
* NOTE:
* Not supported yet.
*
* @throws java.lang.UnsupportedOperationException
*/
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* NOTE:
* Not supported yet.
*
* @param e
* @throws java.lang.UnsupportedOperationException
*/
public void set(Object e) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Not supported yet.");
}
/**
* Add {@see Condition} e to the list.
*
* NOTE:
* Argument "e" has to be an instance of {@see Condition}.
*
* @param e Instance of {@see Condition}. Otherwise exception will be thrown.
* @throws java.lang.IllegalArgumentException
*/
public void add(Object e) throws IllegalArgumentException {
if (e instanceof Condition) {
if (this._conditions.isEmpty()) {
this._conditions.add(0, (Condition) e);
} else {
this._conditions.add(this._index, (Condition) e);
}
this._index++;
} else {
throw new IllegalArgumentException();
}
}