Skip to content

Commit 021ae2f

Browse files
committed
Rename JsonSerializer to Serializer and make it generic.
1 parent bf5bacd commit 021ae2f

File tree

3 files changed

+36
-42
lines changed

3 files changed

+36
-42
lines changed
Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,40 +13,35 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.fernandocejas.android10.sample.app.users.cache.serializer;
16+
package com.fernandocejas.android10.sample.app.data;
1717

18-
import com.fernandocejas.android10.sample.app.users.UserEntity;
1918
import com.google.gson.Gson;
2019
import javax.inject.Inject;
2120
import javax.inject.Singleton;
2221

2322
/**
24-
* Class user as Serializer/Deserializer for user entities.
23+
* Json Serializer/Deserializer.
2524
*/
2625
@Singleton
27-
public class JsonSerializer {
26+
public class Serializer {
2827

2928
private final Gson gson = new Gson();
3029

31-
@Inject
32-
JsonSerializer() {}
30+
@Inject Serializer() {}
3331

3432
/**
3533
* Serialize an object to Json.
36-
*
37-
* @param userEntity {@link UserEntity} to serialize.
34+
* @param object to serialize.
3835
*/
39-
public String serialize(UserEntity userEntity) {
40-
return gson.toJson(userEntity, UserEntity.class);
36+
public String serialize(Object object, Class clazz) {
37+
return gson.toJson(object, clazz);
4138
}
4239

4340
/**
4441
* Deserialize a json representation of an object.
45-
*
46-
* @param jsonString A json string to deserialize.
47-
* @return {@link UserEntity}
42+
* @param string A json string to deserialize.
4843
*/
49-
public UserEntity deserialize(String jsonString) {
50-
return gson.fromJson(jsonString, UserEntity.class);
44+
public <T> T deserialize(String string, Class<T> clazz) {
45+
return gson.fromJson(string, clazz);
5146
}
5247
}

app/src/main/java/com/fernandocejas/android10/sample/app/users/cache/UserCacheImpl.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
package com.fernandocejas.android10.sample.app.users.cache;
1717

1818
import android.content.Context;
19+
import com.fernandocejas.android10.sample.app.core.executor.ThreadExecutor;
1920
import com.fernandocejas.android10.sample.app.data.FileManager;
20-
import com.fernandocejas.android10.sample.app.users.cache.serializer.JsonSerializer;
21+
import com.fernandocejas.android10.sample.app.data.Serializer;
2122
import com.fernandocejas.android10.sample.app.users.UserEntity;
2223
import com.fernandocejas.android10.sample.app.users.UserNotFoundException;
23-
import com.fernandocejas.android10.sample.app.core.executor.ThreadExecutor;
2424
import java.io.File;
2525
import javax.inject.Inject;
2626
import javax.inject.Singleton;
@@ -40,35 +40,35 @@ public class UserCacheImpl implements UserCache {
4040

4141
private final Context context;
4242
private final File cacheDir;
43-
private final JsonSerializer serializer;
43+
private final Serializer serializer;
4444
private final FileManager fileManager;
4545
private final ThreadExecutor threadExecutor;
4646

4747
/**
4848
* Constructor of the class {@link UserCacheImpl}.
4949
*
5050
* @param context A
51-
* @param userCacheSerializer {@link JsonSerializer} for object serialization.
51+
* @param serializer {@link Serializer} for object serialization.
5252
* @param fileManager {@link FileManager} for saving serialized objects to the file system.
5353
*/
54-
@Inject
55-
public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
56-
FileManager fileManager, ThreadExecutor executor) {
57-
if (context == null || userCacheSerializer == null || fileManager == null || executor == null) {
54+
@Inject UserCacheImpl(Context context, Serializer serializer, FileManager fileManager,
55+
ThreadExecutor executor) {
56+
if (context == null || serializer == null || fileManager == null || executor == null) {
5857
throw new IllegalArgumentException("Invalid null parameter");
5958
}
6059
this.context = context.getApplicationContext();
6160
this.cacheDir = this.context.getCacheDir();
62-
this.serializer = userCacheSerializer;
61+
this.serializer = serializer;
6362
this.fileManager = fileManager;
6463
this.threadExecutor = executor;
6564
}
6665

6766
@Override public Observable<UserEntity> get(final int userId) {
6867
return Observable.create(subscriber -> {
69-
File userEntityFile = UserCacheImpl.this.buildFile(userId);
70-
String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
71-
UserEntity userEntity = UserCacheImpl.this.serializer.deserialize(fileContent);
68+
final File userEntityFile = UserCacheImpl.this.buildFile(userId);
69+
final String fileContent = UserCacheImpl.this.fileManager.readFileContent(userEntityFile);
70+
final UserEntity userEntity =
71+
UserCacheImpl.this.serializer.deserialize(fileContent, UserEntity.class);
7272

7373
if (userEntity != null) {
7474
subscriber.onNext(userEntity);
@@ -81,18 +81,17 @@ public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
8181

8282
@Override public void put(UserEntity userEntity) {
8383
if (userEntity != null) {
84-
File userEntityFile = this.buildFile(userEntity.getUserId());
84+
final File userEntityFile = this.buildFile(userEntity.getUserId());
8585
if (!isCached(userEntity.getUserId())) {
86-
String jsonString = this.serializer.serialize(userEntity);
87-
this.executeAsynchronously(new CacheWriter(this.fileManager, userEntityFile,
88-
jsonString));
86+
final String jsonString = this.serializer.serialize(userEntity, UserEntity.class);
87+
this.executeAsynchronously(new CacheWriter(this.fileManager, userEntityFile, jsonString));
8988
setLastCacheUpdateTimeMillis();
9089
}
9190
}
9291
}
9392

9493
@Override public boolean isCached(int userId) {
95-
File userEntityFile = this.buildFile(userId);
94+
final File userEntityFile = this.buildFile(userId);
9695
return this.fileManager.exists(userEntityFile);
9796
}
9897

@@ -133,7 +132,7 @@ private File buildFile(int userId) {
133132
* Set in millis, the last time the cache was accessed.
134133
*/
135134
private void setLastCacheUpdateTimeMillis() {
136-
long currentMillis = System.currentTimeMillis();
135+
final long currentMillis = System.currentTimeMillis();
137136
this.fileManager.writeToPreferences(this.context, SETTINGS_FILE_NAME,
138137
SETTINGS_KEY_LAST_CACHE_UPDATE, currentMillis);
139138
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.fernandocejas.android10.sample.app.users.cache.serializer;
16+
package com.fernandocejas.android10.sample.app.data;
1717

1818
import com.fernandocejas.android10.sample.app.ApplicationTestCase;
1919
import com.fernandocejas.android10.sample.app.users.UserEntity;
@@ -24,7 +24,7 @@
2424
import static org.hamcrest.CoreMatchers.is;
2525
import static org.junit.Assert.assertThat;
2626

27-
public class JsonSerializerTest extends ApplicationTestCase {
27+
public class SerializerTest extends ApplicationTestCase {
2828

2929
private static final String JSON_RESPONSE = "{\n"
3030
+ " \"id\": 1,\n"
@@ -35,27 +35,27 @@ public class JsonSerializerTest extends ApplicationTestCase {
3535
+ " \"email\": \"[email protected]\"\n"
3636
+ "}";
3737

38-
private JsonSerializer jsonSerializer;
38+
private Serializer serializer;
3939

4040
@Before
4141
public void setUp() {
42-
jsonSerializer = new JsonSerializer();
42+
serializer = new Serializer();
4343
}
4444

4545
@Test
4646
public void testSerializeHappyCase() {
47-
UserEntity userEntityOne = jsonSerializer.deserialize(JSON_RESPONSE);
48-
String jsonString = jsonSerializer.serialize(userEntityOne);
49-
UserEntity userEntityTwo = jsonSerializer.deserialize(jsonString);
47+
final UserEntity userEntityOne = serializer.deserialize(JSON_RESPONSE, UserEntity.class);
48+
final String jsonString = serializer.serialize(userEntityOne, UserEntity.class);
49+
final UserEntity userEntityTwo = serializer.deserialize(jsonString, UserEntity.class);
5050

5151
assertThat(userEntityOne.getUserId(), is(userEntityTwo.getUserId()));
5252
assertThat(userEntityOne.getFullname(), is(equalTo(userEntityTwo.getFullname())));
5353
assertThat(userEntityOne.getFollowers(), is(userEntityTwo.getFollowers()));
5454
}
5555

5656
@Test
57-
public void testDesearializeHappyCase() {
58-
UserEntity userEntity = jsonSerializer.deserialize(JSON_RESPONSE);
57+
public void testDeserializeHappyCase() {
58+
final UserEntity userEntity = serializer.deserialize(JSON_RESPONSE, UserEntity.class);
5959

6060
assertThat(userEntity.getUserId(), is(1));
6161
assertThat(userEntity.getFullname(), is("Simon Hill"));

0 commit comments

Comments
 (0)