ca.odell.glazedlists.FreezableList Maven / Gradle / Ivy
Show all versions of glazedlists_java15 Show documentation
/* Glazed Lists (c) 2003-2006 */
/* http://publicobject.com/glazedlists/ publicobject.com,*/
/* O'Dell Engineering Ltd.*/
package ca.odell.glazedlists;
// the core Glazed Lists package
import ca.odell.glazedlists.event.ListEvent;
import java.util.ArrayList;
import java.util.List;
/**
* An {@link EventList} that shows the current contents of its source {@link EventList}.
*
* When this {@link EventList} is frozen, changes to its source {@link EventList}
* will not be reflected. Instead, the {@link FreezableList} will continue to show
* the state of its source {@link EventList} at the time it was frozen.
*
*
When this {@link EventList} is thawed, changes to its source
* {@link EventList} will be reflected.
*
*
Warning: This class is
* thread ready but not thread safe. See {@link EventList} for an example
* of thread safe code.
*
*
* EventList Overview
* Writable: writable when thawed (default), not writable when frozen
* Concurrency: thread ready, not thread safe
* Performance: reads: O(1), writes O(1), freezes O(N)
* Memory: frozen: 4 bytes per element, thawed: 0 bytes per element
* Unit Tests: N/A
* Issues: N/A
* Issues:
*
*
*
* @author Jesse Wilson
*/
public final class FreezableList extends TransformedList {
/** the state of the freezable list */
private boolean frozen = false;
/** the frozen objects */
private List frozenData = new ArrayList();
/**
* Creates a {@link FreezableList} that can freeze the view of the specified
* source {@link EventList}.
*/
public FreezableList(EventList source) {
super(source);
source.addListEventListener(this);
}
/** {@inheritDoc} */
@Override
public E get(int index) {
if(frozen) {
return frozenData.get(index);
} else {
return source.get(index);
}
}
/** {@inheritDoc} */
@Override
public int size() {
if(frozen) {
return frozenData.size();
} else {
return source.size();
}
}
/** {@inheritDoc} */
@Override
protected boolean isWritable() {
return !frozen;
}
/**
* Gets whether this {@link EventList} is showing a previous state of the source
* {@link EventList}.
*
* @return true if this list is showing a previous state of the source
* {@link EventList} or false if this is showing the current state
* of the source {@link EventList}.
*/
public boolean isFrozen() {
return frozen;
}
/**
* Locks this {@link FreezableList} on the current state of the source
* {@link EventList}. While frozen, changes to the source {@link EventList}
* will not be reflected by this list.
*
* Warning: This method is
* thread ready but not thread safe. See {@link EventList} for an example
* of thread safe code.
*/
public void freeze() {
if(frozen) throw new IllegalStateException("Cannot freeze a list that is already frozen");
// we are no longer interested in update events
source.removeListEventListener(this);
// copy the source array into the frozen list
frozenData.addAll(source);
// mark this list as frozen
frozen = true;
}
/**
* Unlocks this {@link FreezableList} to show the same contents of the source
* {@link EventList}. When thawed, changes to the source {@link EventList}
* will be reflected by this list.
*
*
Warning: This method is
* thread ready but not thread safe. See {@link EventList} for an example
* of thread safe code.
*/
public void thaw() {
if(!frozen) throw new IllegalStateException("Cannot thaw a list that is not frozen");
// prep events to listeners of the thaw
updates.beginEvent();
for(int i = 0, size = frozenData.size(); i < size; i++) {
updates.elementDeleted(0, frozenData.get(i));
}
for(int i = 0, size = source.size(); i < size; i++) {
updates.elementInserted(0, source.get(i));
}
// we don't need our frozen data anymore
frozenData.clear();
frozen = false;
// being listening to update events
source.addListEventListener(this);
// fire off the thaw event
updates.commitEvent();
}
/** {@inheritDoc} */
@Override
public void listChanged(ListEvent listChanges) {
if(frozen) {
// when a list change event arrives and this list is frozen,
// it is possible that the event was queued before this list
// was frozen. for this reason we do not throw any exceptions
// but instead silently ignore the event
} else {
// just pass on the changes
updates.forwardEvent(listChanges);
}
}
}