// Path: library/src/org/gawst/asyncdb/adapter/UIHandler.java
// public class UIHandler extends Handler {
// 
// 	public UIHandler() {
// 		super(Looper.getMainLooper());
// 	}
// 
// 	/**
// 	 * Run the {@code Runnable} in the UI thread
// 	 * @param r The Runnable that will be executed.
// 	 */
// 	public void runOnUiThread(Runnable r) {
// 		if (isUIThread())
// 			r.run();
// 		else
// 			post(r);
// 	}
// 
// 	/**
// 	 * Indicates the current thread is the UI thread
// 	 * @return {@code true} if the current Thread is the UI thread
// 	 */
// 	public static boolean isUIThread() {
// 		return Thread.currentThread()==Looper.getMainLooper().getThread();
// 	}
// 
// 	/**
// 	 * Assert that we are running in the UI thread
// 	 */
// 	public static void assertUIThread() throws IllegalThreadStateException {
// 		if (!isUIThread())
// 			throw new IllegalThreadStateException();
// 	}
// 
// 	/**
// 	 * Assert that we are NOT running in the UI thread
// 	 */
// 	public static void assertNotUIThread() throws IllegalThreadStateException {
// 		if (isUIThread())
// 			throw new IllegalThreadStateException();
// 	}
// }
// 
// Path: library/src/org/gawst/asyncdb/purge/PurgeHandler.java
// public interface PurgeHandler {
// 	/**
// 	 * Called anytime an element(s) is added to the database.
// 	 * <p>Called in the database worker thread.</p>
// 	 * @param db The {@link org.gawst.asyncdb.AsynchronousDbHelper} to purge.
// 	 */
// 	void onElementsAdded(AsynchronousDbHelper<?, ?> db);
// }


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import org.gawst.asyncdb.adapter.UIHandler;
import org.gawst.asyncdb.purge.PurgeHandler;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
package org.gawst.asyncdb;




/**
 * the main helper class that saves/restore item in memory using a DB storage
 * <p/>
 * the storage handling is done in a separate thread
 *
 * @param <E> the type of items stored in memory
 * @author Steve Lhomme
 * @see AsyncDbHelperHandler
 * @see org.gawst.asyncdb.AsynchronousDatabase
 */
public abstract class AsynchronousDbHelper<E, INSERT_ID> implements DataSource.BatchReadingCallback<E> {

	public final static String TAG = "MemoryDb";
	protected final static String STARTUP_TAG = "Startup";
	protected final static boolean DEBUG_DB = false;

	private static final int MSG_LOAD_IN_MEMORY    = 100;
	private static final int MSG_STORE_ITEM        = 101;
	private static final int MSG_STORE_ITEMS       = 102;
	private static final int MSG_REMOVE_ITEM       = 103;
	private static final int MSG_UPDATE_ITEM       = 104;
	private static final int MSG_CLEAR_DATABASE    = 105;
	private static final int MSG_SWAP_ITEMS        = 106;
	private static final int MSG_REPLACE_ITEM      = 107;
	private static final int MSG_CUSTOM_OPERATION  = 108;

// Path: library/src/org/gawst/asyncdb/adapter/UIHandler.java
// public class UIHandler extends Handler {
// 
// 	public UIHandler() {
// 		super(Looper.getMainLooper());
// 	}
// 
// 	/**
// 	 * Run the {@code Runnable} in the UI thread
// 	 * @param r The Runnable that will be executed.
// 	 */
// 	public void runOnUiThread(Runnable r) {
// 		if (isUIThread())
// 			r.run();
// 		else
// 			post(r);
// 	}
// 
// 	/**
// 	 * Indicates the current thread is the UI thread
// 	 * @return {@code true} if the current Thread is the UI thread
// 	 */
// 	public static boolean isUIThread() {
// 		return Thread.currentThread()==Looper.getMainLooper().getThread();
// 	}
// 
// 	/**
// 	 * Assert that we are running in the UI thread
// 	 */
// 	public static void assertUIThread() throws IllegalThreadStateException {
// 		if (!isUIThread())
// 			throw new IllegalThreadStateException();
// 	}
// 
// 	/**
// 	 * Assert that we are NOT running in the UI thread
// 	 */
// 	public static void assertNotUIThread() throws IllegalThreadStateException {
// 		if (isUIThread())
// 			throw new IllegalThreadStateException();
// 	}
// }
// 
// Path: library/src/org/gawst/asyncdb/purge/PurgeHandler.java
// public interface PurgeHandler {
// 	/**
// 	 * Called anytime an element(s) is added to the database.
// 	 * <p>Called in the database worker thread.</p>
// 	 * @param db The {@link org.gawst.asyncdb.AsynchronousDbHelper} to purge.
// 	 */
// 	void onElementsAdded(AsynchronousDbHelper<?, ?> db);
// }


// Path: library/src/org/gawst/asyncdb/AsynchronousDbHelper.java
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import org.gawst.asyncdb.adapter.UIHandler;
import org.gawst.asyncdb.purge.PurgeHandler;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

package org.gawst.asyncdb;




/**
 * the main helper class that saves/restore item in memory using a DB storage
 * <p/>
 * the storage handling is done in a separate thread
 *
 * @param <E> the type of items stored in memory
 * @author Steve Lhomme
 * @see AsyncDbHelperHandler
 * @see org.gawst.asyncdb.AsynchronousDatabase
 */
public abstract class AsynchronousDbHelper<E, INSERT_ID> implements DataSource.BatchReadingCallback<E> {

	public final static String TAG = "MemoryDb";
	protected final static String STARTUP_TAG = "Startup";
	protected final static boolean DEBUG_DB = false;

	private static final int MSG_LOAD_IN_MEMORY    = 100;
	private static final int MSG_STORE_ITEM        = 101;
	private static final int MSG_STORE_ITEMS       = 102;
	private static final int MSG_REMOVE_ITEM       = 103;
	private static final int MSG_UPDATE_ITEM       = 104;
	private static final int MSG_CLEAR_DATABASE    = 105;
	private static final int MSG_SWAP_ITEMS        = 106;
	private static final int MSG_REPLACE_ITEM      = 107;
	private static final int MSG_CUSTOM_OPERATION  = 108;

	private static final UIHandler UI_HANDLER = new UIHandler();