ca.odell.glazedlists.calculation.ElementAt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glazedlists_java15 Show documentation
Show all versions of glazedlists_java15 Show documentation
Event-driven lists for dynamically filtered and sorted tables
/* Glazed Lists (c) 2003-2007 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.calculation;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.event.ListEvent;
import ca.odell.glazedlists.event.ListEventListener;
/**
* Returns the element at the specified position in the list. If the size of
* the list is less than the position this Calculation returns a specified
* default value.
*
* @author Kevin Day
*/
final class ElementAt extends AbstractCalculation implements ListEventListener {
private final EventList source;
private final int index;
private final E defaultValue;
/**
* @param source the List whose element at position index
is returned
* @param index the position of the element in source
to be returned
* @param defaultValue the value of the calculation if the list does not contain index+1
entries
*/
public ElementAt(EventList source, int index, E defaultValue) {
super(source.size() > index ? source.get(index) : defaultValue);
this.source = source;
this.index = index;
this.defaultValue = defaultValue;
this.source.addListEventListener(this);
}
/** @inheritDoc */
public void dispose() {
source.removeListEventListener(this);
}
/** @inheritDoc */
public void listChanged(ListEvent listChanges) {
final E oldValue = getValue();
setValue(source.size() > index ? source.get(index) : defaultValue);
final E newValue = getValue();
fireValueChange(oldValue, newValue);
}
}