Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.gawst.asyncdb;
  
  import java.util.TreeSet;
a basic helper class to keep the content of a flat database in an java.util.TreeSet

you should use the mDataLock when iterating through the java.util.TreeSet

Parameters:
<E> the type of items stored in memory by the InMemoryDbTreeSet
Author(s):
Steve Lhomme
 
 public abstract class InMemoryDbTreeSet<E, INSERT_ID> extends InMemoryDbSet<E, TreeSet<E>, INSERT_ID> {

The array where the data are stored, locked when writing on it
 
 	private TreeSet<E> mData;

Field to tell when the data are being reloaded from the DB
 
 	private boolean mIsLoading;
 
 	private final Comparator<E> comparator;

ReentrantLock used to protect mData when reading/writing/iterating it
 
 	protected ReentrantLock mDataLock;

Condition to block the mData access before the data are loaded
 
 	private Condition dataLoaded;

Parameters:
db The already created android.database.sqlite.SQLiteOpenHelper to use as storage
name of the database file, or null for an in-memory database
logger the Logger to use for all logs (can be null for the default Android logs)
comparator comparator to sort the elements
 
 	protected InMemoryDbTreeSet(DataSource<E, INSERT_ID> dbString nameLogger loggerComparator<E> comparator) {
 		this(dbnameloggercomparatornull);
 	}

Parameters:
db
name of the database file, or null for an in-memory database
logger the Logger to use for all logs (can be null for the default Android logs)
comparator comparator to sort the elements
initCookie Cookie to pass to AsynchronousDbHelper.preloadInit(java.lang.Object)
 
 	protected InMemoryDbTreeSet(DataSource<E, INSERT_ID> dbString nameLogger loggerComparator<E> comparatorObject initCookie) {
 		super(dbnameloggerinitCookie);
 		this. = comparator;
 	}
 
 	protected void preloadInit(Object cookie) {
 		 = new ReentrantLock();
 		super.preloadInit(cookie);
 		 = new TreeSet<E>();
 	}
 
 	protected TreeSet<E> getSet() {
 		if (!.isHeldByCurrentThread()) throw new IllegalStateException("we need a lock on mDataLock to access mData in "+this);
 		if (!isDataLoaded() && !)
 			try {
 				// we're trying to read the data but they are not loading yet
 				..v("waiting data loaded in "+this);
 				long now = System.currentTimeMillis();
 				..v("waiting data loaded in "+this+" finished after "+(System.currentTimeMillis()-now));
 			} catch (InterruptedException ignored) {
 				..w("timed out waiting for data loaded in "+this);
 			}
 		return ;
 	}
 
 	protected void startLoadingInMemory() {
 		 = true;
 	}
 
 	protected void finishLoadingInMemory() {
		 = false;
	}
	public boolean contains(E object) {
		// protect the data coherence
		try {
			return super.contains(object);
finally {
		}
	}
	public boolean add(E item) {
		// protect the data coherence
		try {
			return super.add(item);
finally {
		}
	}
	public boolean replace(E item) {
		// protect the data coherence
		try {
			return super.replace(item);
finally {
		}
	}
	public boolean remove(E item) {
		// protect the data coherence
		try {
			return super.remove(item);
finally {
		}
	}
	public boolean removeAll(Collection<E> collection) {
		// protect the data coherence
		try {
			return super.removeAll(collection);
finally {
		}
	}
	protected void clearDataInMemory() {
		// protect the data coherence
		try {
finally {
		}
	}
	public int size() {
		try {
			return null== ? 0 : .size();
finally {
		}
	}
	public E get(int position) {
		try {
			return super.get(position);
finally {
		}
	}
	public void waitForDataLoaded() {
		try {
		    super.waitForDataLoaded();
finally {
		}
	}
	public java.util.List<E> getListCopy() {
		try {
			return Collections.unmodifiableList(new ArrayList<E>(getSet()));
finally {
		}
	}
New to GrepCode? Check out our FAQ X