All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ca.odell.glazedlists.FreezableList Maven / Gradle / Ivy

There is a newer version: 1.9.1
Show newest version
/* 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.*;
// an arraylist holds the frozen data
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. * *

* * * * * * * * *
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} */ public E get(int index) { if(frozen) { return frozenData.get(index); } else { return source.get(index); } } /** {@inheritDoc} */ public int size() { if(frozen) { return frozenData.size(); } else { return source.size(); } } /** {@inheritDoc} */ 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"); // mark this list as thawed frozen = false; int frozenDataSize = frozenData.size(); frozenData.clear(); // fire events to listeners of the thaw updates.beginEvent(); if(frozenDataSize > 0) updates.addDelete(0, frozenDataSize - 1); if(source.size() > 0) updates.addInsert(0, source.size() - 1); updates.commitEvent(); // being listening to update events source.addListEventListener(this); } /** {@inheritDoc} */ 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); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy