Skip to content

Commit 38f47c8

Browse files
committed
- Minor Refactor.
- Improve class scoping.
1 parent 6e59ee8 commit 38f47c8

File tree

21 files changed

+51
-152
lines changed

21 files changed

+51
-152
lines changed

build.gradle

-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ allprojects {
2323
}
2424
}
2525

26-
task wrapper(type: Wrapper) {
27-
description 'Creates the gradle wrapper.'
28-
gradleVersion '2.12'
29-
}
30-
3126
task runDomainUnitTests(dependsOn: [':domain:test']) {
3227
description 'Run unit tests for the domain layer.'
3328
}

data/src/main/java/com/fernandocejas/android10/sample/data/cache/FileManager.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public class FileManager {
3333

3434
@Inject
35-
public FileManager() {}
35+
FileManager() {}
3636

3737
/**
3838
* Writes a file to Disk.
@@ -41,7 +41,7 @@ public FileManager() {}
4141
*
4242
* @param file The file to write to Disk.
4343
*/
44-
public void writeToFile(File file, String fileContent) {
44+
void writeToFile(File file, String fileContent) {
4545
if (!file.exists()) {
4646
try {
4747
FileWriter writer = new FileWriter(file);
@@ -61,15 +61,15 @@ public void writeToFile(File file, String fileContent) {
6161
* @param file The file to read from.
6262
* @return A string with the content of the file.
6363
*/
64-
public String readFileContent(File file) {
64+
String readFileContent(File file) {
6565
StringBuilder fileContentBuilder = new StringBuilder();
6666
if (file.exists()) {
6767
String stringLine;
6868
try {
6969
FileReader fileReader = new FileReader(file);
7070
BufferedReader bufferedReader = new BufferedReader(fileReader);
7171
while ((stringLine = bufferedReader.readLine()) != null) {
72-
fileContentBuilder.append(stringLine + "\n");
72+
fileContentBuilder.append(stringLine).append("\n");
7373
}
7474
bufferedReader.close();
7575
fileReader.close();
@@ -87,7 +87,7 @@ public String readFileContent(File file) {
8787
* @param file The file to check existence.
8888
* @return true if this file exists, false otherwise.
8989
*/
90-
public boolean exists(File file) {
90+
boolean exists(File file) {
9191
return file.exists();
9292
}
9393

@@ -98,7 +98,7 @@ public boolean exists(File file) {
9898
*
9999
* @param directory The directory which its content will be deleted.
100100
*/
101-
public boolean clearDirectory(File directory) {
101+
boolean clearDirectory(File directory) {
102102
boolean result = false;
103103
if (directory.exists()) {
104104
for (File file : directory.listFiles()) {
@@ -116,7 +116,7 @@ public boolean clearDirectory(File directory) {
116116
* @param key A string for the key that will be used to retrieve the value in the future.
117117
* @param value A long representing the value to be inserted.
118118
*/
119-
public void writeToPreferences(Context context, String preferenceFileName, String key,
119+
void writeToPreferences(Context context, String preferenceFileName, String key,
120120
long value) {
121121

122122
SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName,
@@ -134,7 +134,7 @@ public void writeToPreferences(Context context, String preferenceFileName, Strin
134134
* @param key A key that will be used to retrieve the value from the preference file.
135135
* @return A long representing the value retrieved from the preferences file.
136136
*/
137-
public long getFromPreferences(Context context, String preferenceFileName, String key) {
137+
long getFromPreferences(Context context, String preferenceFileName, String key) {
138138
SharedPreferences sharedPreferences = context.getSharedPreferences(preferenceFileName,
139139
Context.MODE_PRIVATE);
140140
return sharedPreferences.getLong(key, 0);

data/src/main/java/com/fernandocejas/android10/sample/data/cache/UserCacheImpl.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public class UserCacheImpl implements UserCache {
5151
* @param fileManager {@link FileManager} for saving serialized objects to the file system.
5252
*/
5353
@Inject
54-
public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
54+
UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
5555
FileManager fileManager, ThreadExecutor executor) {
5656
if (context == null || userCacheSerializer == null || fileManager == null || executor == null) {
5757
throw new IllegalArgumentException("Invalid null parameter");
@@ -91,8 +91,8 @@ public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
9191
}
9292

9393
@Override public boolean isCached(int userId) {
94-
File userEntitiyFile = this.buildFile(userId);
95-
return this.fileManager.exists(userEntitiyFile);
94+
final File userEntityFile = this.buildFile(userId);
95+
return this.fileManager.exists(userEntityFile);
9696
}
9797

9898
@Override public boolean isExpired() {
@@ -119,7 +119,7 @@ public UserCacheImpl(Context context, JsonSerializer userCacheSerializer,
119119
* @return A valid file.
120120
*/
121121
private File buildFile(int userId) {
122-
StringBuilder fileNameBuilder = new StringBuilder();
122+
final StringBuilder fileNameBuilder = new StringBuilder();
123123
fileNameBuilder.append(this.cacheDir.getPath());
124124
fileNameBuilder.append(File.separator);
125125
fileNameBuilder.append(DEFAULT_FILE_NAME);

data/src/main/java/com/fernandocejas/android10/sample/data/cache/serializer/JsonSerializer.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@ public class JsonSerializer {
2929
private final Gson gson = new Gson();
3030

3131
@Inject
32-
public JsonSerializer() {}
32+
JsonSerializer() {}
3333

3434
/**
3535
* Serialize an object to Json.
3636
*
3737
* @param userEntity {@link UserEntity} to serialize.
3838
*/
3939
public String serialize(UserEntity userEntity) {
40-
String jsonString = gson.toJson(userEntity, UserEntity.class);
41-
return jsonString;
40+
return gson.toJson(userEntity, UserEntity.class);
4241
}
4342

4443
/**
@@ -48,7 +47,6 @@ public String serialize(UserEntity userEntity) {
4847
* @return {@link UserEntity}
4948
*/
5049
public UserEntity deserialize(String jsonString) {
51-
UserEntity userEntity = gson.fromJson(jsonString, UserEntity.class);
52-
return userEntity;
50+
return gson.fromJson(jsonString, UserEntity.class);
5351
}
5452
}

data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityDataMapper.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
public class UserEntityDataMapper {
3232

3333
@Inject
34-
public UserEntityDataMapper() {}
34+
UserEntityDataMapper() {}
3535

3636
/**
3737
* Transform a {@link UserEntity} into an {@link User}.
@@ -49,7 +49,6 @@ public User transform(UserEntity userEntity) {
4949
user.setFollowers(userEntity.getFollowers());
5050
user.setEmail(userEntity.getEmail());
5151
}
52-
5352
return user;
5453
}
5554

@@ -60,15 +59,13 @@ public User transform(UserEntity userEntity) {
6059
* @return {@link User} if valid {@link UserEntity} otherwise null.
6160
*/
6261
public List<User> transform(Collection<UserEntity> userEntityCollection) {
63-
List<User> userList = new ArrayList<>(20);
64-
User user;
62+
final List<User> userList = new ArrayList<>(20);
6563
for (UserEntity userEntity : userEntityCollection) {
66-
user = transform(userEntity);
64+
final User user = transform(userEntity);
6765
if (user != null) {
6866
userList.add(user);
6967
}
7068
}
71-
7269
return userList;
7370
}
7471
}

data/src/main/java/com/fernandocejas/android10/sample/data/entity/mapper/UserEntityJsonMapper.java

+4-18
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ public UserEntityJsonMapper() {
4343
* @throws com.google.gson.JsonSyntaxException if the json string is not a valid json structure.
4444
*/
4545
public UserEntity transformUserEntity(String userJsonResponse) throws JsonSyntaxException {
46-
try {
47-
Type userEntityType = new TypeToken<UserEntity>() {}.getType();
48-
UserEntity userEntity = this.gson.fromJson(userJsonResponse, userEntityType);
49-
50-
return userEntity;
51-
} catch (JsonSyntaxException jsonException) {
52-
throw jsonException;
53-
}
46+
final Type userEntityType = new TypeToken<UserEntity>() {}.getType();
47+
return this.gson.fromJson(userJsonResponse, userEntityType);
5448
}
5549

5650
/**
@@ -62,15 +56,7 @@ public UserEntity transformUserEntity(String userJsonResponse) throws JsonSyntax
6256
*/
6357
public List<UserEntity> transformUserEntityCollection(String userListJsonResponse)
6458
throws JsonSyntaxException {
65-
66-
List<UserEntity> userEntityCollection;
67-
try {
68-
Type listOfUserEntityType = new TypeToken<List<UserEntity>>() {}.getType();
69-
userEntityCollection = this.gson.fromJson(userListJsonResponse, listOfUserEntityType);
70-
71-
return userEntityCollection;
72-
} catch (JsonSyntaxException jsonException) {
73-
throw jsonException;
74-
}
59+
final Type listOfUserEntityType = new TypeToken<List<UserEntity>>() {}.getType();
60+
return this.gson.fromJson(userListJsonResponse, listOfUserEntityType);
7561
}
7662
}

data/src/main/java/com/fernandocejas/android10/sample/data/exception/NetworkConnectionException.java

-8
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ public NetworkConnectionException() {
2424
super();
2525
}
2626

27-
public NetworkConnectionException(final String message) {
28-
super(message);
29-
}
30-
31-
public NetworkConnectionException(final String message, final Throwable cause) {
32-
super(message, cause);
33-
}
34-
3527
public NetworkConnectionException(final Throwable cause) {
3628
super(cause);
3729
}

data/src/main/java/com/fernandocejas/android10/sample/data/exception/RepositoryErrorBundle.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
/**
2121
* Wrapper around Exceptions used to manage errors in the repository.
2222
*/
23-
public class RepositoryErrorBundle implements ErrorBundle {
23+
class RepositoryErrorBundle implements ErrorBundle {
2424

2525
private final Exception exception;
2626

27-
public RepositoryErrorBundle(Exception exception) {
27+
RepositoryErrorBundle(Exception exception) {
2828
this.exception = exception;
2929
}
3030

data/src/main/java/com/fernandocejas/android10/sample/data/exception/UserNotFoundException.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -19,20 +19,7 @@
1919
* Exception throw by the application when a User search can't return a valid result.
2020
*/
2121
public class UserNotFoundException extends Exception {
22-
2322
public UserNotFoundException() {
2423
super();
2524
}
26-
27-
public UserNotFoundException(final String message) {
28-
super(message);
29-
}
30-
31-
public UserNotFoundException(final String message, final Throwable cause) {
32-
super(message, cause);
33-
}
34-
35-
public UserNotFoundException(final Throwable cause) {
36-
super(cause);
37-
}
3825
}

data/src/main/java/com/fernandocejas/android10/sample/data/executor/JobExecutor.java

+7-27
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
*/
1616
package com.fernandocejas.android10.sample.data.executor;
1717

18+
import android.support.annotation.NonNull;
1819
import com.fernandocejas.android10.sample.domain.executor.ThreadExecutor;
19-
import java.util.concurrent.BlockingQueue;
2020
import java.util.concurrent.LinkedBlockingQueue;
2121
import java.util.concurrent.ThreadFactory;
2222
import java.util.concurrent.ThreadPoolExecutor;
@@ -29,43 +29,23 @@
2929
*/
3030
@Singleton
3131
public class JobExecutor implements ThreadExecutor {
32-
33-
private static final int INITIAL_POOL_SIZE = 3;
34-
private static final int MAX_POOL_SIZE = 5;
35-
36-
// Sets the amount of time an idle thread waits before terminating
37-
private static final int KEEP_ALIVE_TIME = 10;
38-
39-
// Sets the Time Unit to seconds
40-
private static final TimeUnit KEEP_ALIVE_TIME_UNIT = TimeUnit.SECONDS;
41-
42-
private final BlockingQueue<Runnable> workQueue;
43-
4432
private final ThreadPoolExecutor threadPoolExecutor;
4533

46-
private final ThreadFactory threadFactory;
47-
4834
@Inject
49-
public JobExecutor() {
50-
this.workQueue = new LinkedBlockingQueue<>();
51-
this.threadFactory = new JobThreadFactory();
52-
this.threadPoolExecutor = new ThreadPoolExecutor(INITIAL_POOL_SIZE, MAX_POOL_SIZE,
53-
KEEP_ALIVE_TIME, KEEP_ALIVE_TIME_UNIT, this.workQueue, this.threadFactory);
35+
JobExecutor() {
36+
this.threadPoolExecutor = new ThreadPoolExecutor(3, 5, 10, TimeUnit.SECONDS,
37+
new LinkedBlockingQueue<>(), new JobThreadFactory());
5438
}
5539

56-
@Override public void execute(Runnable runnable) {
57-
if (runnable == null) {
58-
throw new IllegalArgumentException("Runnable to execute cannot be null");
59-
}
40+
@Override public void execute(@NonNull Runnable runnable) {
6041
this.threadPoolExecutor.execute(runnable);
6142
}
6243

6344
private static class JobThreadFactory implements ThreadFactory {
64-
private static final String THREAD_NAME = "android_";
6545
private int counter = 0;
6646

67-
@Override public Thread newThread(Runnable runnable) {
68-
return new Thread(runnable, THREAD_NAME + counter++);
47+
@Override public Thread newThread(@NonNull Runnable runnable) {
48+
return new Thread(runnable, "android_" + counter++);
6949
}
7050
}
7151
}

data/src/main/java/com/fernandocejas/android10/sample/data/net/ApiConnection.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* Implements {@link java.util.concurrent.Callable} so when executed asynchronously can
3030
* return a value.
3131
*/
32-
public class ApiConnection implements Callable<String> {
32+
class ApiConnection implements Callable<String> {
3333

3434
private static final String CONTENT_TYPE_LABEL = "Content-Type";
3535
private static final String CONTENT_TYPE_VALUE_JSON = "application/json; charset=utf-8";
@@ -41,7 +41,7 @@ private ApiConnection(String url) throws MalformedURLException {
4141
this.url = new URL(url);
4242
}
4343

44-
public static ApiConnection createGET(String url) throws MalformedURLException {
44+
static ApiConnection createGET(String url) throws MalformedURLException {
4545
return new ApiConnection(url);
4646
}
4747

@@ -52,7 +52,7 @@ public static ApiConnection createGET(String url) throws MalformedURLException {
5252
* @return A string response
5353
*/
5454
@Nullable
55-
public String requestSyncCall() {
55+
String requestSyncCall() {
5656
connectToApi();
5757
return response;
5858
}

data/src/main/java/com/fernandocejas/android10/sample/data/repository/UserDataRepository.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class UserDataRepository implements UserRepository {
4141
* @param userEntityDataMapper {@link UserEntityDataMapper}.
4242
*/
4343
@Inject
44-
public UserDataRepository(UserDataStoreFactory dataStoreFactory,
44+
UserDataRepository(UserDataStoreFactory dataStoreFactory,
4545
UserEntityDataMapper userEntityDataMapper) {
4646
this.userDataStoreFactory = dataStoreFactory;
4747
this.userEntityDataMapper = userEntityDataMapper;

data/src/main/java/com/fernandocejas/android10/sample/data/repository/datasource/UserDataStoreFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class UserDataStoreFactory {
3434
private final UserCache userCache;
3535

3636
@Inject
37-
public UserDataStoreFactory(@NonNull Context context, @NonNull UserCache userCache) {
37+
UserDataStoreFactory(@NonNull Context context, @NonNull UserCache userCache) {
3838
this.context = context.getApplicationContext();
3939
this.userCache = userCache;
4040
}

domain/src/main/java/com/fernandocejas/android10/sample/domain/User.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void setFollowers(int followers) {
7777
}
7878

7979
@Override public String toString() {
80-
StringBuilder stringBuilder = new StringBuilder();
80+
final StringBuilder stringBuilder = new StringBuilder();
8181

8282
stringBuilder.append("***** User Details *****\n");
8383
stringBuilder.append("id=" + this.getUserId() + "\n");

domain/src/main/java/com/fernandocejas/android10/sample/domain/interactor/GetUserList.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class GetUserList extends UseCase {
3131
private final UserRepository userRepository;
3232

3333
@Inject
34-
public GetUserList(UserRepository userRepository, ThreadExecutor threadExecutor,
34+
GetUserList(UserRepository userRepository, ThreadExecutor threadExecutor,
3535
PostExecutionThread postExecutionThread) {
3636
super(threadExecutor, postExecutionThread);
3737
this.userRepository = userRepository;

0 commit comments

Comments
 (0)