forked from anupcowkur/Reservoir
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3ae43a7
Showing
35 changed files
with
934 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#built files | ||
build/ | ||
out/ | ||
gen/ | ||
|
||
# Local configuration file (sdk path, etc) | ||
local.properties | ||
|
||
# IntelliJ Idea/Android Studio files | ||
*.iml | ||
*.ipr | ||
*.iws | ||
.idea/ | ||
|
||
# Mac system files | ||
.DS_* | ||
|
||
#proguard | ||
proguard_logs/ | ||
|
||
#gradle | ||
.gradle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
} | ||
dependencies { | ||
classpath 'com.android.tools.build:gradle:0.8.+' | ||
} | ||
} | ||
apply plugin: 'android-library' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
android { | ||
compileSdkVersion 19 | ||
buildToolsVersion "19.0.1" | ||
|
||
defaultConfig { | ||
minSdkVersion 7 | ||
targetSdkVersion 19 | ||
} | ||
} | ||
|
||
dependencies { | ||
compile fileTree(dir: 'libs', include: '*.jar') | ||
} | ||
|
||
task clearJar(type: Delete) { | ||
delete 'build/libs/reservoir-1.0.jar' | ||
} | ||
|
||
task makeJar(type: Copy) { | ||
from('build/bundles/debug/') | ||
into('build/libs/') | ||
include('classes.jar') | ||
rename ('classes.jar', 'reservoir-1.0.jar') | ||
} | ||
|
||
makeJar.dependsOn(clearJar, build) |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.anupcowkur.reservoir" | ||
android:versionCode="1" | ||
android:versionName="1.0"> | ||
|
||
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19" /> | ||
|
||
<application android:allowBackup="true" | ||
android:label="@string/app_name" | ||
android:icon="@drawable/ic_launcher"> | ||
|
||
</application> | ||
|
||
</manifest> |
175 changes: 175 additions & 0 deletions
175
Library/src/main/java/com/anupcowkur/reservoir/Reservoir.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package com.anupcowkur.reservoir; | ||
|
||
import android.content.Context; | ||
import android.os.AsyncTask; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* The main reservoir class. | ||
*/ | ||
public class Reservoir { | ||
|
||
private static SimpleDiskCache cache; | ||
|
||
/** | ||
* Initialize Reservoir | ||
* | ||
* @param context context. | ||
* @param maxSize the maximum size in bytes. | ||
*/ | ||
public static synchronized boolean init(Context context, long maxSize) { | ||
try { | ||
cache = SimpleDiskCache.open(context.getFilesDir(), 1, maxSize); | ||
return true; | ||
} catch (IOException e) { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Check if an object with the given key exists in the Reservoir. | ||
* | ||
* @param key they key string. | ||
* @return true if object with given key exists, false if object doesn't exist or an | ||
* exception occurs during disk read. | ||
*/ | ||
public static boolean contains(String key) throws IOException { | ||
|
||
return cache.contains(key); | ||
} | ||
|
||
/** | ||
* Put an object into Reservoir with the given key. This a blocking IO operation. Previously | ||
* stored object with the same | ||
* key (if any) will be overwritten. | ||
* | ||
* @param key they key string. | ||
* @param object the object to be stored. | ||
*/ | ||
public static void put(String key, Object object) throws IOException { | ||
String json = new Gson().toJson(object); | ||
cache.put(key, json); | ||
} | ||
|
||
/** | ||
* Put an object into Reservoir with the given key asynchronously. Previously | ||
* stored object with the same | ||
* key (if any) will be overwritten. | ||
* | ||
* @param key they key string. | ||
* @param object the object to be stored. | ||
* @param callback a callback of type {@link com.anupcowkur.reservoirsample | ||
* .ReservoirPutCallback} which is called upon completion. | ||
*/ | ||
public static void putAsync(String key, Object object, | ||
ReservoirPutCallback callback) { | ||
|
||
new PutTask(key, object, callback).execute(); | ||
} | ||
|
||
/** | ||
* Get an object from Reservoir with the given key. This a blocking IO operation. | ||
* | ||
* @param key they key string. | ||
* @param classOfT the Class type of the expected return object. | ||
* @return the object of the given type if it exists, null if object doesn't exist. | ||
*/ | ||
public static <T> T get(String key, Class<T> classOfT) throws IOException { | ||
try { | ||
String json = cache.getString(key).getString(); | ||
return new Gson().fromJson(json, classOfT); | ||
} catch (NullPointerException e) { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Get an object from Reservoir with the given key asynchronously. | ||
* | ||
* @param key they key string. | ||
* @param callback a callback of type {@link com.anupcowkur.reservoirsample | ||
* .ReservoirGetCallback} which is called upon completion. | ||
*/ | ||
public static <T> void getAsync(String key, Class<T> classOfT, | ||
ReservoirGetCallback<T> callback) { | ||
new GetTask<T>(key, classOfT, callback).execute(); | ||
} | ||
|
||
/** | ||
* AsyncTask to perform put operation in a background thread. | ||
*/ | ||
private static class PutTask extends AsyncTask<Void, Void, Void> { | ||
|
||
private final String key; | ||
private Exception e; | ||
private final ReservoirPutCallback callback; | ||
final Object object; | ||
|
||
private PutTask(String key, Object object, ReservoirPutCallback callback) { | ||
this.key = key; | ||
this.callback = callback; | ||
this.object = object; | ||
this.e = null; | ||
} | ||
|
||
@Override | ||
protected Void doInBackground(Void... params) { | ||
try { | ||
String json = new Gson().toJson(object); | ||
cache.put(key, json); | ||
} catch (Exception e) { | ||
this.e = e; | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(Void aVoid) { | ||
if (callback != null) { | ||
callback.onComplete(e); | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* AsyncTask to perform get operation in a background thread. | ||
*/ | ||
private static class GetTask<T> extends AsyncTask<Void, Void, T> { | ||
|
||
private final String key; | ||
private final ReservoirGetCallback callback; | ||
private final Class<T> classOfT; | ||
private Exception e; | ||
|
||
private GetTask(String key, Class<T> classOfT, ReservoirGetCallback callback) { | ||
this.key = key; | ||
this.callback = callback; | ||
this.classOfT = classOfT; | ||
this.e = null; | ||
} | ||
|
||
@Override | ||
protected T doInBackground(Void... params) { | ||
try { | ||
String json = cache.getString(key).getString(); | ||
return new Gson().fromJson(json, classOfT); | ||
} catch (Exception e) { | ||
this.e = e; | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPostExecute(T object) { | ||
if (callback != null) { | ||
callback.onComplete(e, object); | ||
} | ||
} | ||
|
||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Library/src/main/java/com/anupcowkur/reservoir/ReservoirGetCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.anupcowkur.reservoir; | ||
|
||
/** | ||
* | ||
*/ | ||
public interface ReservoirGetCallback<T> { | ||
public void onComplete(Exception e, T object); | ||
} |
8 changes: 8 additions & 0 deletions
8
Library/src/main/java/com/anupcowkur/reservoir/ReservoirPutCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.anupcowkur.reservoir; | ||
|
||
/** | ||
* | ||
*/ | ||
public interface ReservoirPutCallback { | ||
public void onComplete(Exception e); | ||
} |
Oops, something went wrong.