Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted disk serializers to have direct access to streams #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ protected void onCreate(Bundle savedInstanceState) {
mRamCacheSize = getIntent().getIntExtra(EXTRA_RAM_CACHE_SIZE, 50);

CacheSerializer<String> jsonSerializer = new JsonSerializer<>(String.class);
DiskCacheSerializer<String> jsonDiskSerializer = new JsonDiskSerializer<>(String.class);

mCache = new Builder<String>(mCacheId, 1)
.enableLog()
.useSerializerInRam(mRamCacheSize, jsonSerializer)
.useSerializerInDisk(mDiskCacheSize, true, jsonSerializer, getApplicationContext())
.useSerializerInDisk(mDiskCacheSize, true, jsonDiskSerializer, getApplicationContext())
.build();

mHandler = new Handler();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.vincentbrison.openlibraries.android.dualcache;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
* Serializer which will serialize and deserialize object using <a href="https://github.com/FasterXML/jackson">Jackson</a>
* converter.
* @param <T> is the class of object to serialize/deserialize.
*/
public class JsonDiskSerializer<T> implements DiskCacheSerializer<T> {
private final ObjectMapper mapper;
private final Class<T> clazz;

/**
* Default constructor.
* @param clazz is the class of object to serialize/deserialize.
*/
public JsonDiskSerializer(Class<T> clazz) {
this.clazz = clazz;
mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
}

@Override
public T fromStream(InputStream data) {
try {
return mapper.readValue(data, clazz);
} catch (IOException e) {
e.printStackTrace();
}
throw new IllegalStateException();
}

@Override
public void writeToStream(OutputStream out, T object) {
try {
mapper.writeValue(out, object);
return;
} catch (IOException e) {
e.printStackTrace();
}
throw new IllegalStateException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import android.util.Log;

import com.vincentbrison.openlibraries.android.dualcache.CacheSerializer;
import com.vincentbrison.openlibraries.android.dualcache.DiskCacheSerializer;
import com.vincentbrison.openlibraries.android.dualcache.DualCache;
import com.vincentbrison.openlibraries.android.dualcache.DualCacheDiskMode;
import com.vincentbrison.openlibraries.android.dualcache.DualCacheRamMode;
import com.vincentbrison.openlibraries.android.dualcache.JsonDiskSerializer;
import com.vincentbrison.openlibraries.android.dualcache.JsonSerializer;
import com.vincentbrison.openlibraries.android.dualcache.SizeOf;
import com.vincentbrison.openlibraries.android.dualcache.IOUtil;
import com.vincentbrison.openlibraries.android.dualcache.lib.testobjects.AbstractVehicule;
import com.vincentbrison.openlibraries.android.dualcache.lib.testobjects.CoolBike;
import com.vincentbrison.openlibraries.android.dualcache.lib.testobjects.CoolCar;
Expand All @@ -20,12 +23,16 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.*;

@RunWith(AndroidJUnit4.class)
public abstract class DualCacheTest {
Expand All @@ -36,6 +43,7 @@ public abstract class DualCacheTest {
protected static final int TEST_APP_VERSION = 0;
protected DualCache<AbstractVehicule> cache;
protected CacheSerializer<AbstractVehicule> defaultCacheSerializer;
protected JsonDiskSerializer<AbstractVehicule> defaultDiskCacheSerializer;
private Context context;

protected Context getContext() {
Expand All @@ -45,6 +53,7 @@ protected Context getContext() {
@Before
public void setUp() throws Exception {
defaultCacheSerializer = new JsonSerializer<>(AbstractVehicule.class);
defaultDiskCacheSerializer = new JsonDiskSerializer<>(AbstractVehicule.class);
context = InstrumentationRegistry.getTargetContext();
}

Expand Down Expand Up @@ -255,6 +264,45 @@ public String toString(AbstractVehicule object) {
}
}

public static class DiskSerializerForTesting implements DiskCacheSerializer<AbstractVehicule> {
private static final Charset CHARSET = Charset.forName("UTF-8");

@Override
public AbstractVehicule fromStream(InputStream data) throws IOException {
char[] buffer = new char[1024];
InputStreamReader reader = new InputStreamReader(data, CHARSET);
StringBuilder builder = new StringBuilder();

try {
int numRead;
while ((numRead = reader.read(buffer)) != -1) {
builder.append(buffer, 0, numRead);
}
final String string = builder.toString();

if (string.equals(CoolBike.class.getSimpleName())) {
return new CoolBike();
} else if (string.equals(CoolCar.class.getSimpleName())) {
return new CoolCar();
} else {
return null;
}
} finally {
IOUtil.closeQuietly(reader);
}
}

@Override
public void writeToStream(OutputStream out, AbstractVehicule object) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(out, CHARSET);
try {
writer.write(object.getClass().getSimpleName());
} finally {
IOUtil.closeQuietly(writer);
}
}
}

public static class SizeOfVehiculeForTesting implements SizeOf<AbstractVehicule> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import com.vincentbrison.openlibraries.android.dualcache.Builder;
import com.vincentbrison.openlibraries.android.dualcache.CacheSerializer;
import com.vincentbrison.openlibraries.android.dualcache.DiskCacheSerializer;
import com.vincentbrison.openlibraries.android.dualcache.DualCache;
import com.vincentbrison.openlibraries.android.dualcache.JsonDiskSerializer;
import com.vincentbrison.openlibraries.android.dualcache.JsonSerializer;

import org.junit.After;
Expand All @@ -18,7 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;

/**
* Test issue 11.
Expand All @@ -36,10 +38,11 @@ public void setUp() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
File cacheDir = new File(context.getCacheDir(), CACHE_NAME);
CacheSerializer<String> jsonSerializer = new JsonSerializer<>(String.class);
DiskCacheSerializer<String> jsonDiskSerializer = new JsonDiskSerializer<>(String.class);
mCache = new Builder<String>(CACHE_NAME, 0)
.enableLog()
.useSerializerInRam(CACHE_RAM_ENTRIES, jsonSerializer)
.useSerializerInDisk(CACHE_SIZE, cacheDir, jsonSerializer)
.useSerializerInDisk(CACHE_SIZE, cacheDir, jsonDiskSerializer)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void setUp() throws Exception {
.useSerializerInDisk(
DISK_MAX_SIZE,
true,
new DualCacheTest.SerializerForTesting(),
new DualCacheTest.DiskSerializerForTesting(),
getContext())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.noRam()
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultCacheSerializer, getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultDiskCacheSerializer, getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useSerializerInRam(RAM_MAX_SIZE, new SerializerForTesting())
.useSerializerInDisk(DISK_MAX_SIZE, true, new SerializerForTesting(), getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, new DiskSerializerForTesting(), getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useSerializerInRam(RAM_MAX_SIZE, new SerializerForTesting())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultCacheSerializer, getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultDiskCacheSerializer, getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useSerializerInRam(RAM_MAX_SIZE, defaultCacheSerializer)
.useSerializerInDisk(DISK_MAX_SIZE, true, new SerializerForTesting(), getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, new DiskSerializerForTesting(), getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useSerializerInRam(RAM_MAX_SIZE, defaultCacheSerializer)
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultCacheSerializer, getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultDiskCacheSerializer, getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useReferenceInRam(RAM_MAX_SIZE, new SizeOfVehiculeForTesting())
.useSerializerInDisk(DISK_MAX_SIZE, true, new DualCacheTest.SerializerForTesting(), getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, new DualCacheTest.DiskSerializerForTesting(), getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void setUp() throws Exception {
cache = new Builder<AbstractVehicule>(CACHE_NAME, TEST_APP_VERSION)
.enableLog()
.useReferenceInRam(RAM_MAX_SIZE, new SizeOfVehiculeForTesting())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultCacheSerializer, getContext())
.useSerializerInDisk(DISK_MAX_SIZE, true, defaultDiskCacheSerializer, getContext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Builder<T> {
private SizeOf<T> sizeOf;
private int maxDiskSizeBytes;
private DualCacheDiskMode diskMode;
private CacheSerializer<T> diskSerializer;
private DiskCacheSerializer<T> diskSerializer;
private File diskFolder;

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ public Builder<T> noRam() {
public Builder<T> useSerializerInDisk(
int maxDiskSizeBytes,
boolean usePrivateFiles,
CacheSerializer<T> serializer,
DiskCacheSerializer<T> serializer,
Context context
) {
File folder = getDefaultDiskCacheFolder(usePrivateFiles, context);
Expand All @@ -172,7 +172,7 @@ public Builder<T> useSerializerInDisk(
* @return the builder.
*/
public Builder<T> useSerializerInDisk(
int maxDiskSizeBytes, File diskCacheFolder, CacheSerializer<T> serializer
int maxDiskSizeBytes, File diskCacheFolder, DiskCacheSerializer<T> serializer
) {
this.diskFolder = diskCacheFolder;
this.diskMode = DualCacheDiskMode.ENABLE_WITH_SPECIFIC_SERIALIZER;
Expand Down
Loading