Android Open Source - colibri Entries






From Project

Back to project page colibri.

License

The source code is released under:

Apache License

If you think the Android project colibri listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package labrom.colibri.cache;
/*  w  w  w .  ja va  2s.c  o  m*/
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.HashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import labrom.colibri.data.ActiveRecordList;
import labrom.colibri.data.Database;

/**
* @author Romain Laboisse labrom@gmail.com
*/
class Entries extends HashMap<String, CacheEntry> {

    private static final String COL_KEY = "key";
    private static final String COL_PATH = "file";
    private static final String WHERE_KEY = "key=?";
    private final Executor dbExecutor = Executors.newSingleThreadExecutor();

    private final Database db;

    Entries(Context ctx) {
        db = new Database(ctx) {
            @Override
            protected SQLiteOpenHelper createHelper(Context ctx) {
                return new SQLiteOpenHelper(ctx, CacheEntry.PROTO.getTableName(), null, 1) {
                    @Override
                    public void onCreate(SQLiteDatabase sqLiteDatabase) {
                        sqLiteDatabase.execSQL(String.format("CREATE TABLE %s (%s TEXT PRIMARY KEY NOT NULL, %s TEXT NOT NULL);", CacheEntry.PROTO.getTableName(), COL_KEY, COL_PATH));
                    }

                    @Override
                    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {

                    }
                };
            }
        };

        ActiveRecordList<CacheEntry> entries = db.query(CacheEntry.PROTO, null, null, null, null);
        for(CacheEntry entry : entries.asList()) {
            super.put(entry.key, entry);
        }
    }

    @Override
    public CacheEntry put(final String key, final CacheEntry value) {
        if(key == null || value == null) throw new NullPointerException();
        if(containsKey(key)) {
            dbExecutor.execute(new Runnable() {
                public void run() {
                    db.update(value);
                }
            });
        } else {
            dbExecutor.execute(new Runnable() {
                public void run() {
                    db.persist(value);
                }
            });

        }
        return super.put(key, value);
    }

    @Override
    public CacheEntry remove(final Object key) {
        if(key == null) throw new NullPointerException();
        final CacheEntry entry = new CacheEntry();
        entry.key = key.toString();
        dbExecutor.execute(new Runnable() {
            @Override
            public void run() {
                db.delete(entry);
            }
        });
        return super.remove(key);
    }
}




Java Source Code List

labrom.colibri.Util.java
labrom.colibri.cache.CacheEntry.java
labrom.colibri.cache.Cache.java
labrom.colibri.cache.Entries.java
labrom.colibri.data.ActiveContentProvider.java
labrom.colibri.data.ActiveRecordList.java
labrom.colibri.data.ActiveRecord.java
labrom.colibri.data.Database.java
labrom.colibri.data.SelectionArgs.java
labrom.colibri.xml.AttributeContext.java
labrom.colibri.xml.CursorDef.java
labrom.colibri.xml.CursorException.java
labrom.colibri.xml.Cursor.java
labrom.colibri.xml.CustomMap.java
labrom.colibri.xml.ElementContext.java
labrom.colibri.xml.FieldSetter.java
labrom.colibri.xml.Item.java
labrom.colibri.xml.NodeContext.java
labrom.colibri.xml.ObjectFactory.java
labrom.colibri.xml.ParserFactory.java
labrom.colibri.xml.ParserUtil.java
labrom.colibri.xml.PullTest.java
labrom.colibri.xml.ReflectorTest.java
labrom.colibri.xml.Reflector.java
labrom.colibri.xml.RootContext.java
labrom.colibri.xml.SetterTest.java
labrom.colibri.xml.StringToDateTransformer.java
labrom.colibri.xml.StringTransformer.java
labrom.colibri.xml.maps.Cursor4Maps.java
labrom.colibri.xml.maps.CursorDef4Maps.java
labrom.colibri.xml.maps.ElementContext4Maps.java
labrom.colibri.xml.maps.RootContext4Maps.java
labrom.colibri.xml.maps.package-info.java
labrom.colibri.xml.package-info.java