Skip to content

Commit

Permalink
Merge pull request pilgr#37 from pilgr/development
Browse files Browse the repository at this point in the history
Version 1.5
  • Loading branch information
pilgr committed Apr 28, 2016
2 parents b3c6577 + e71788a commit c4cb0fb
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 30 deletions.
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Change Log
==========

Version 1.5 *(2016-04-28)*
----------------------------

* Save all the things! No more restriction to use classes only having no-arg constructor.
* Custom serializers can be added using `Paper.addSerializer()`.
* Kotlin is fully supported now, including saving `data class`es. Saving lambdas is not supported.


Version 1.1 *(2015-11-27)*
----------------------------

* New ```Paper.book().getAllKeys()``` api
* Proguard config for lib itself is included in aar.


Version 1.0 *(2015-09-15)*
----------------------------

* New multi-book API.
* 0.9 API is still supported and marked as deprecated.
* Unsafe possibility to write null values is disabled.

*NOTE:* Data storage format is unchanged. You can easily use files created within version 0.9.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
# Paper
[![Build Status](https://travis-ci.org/pilgr/Paper.svg?branch=master)](https://travis-ci.org/pilgr/Paper) [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-Paper-blue.svg?style=flat)](http://android-arsenal.com/details/1/2080)

Paper is a [fast](#benchmark-results) NoSQL data storage for Android that lets you save/restore Java objects by using efficient Kryo serialization and handling data structure changes automatically.
Paper is a [fast](#benchmark-results) NoSQL data storage for Android that lets you save/restore Java/Kotlin objects using efficient Kryo serialization. Object structure changes handled automatically.

![Paper icon](/paper_icon.png)

#### What's [new](/CHANGELOG.md) in 1.5
* Save all the things! No more restriction to use classes only having no-arg constructor.
* Custom serializers can be added using `Paper.addSerializer()`.
* Kotlin is fully supported now, including saving `data class`es. Obviously saving lambdas is not supported.

#### Add dependency
```groovy
compile 'io.paperdb:paperdb:1.1'
compile 'io.paperdb:paperdb:1.5'
```

#### Initialize Paper
Expand All @@ -20,7 +25,7 @@ Paper.init(context);
It's OK to call it in UI thread. All other methods should be used in background thread.

#### Save
Save data object. **Your custom classes must have no-arg constructor.**
Save data object.
Paper creates separate data file for each key.

```java
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.0'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.gradle.jvmargs=-XX:MaxPermSize=1024m -XX:+CMSClassUnloadingEnabled -XX:+HeapDumpOnOutOfMemoryError -Xmx2048m
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-bin.zip
28 changes: 28 additions & 0 deletions paperdb/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}

android {
compileSdkVersion 21
Expand Down Expand Up @@ -28,6 +42,18 @@ android {
packagingOptions {
exclude 'LICENSE.txt'
}
sourceSets {
test.java.srcDirs += 'src/androidTest/kotlin'
}
}

afterEvaluate {
android.sourceSets.all { sourceSet ->
if (!sourceSet.name.startsWith("androidTest"))
{
sourceSet.kotlin.setSrcDirs([])
}
}
}

dependencies {
Expand All @@ -37,6 +63,8 @@ dependencies {
androidTestCompile 'com.orhanobut:hawk:1.14'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.squareup.assertj:assertj-android:1.0.0'
androidTestCompile 'joda-time:joda-time:2.9.1'
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

apply from: '../publish.gradle'
8 changes: 4 additions & 4 deletions paperdb/src/androidTest/java/io/paperdb/DataTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import java.util.List;
import java.util.Map;

import io.paperdb.testdata.ClassWithoutPublicNoArgConstructor;
import io.paperdb.testdata.Person;
import io.paperdb.testdata.PersonArg;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static io.paperdb.testdata.TestDataGenerator.genPerson;
Expand Down Expand Up @@ -61,7 +61,7 @@ public void testPutMap() {

@Test
public void testPutPOJO() {
final Person person = genPerson(1);
final Person person = genPerson(new Person(), 1);
Paper.book().write("profile", person);

final Person savedPerson = Paper.book().read("profile");
Expand Down Expand Up @@ -134,9 +134,9 @@ public void testPutSynchronizedList() {
testReadWrite(Collections.synchronizedList(new ArrayList<>()));
}

@Test(expected = PaperDbException.class)
@Test()
public void testReadWriteClassWithoutNoArgConstructor() {
testReadWrite(new ClassWithoutPublicNoArgConstructor("constructor argument"));
testReadWrite(new PersonArg("name"));
}

private Object testReadWriteWithoutClassCheck(Object originObj) {
Expand Down
12 changes: 12 additions & 0 deletions paperdb/src/androidTest/java/io/paperdb/PaperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import android.support.test.runner.AndroidJUnit4;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.List;

import de.javakaffee.kryoserializers.jodatime.JodaDateTimeSerializer;
import io.paperdb.testdata.TestDataGenerator;

import static android.support.test.InstrumentationRegistry.getTargetContext;
import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -168,4 +172,12 @@ public void testGetAllKeys() {
assertThat(allKeys.contains("city2")).isTrue();
}

@Test
public void testCustomSerializer() {
Paper.addSerializer(DateTime.class, new JodaDateTimeSerializer());

DateTime now = DateTime.now(DateTimeZone.UTC);
Paper.book("custom").write("joda-datetime", now);
assertEquals(Paper.book("custom").read("joda-datetime"), now);
}
}
23 changes: 22 additions & 1 deletion paperdb/src/androidTest/java/io/paperdb/benchmark/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.paperdb.Paper;
import io.paperdb.testdata.Person;
import io.paperdb.testdata.PersonArg;
import io.paperdb.testdata.TestDataGenerator;

import static android.support.test.InstrumentationRegistry.getTargetContext;
Expand All @@ -39,7 +40,12 @@ public void testReadWrite500Contacts() throws Exception {
Hawk.clear();
long hawkTime = runTest(new HawkReadWriteContactsTest(), contacts, REPEAT_COUNT);

printResults("Read/write 500 contacts", paperTime, hawkTime);
final List<PersonArg> contactsArg = TestDataGenerator.genPersonArgList(500);
Paper.init(getTargetContext());
Paper.book().destroy();
long paperArg = runTest(new PaperReadWriteContactsArgTest(), contactsArg, REPEAT_COUNT);

printResults("Read/write 500 contacts", paperTime, hawkTime, paperArg);
}

@Test
Expand Down Expand Up @@ -77,6 +83,12 @@ private void printResults(String name, long paperTime, long hawkTime) {
name, paperTime, hawkTime));
}

private void printResults(String name, long paperTime, long hawkTime, long paperArgTime) {
Log.i(TAG, String.format("..................................\n%s " +
"\n Paper: %d \n Paper(arg-cons): %d \n Hawk: %d",
name, paperTime, paperArgTime, hawkTime));
}

private <T> long runTest(TestTask<T> task, T extra, int repeat) {
long start = SystemClock.uptimeMillis();
for (int i = 0; i < repeat; i++) {
Expand All @@ -98,6 +110,15 @@ public void run(int i, List<Person> extra) {
}
}

private class PaperReadWriteContactsArgTest implements TestTask<List<PersonArg>> {
@Override
public void run(int i, List<PersonArg> extra) {
String key = "contacts" + i;
Paper.book().write(key, extra);
Paper.book().<List<Person>>read(key);
}
}

private class HawkReadWriteContactsTest implements TestTask<List<Person>> {
@Override
public void run(int i, List<Person> extra) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
/**
* Tests deprecated put/get API
*/
@SuppressWarnings("deprecation")
@RunWith(AndroidJUnit4.class)
public class DataTest {

Expand Down Expand Up @@ -61,7 +62,7 @@ public void testPutMap() {

@Test
public void testPutPOJO() {
final Person person = genPerson(1);
final Person person = genPerson(new Person(), 1);
Paper.put("profile", person);

final Person savedPerson = Paper.get("profile");
Expand Down

This file was deleted.

36 changes: 36 additions & 0 deletions paperdb/src/androidTest/java/io/paperdb/testdata/PersonArg.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.paperdb.testdata;

import java.util.Arrays;

// Person + arg constructor
public class PersonArg extends Person {
public PersonArg(String name) {
super();
setName("changed" + name);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || PersonArg.class != o.getClass()) return false;

PersonArg person = (PersonArg) o;

if (getAge() != person.getAge()) return false;
if (!Arrays.equals(getBikes(), person.getBikes())) return false;
if (getName() != null ? !getName().equals(person.getName()) : person.getName() != null)
return false;
//noinspection RedundantIfStatement
if (getPhoneNumbers() != null
? !getPhoneNumbers().equals(person.getPhoneNumbers())
: person.getPhoneNumbers() != null)
return false;

return true;
}

@Override
public int hashCode() {
return super.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ public class TestDataGenerator {
public static List<Person> genPersonList(int size) {
List<Person> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
Person p = genPerson(i);
Person p = genPerson(new Person(), i);
list.add(p);
}
return list;
}

public static List<PersonArg> genPersonArgList(int size) {
List<PersonArg> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
PersonArg p = genPerson(new PersonArg("name"), i);
list.add(p);
}
return list;
}

@NonNull
public static Person genPerson(int i) {
Person p = new Person();
public static <T extends Person> T genPerson(T p, int i) {
p.setAge(i);
p.setBikes(new String[2]);
p.getBikes()[0] = "Kellys gen#" + i;
Expand Down
Loading

0 comments on commit c4cb0fb

Please sign in to comment.