mirror of
https://github.com/android10/Android-CleanArchitecture.git
synced 2026-03-13 10:13:41 +08:00
Rename JsonSerializer to Serializer and make it generic.
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
@@ -16,7 +16,7 @@
|
||||
package com.fernandocejas.android10.sample.data.cache;
|
||||
|
||||
import android.content.Context;
|
||||
import com.fernandocejas.android10.sample.data.cache.serializer.JsonSerializer;
|
||||
import com.fernandocejas.android10.sample.data.cache.serializer.Serializer;
|
||||
import com.fernandocejas.android10.sample.data.entity.UserEntity;
|
||||
import com.fernandocejas.android10.sample.data.exception.UserNotFoundException;
|
||||
import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
|
||||
@@ -39,7 +39,7 @@ public class UserCacheImpl implements UserCache {
|
||||
|
||||
private final Context context;
|
||||
private final File cacheDir;
|
||||
private final JsonSerializer serializer;
|
||||
private final Serializer serializer;
|
||||
private final FileManager fileManager;
|
||||
private final ThreadExecutor threadExecutor;
|
||||
|
||||
@@ -47,27 +47,27 @@ public class UserCacheImpl implements UserCache {
|
||||
* Constructor of the class {@link UserCacheImpl}.
|
||||
*
|
||||
* @param context A
|
||||
* @param userCacheSerializer {@link JsonSerializer} for object serialization.
|
||||
* @param serializer {@link Serializer} for object serialization.
|
||||
* @param fileManager {@link FileManager} for saving serialized objects to the file system.
|
||||
*/
|
||||
@Inject
|
||||
UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
|
||||
@Inject UserCacheImpl(Context context, Serializer serializer,
|
||||
FileManager fileManager, ThreadExecutor executor) {
|
||||
if (context == null || userCacheSerializer == null || fileManager == null || executor == null) {
|
||||
if (context == null || serializer == null || fileManager == null || executor == null) {
|
||||
throw new IllegalArgumentException("Invalid null parameter");
|
||||
}
|
||||
this.context = context.getApplicationContext();
|
||||
this.cacheDir = this.context.getCacheDir();
|
||||
this.serializer = userCacheSerializer;
|
||||
this.serializer = serializer;
|
||||
this.fileManager = fileManager;
|
||||
this.threadExecutor = executor;
|
||||
}
|
||||
|
||||
@Override public Observable<UserEntity> get(final int userId) {
|
||||
return Observable.create(subscriber -> {
|
||||
File userEntityFile = UserCacheImpl.this.buildFile(userId);
|
||||
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
|
||||
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
|
||||
final File userEntityFile = UserCacheImpl.this.buildFile(userId);
|
||||
final String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
|
||||
final UserEntity userEntity =
|
||||
UserCacheImpl.this.serializer.deserialize(fileContent, UserEntity.class);
|
||||
|
||||
if (userEntity != null) {
|
||||
subscriber.onNext(userEntity);
|
||||
@@ -80,11 +80,10 @@ public class UserCacheImpl implements UserCache {
|
||||
|
||||
@Override public void put(UserEntity userEntity) {
|
||||
if (userEntity != null) {
|
||||
File userEntityFile = this.buildFile(userEntity.getUserId());
|
||||
final File userEntityFile = this.buildFile(userEntity.getUserId());
|
||||
if (!isCached(userEntity.getUserId())) {
|
||||
String jsonString = this.serializer.serialize(userEntity);
|
||||
this.executeAsynchronously(new CacheWriter(this.fileManager, userEntityFile,
|
||||
jsonString));
|
||||
final String jsonString = this.serializer.serialize(userEntity, UserEntity.class);
|
||||
this.executeAsynchronously(new CacheWriter(this.fileManager, userEntityFile, jsonString));
|
||||
setLastCacheUpdateTimeMillis();
|
||||
}
|
||||
}
|
||||
@@ -132,7 +131,7 @@ public class UserCacheImpl implements UserCache {
|
||||
* Set in millis, the last time the cache was accessed.
|
||||
*/
|
||||
private void setLastCacheUpdateTimeMillis() {
|
||||
long currentMillis = System.currentTimeMillis();
|
||||
final long currentMillis = System.currentTimeMillis();
|
||||
this.fileManager.writeToPreferences(this.context, SETTINGS_FILE_NAME,
|
||||
SETTINGS_KEY_LAST_CACHE_UPDATE, currentMillis);
|
||||
}
|
||||
|
||||
@@ -15,38 +15,35 @@
|
||||
*/
|
||||
package com.fernandocejas.android10.sample.data.cache.serializer;
|
||||
|
||||
import com.fernandocejas.android10.sample.data.entity.UserEntity;
|
||||
import com.google.gson.Gson;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
/**
|
||||
* Class user as Serializer/Deserializer for user entities.
|
||||
* Json Serializer/Deserializer.
|
||||
*/
|
||||
@Singleton
|
||||
public class JsonSerializer {
|
||||
public class Serializer {
|
||||
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
@Inject
|
||||
JsonSerializer() {}
|
||||
@Inject Serializer() {}
|
||||
|
||||
/**
|
||||
* Serialize an object to Json.
|
||||
*
|
||||
* @param userEntity {@link UserEntity} to serialize.
|
||||
* @param object to serialize.
|
||||
*/
|
||||
public String serialize(UserEntity userEntity) {
|
||||
return gson.toJson(userEntity, UserEntity.class);
|
||||
public String serialize(Object object, Class clazz) {
|
||||
return gson.toJson(object, clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a json representation of an object.
|
||||
*
|
||||
* @param jsonString A json string to deserialize.
|
||||
* @return {@link UserEntity}
|
||||
* @param string A json string to deserialize.
|
||||
*/
|
||||
public UserEntity deserialize(String jsonString) {
|
||||
return gson.fromJson(jsonString, UserEntity.class);
|
||||
public <T> T deserialize(String string, Class<T> clazz) {
|
||||
return gson.fromJson(string, clazz);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class JsonSerializerTest extends ApplicationTestCase {
|
||||
public class SerializerTest extends ApplicationTestCase {
|
||||
|
||||
private static final String JSON_RESPONSE = "{\n"
|
||||
+ " \"id\": 1,\n"
|
||||
@@ -35,18 +35,18 @@ public class JsonSerializerTest extends ApplicationTestCase {
|
||||
+ " \"email\": \"jcooper@babbleset.edu\"\n"
|
||||
+ "}";
|
||||
|
||||
private JsonSerializer jsonSerializer;
|
||||
private Serializer serializer;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
jsonSerializer = new JsonSerializer();
|
||||
serializer = new Serializer();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSerializeHappyCase() {
|
||||
UserEntity userEntityOne = jsonSerializer.deserialize(JSON_RESPONSE);
|
||||
String jsonString = jsonSerializer.serialize(userEntityOne);
|
||||
UserEntity userEntityTwo = jsonSerializer.deserialize(jsonString);
|
||||
final UserEntity userEntityOne = serializer.deserialize(JSON_RESPONSE, UserEntity.class);
|
||||
final String jsonString = serializer.serialize(userEntityOne, UserEntity.class);
|
||||
final UserEntity userEntityTwo = serializer.deserialize(jsonString, UserEntity.class);
|
||||
|
||||
assertThat(userEntityOne.getUserId(), is(userEntityTwo.getUserId()));
|
||||
assertThat(userEntityOne.getFullname(), is(equalTo(userEntityTwo.getFullname())));
|
||||
@@ -55,7 +55,7 @@ public class JsonSerializerTest extends ApplicationTestCase {
|
||||
|
||||
@Test
|
||||
public void testDesearializeHappyCase() {
|
||||
UserEntity userEntity = jsonSerializer.deserialize(JSON_RESPONSE);
|
||||
final UserEntity userEntity = serializer.deserialize(JSON_RESPONSE, UserEntity.class);
|
||||
|
||||
assertThat(userEntity.getUserId(), is(1));
|
||||
assertThat(userEntity.getFullname(), is("Simon Hill"));
|
||||
Reference in New Issue
Block a user