com.avaje.ebean.common.ModifyIterator Maven / Gradle / Ivy
package com.avaje.ebean.common;
import java.util.Iterator;
import com.avaje.ebean.bean.BeanCollection;
/**
* Wraps an iterator for the purposes of notifying removals and additions to the
* BeanCollection owner.
*
* This is required for persisting ManyToMany objects. Additions and removals
* become inserts and deletes to the intersection table.
*
*/
class ModifyIterator implements Iterator {
private final BeanCollection owner;
private final Iterator it;
private E last;
/**
* Create with an Owner and the underlying Iterator this wraps.
*
* The owner is notified of the removals.
*
*/
ModifyIterator(BeanCollection owner, Iterator it) {
this.owner = owner;
this.it = it;
}
public boolean hasNext() {
return it.hasNext();
}
public E next() {
last = it.next();
return last;
}
public void remove() {
owner.modifyRemoval(last);
it.remove();
}
}