Reputation: 1243
public List<CustomerDTO> getCustomerSalaryByDepartmentNumber(String departmentNumber) {
List<CustomerDTO> customers = customerService.getCustomerSalaryByDepartmentNumber(departmentNumber);
if (CollectionUtils.isNotEmpty(customers)) {
for (CustomerDTO customer : customers) {
customerService.updateCustomDataOne(customer);
customerService.updateCustomDataTwo(customer);
}
}
return customers;
}
I am new to RxJava and trying to understand the concept of Observables
I want to refactor above simple code in Rx Java and don't want to use Observable.just(customers);
at the end of the method. if someone can point me in right direction it would be helpful.
Please look at the following code and provide suggestion.
public Observable<Object> getCustomerSalaryByDepartmentNumber(String departmentNumber) {
Observable<List<CustomerDTO>> customersObservable = Observable.just(departmentNumber).map(dnum -> {
return customerService.getCustomerSalaryByDepartmentNumber(dnum);
});
return customersObservable.map(customers -> {
return Observable.from(customers).map( customer -> {
return Observable.zip(
Observable.just(customerService.updateCustomDataOne(customer)),
Observable.just(customerService.updateCustomDataTwo(customer)),
(r, u) -> {
customer.setCustomDataOne(r.getCustomDataOne());
return customer;
});
});
});
}
Upvotes: 1
Views: 100
Reputation: 6711
This is a bit confusing, because your two methods above do different things.
In the imperative version, you're not using the return value of your updateCustomData
methods, nor are you calling the setCustomDataOne
method.
If you want a direct Observable
conversion of the first method, then how about the following:
public Observable<CustomerDTO> getCustomerSalaryByDepartmentNumber(String departmentNumber) {
return Observable.from(customerService.getCustomerSalaryByDepartmentNumber(departmentNumber))
.doOnNext(customer -> {
customerService.updateCustomDataOne(customer);
customerService.updateCustomDataTwo(customer);
});
}
Note the usage of Observable.from(List<T>)
which will return an Observable
that will emit each element of the list
in order. Doing this negates the need to check if an empty list.
With respect to the second method, the creation and usage of the customersObservable
object is redundant so can be replaced with the following:
public Observable<CustomerDTO> getCustomerSalaryByDepartmentNumber(String departmentNumber) {
return Observable.from(customerService.getCustomerSalaryByDepartmentNumber(departmentNumber)
.map(customer -> {
return Observable.zip(
Observable.just(customerService.updateCustomDataOne(customer)),
Observable.just(customerService.updateCustomDataTwo(customer)),
(r, u) -> {
customer.setCustomDataOne(r.getCustomDataOne());
return customer;
});
});
}
Be careful that customerService.updateCustomDataxxx
returns something not void. Observable.zip
won't execute the zipping function until all Observables being zipped emit something. This has caught me out many times.
Hope this helps
Upvotes: 1