Start line:  
End line:  

Snippet Preview

Snippet HTML Code

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

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

Parameters:
<E> the type of items stored in memory by the InMemoryDbArrayList
Author(s):
Steve Lhomme
 
 public abstract class InMemoryDbArrayList<E, INSERT_ID> extends InMemoryDbList<E, ArrayList<E>, INSERT_ID> implements InMemoryFilteredAdapter.InMemoryFilter.InMemoryFilterable<E> {

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

Field to tell when the data are being reloaded from the DB
 
 	private boolean mIsLoading;

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 Database name for logs
logger The Logger to use for all logs (can be null for the default Android logs)
 
 	protected InMemoryDbArrayList(DataSource<E, INSERT_ID> dbString nameLogger logger) {
 		super(dbnameloggernull);
 	}

Parameters:
db The already created android.database.sqlite.SQLiteOpenHelper to use as storage
name Database name for logs
logger The Logger to use for all logs (can be null for the default Android logs)
initCookie Cookie to pass to AsynchronousDbHelper.preloadInit(java.lang.Object)
 
 	protected InMemoryDbArrayList(DataSource<E, INSERT_ID> dbString nameLogger loggerObject initCookie) {
 		super(dbnameloggerinitCookie);
 	}
 	
 	protected void preloadInit(Object cookie) {
 		 = new ReentrantLock();
 		super.preloadInit(cookie);
 		 = new ArrayList<E>();
 	}
 
 	protected ArrayList<E> getList() {
 		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 void startLoadingAllItems(int elementCount) {
		super.startLoadingAllItems(elementCount);
		getList().ensureCapacity(elementCount);
	}
	public boolean add(E item) {
		// protect the data coherence
		try {
			return super.add(item);
finally {
		}
	}
	public boolean addAll(Collection<? extends E> items) {
		// protect the data coherence
		try {
			return super.addAll(items);
finally {
		}
	}
	public boolean remove(E item) {
		// protect the data coherence
		try {
			return super.remove(item);
finally {
		}
	}
	public boolean remove(int index) {
		// protect the data coherence
		try {
			return super.remove(index);
finally {
		}
	}
	public void notifyItemChanged(E item) {
		// protect the data coherence
		try {
			super.notifyItemChanged(item);
finally {
		}
	}
	protected void clearDataInMemory() {
		// protect the data coherence
		try {
finally {
		}
	}
	public int getCount() {
		try {
			return null== ? 0 : .size();
finally {
		}
	}
	public E get(int position) {
		try {
			return super.get(position);
finally {
		}
	}
	public int indexOf(E item) {
		try {
			return super.indexOf(item);
finally {
		}
	}
	public E findItem(E similar) {
		try {
			return super.findItem(similar);
finally {
		}
	}
	public boolean replace(int index, E newData) {
		try {
			return super.replace(indexnewData);
finally {
		}
	}
	public boolean swap(int positionAint positionB) {
		try {
			return super.swap(positionApositionB);
finally {
		}
	}
	public void waitForDataLoaded() {
		try {
		    super.waitForDataLoaded();
finally {
		}
	}
	public java.util.List<E> getListCopy() {
		try {
			return Collections.unmodifiableList(new ArrayList<E>(getList()));
finally {
		}
	}
	public void onReplaceItemFailed(AsynchronousDbHelper<E, ?> db, E original, E replacementThrowable cause) {
		try {
			super.onReplaceItemFailed(dboriginalreplacementcause);
finally {
		}
	}
New to GrepCode? Check out our FAQ X