Skip to content

Commit 639f853

Browse files
committed
Refactor to use lambdas instead of Rx Func1.
1 parent 2efd2c4 commit 639f853

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

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

Lines changed: 7 additions & 19 deletions
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,
@@ -15,7 +15,6 @@
1515
*/
1616
package com.fernandocejas.android10.sample.data.repository;
1717

18-
import com.fernandocejas.android10.sample.data.entity.UserEntity;
1918
import com.fernandocejas.android10.sample.data.entity.mapper.UserEntityDataMapper;
2019
import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStore;
2120
import com.fernandocejas.android10.sample.data.repository.datasource.UserDataStoreFactory;
@@ -25,7 +24,6 @@
2524
import javax.inject.Inject;
2625
import javax.inject.Singleton;
2726
import rx.Observable;
28-
import rx.functions.Func1;
2927

3028
/**
3129
* {@link UserRepository} for retrieving user data.
@@ -36,20 +34,6 @@ public class UserDataRepository implements UserRepository {
3634
private final UserDataStoreFactory userDataStoreFactory;
3735
private final UserEntityDataMapper userEntityDataMapper;
3836

39-
private final Func1<List<UserEntity>, List<User>> userListEntityMapper =
40-
new Func1<List<UserEntity>, List<User>>() {
41-
@Override public List<User> call(List<UserEntity> userEntities) {
42-
return UserDataRepository.this.userEntityDataMapper.transform(userEntities);
43-
}
44-
};
45-
46-
private final Func1<UserEntity, User>
47-
userDetailsEntityMapper = new Func1<UserEntity, User>() {
48-
@Override public User call(UserEntity userEntity) {
49-
return UserDataRepository.this.userEntityDataMapper.transform(userEntity);
50-
}
51-
};
52-
5337
/**
5438
* Constructs a {@link UserRepository}.
5539
*
@@ -63,14 +47,18 @@ public UserDataRepository(UserDataStoreFactory dataStoreFactory,
6347
this.userEntityDataMapper = userEntityDataMapper;
6448
}
6549

50+
@SuppressWarnings("Convert2MethodRef")
6651
@Override public Observable<List<User>> getUsers() {
6752
//we always get all users from the cloud
6853
final UserDataStore userDataStore = this.userDataStoreFactory.createCloudDataStore();
69-
return userDataStore.getUserEntityList().map(userListEntityMapper);
54+
return userDataStore.getUserEntityList()
55+
.map(userEntities -> this.userEntityDataMapper.transform(userEntities));
7056
}
7157

58+
@SuppressWarnings("Convert2MethodRef")
7259
@Override public Observable<User> getUser(int userId) {
7360
final UserDataStore userDataStore = this.userDataStoreFactory.create(userId);
74-
return userDataStore.getUserEntityDetails(userId).map(userDetailsEntityMapper);
61+
return userDataStore.getUserEntityDetails(userId)
62+
.map(userEntity -> this.userEntityDataMapper.transform(userEntity));
7563
}
7664
}

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

Lines changed: 9 additions & 9 deletions
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,
@@ -30,13 +30,12 @@ public class CloudUserDataStore implements UserDataStore {
3030
private final RestApi restApi;
3131
private final UserCache userCache;
3232

33-
private final Action1<UserEntity> saveToCacheAction = new Action1<UserEntity>() {
34-
@Override public void call(UserEntity userEntity) {
35-
if (userEntity != null) {
36-
CloudUserDataStore.this.userCache.put(userEntity);
37-
}
38-
}
39-
};
33+
private final Action1<UserEntity> saveToCacheAction =
34+
userEntity -> {
35+
if (userEntity != null) {
36+
CloudUserDataStore.this.userCache.put(userEntity);
37+
}
38+
};
4039

4140
/**
4241
* Construct a {@link UserDataStore} based on connections to the api (Cloud).
@@ -54,6 +53,7 @@ public CloudUserDataStore(RestApi restApi, UserCache userCache) {
5453
}
5554

5655
@Override public Observable<UserEntity> getUserEntityDetails(final int userId) {
57-
return this.restApi.getUserEntityById(userId).doOnNext(saveToCacheAction);
56+
return this.restApi.getUserEntityById(userId)
57+
.doOnNext(saveToCacheAction);
5858
}
5959
}

0 commit comments

Comments
 (0)