Skip to content

Instantly share code, notes, and snippets.

@abdallaadelessa
Created November 13, 2019 17:59
Show Gist options
  • Save abdallaadelessa/94b0df75cd80e709549f4663bcc67a22 to your computer and use it in GitHub Desktop.
Save abdallaadelessa/94b0df75cd80e709549f4663bcc67a22 to your computer and use it in GitHub Desktop.
RxJava Challenge Solution
import io.reactivex.Observable;
import io.reactivex.Scheduler;
import io.reactivex.Single;
import io.reactivex.functions.BiFunction;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.Map;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
class CountriesServiceSolved implements CountriesService {
@Override
public Single<String> countryNameInCapitals(Country country) {
return Single.just(country)
.map(it -> it.name.toUpperCase());
}
public Single<Integer> countCountries(List<Country> countries) {
return Observable.fromIterable(countries).count()
.map(Long::intValue);
}
public Observable<Long> listPopulationOfEachCountry(List<Country> countries) {
return Observable.fromIterable(countries)
.map(Country::getPopulation);
}
@Override
public Observable<String> listNameOfEachCountry(List<Country> countries) {
return Observable.fromIterable(countries)
.map(Country::getName);
}
@Override
public Observable<Country> listOnly3rdAnd4thCountry(List<Country> countries) {
return Observable.fromIterable(countries)
.skip(2)
.take(2);
}
@Override
public Single<Boolean> isAllCountriesPopulationMoreThanOneMillion(List<Country> countries) {
return Observable.fromIterable(countries)
.all(it -> it.getPopulation() > 1000000);
}
@Override
public Observable<Country> listPopulationMoreThanOneMillion(List<Country> countries) {
return Observable.fromIterable(countries)
.filter(it -> it.getPopulation() > 1000000);
}
@Override
public Observable<String> getCurrencyUsdIfNotFound(String countryName, List<Country> countries) {
return Observable.fromIterable(countries)
.filter(it -> it.getName().equals(countryName))
.map(Country::getCurrency)
.defaultIfEmpty("USD");
}
@Override
public Observable<Long> sumPopulationOfCountries(List<Country> countries) {
return Observable.fromIterable(countries)
.map(Country::getPopulation)
.reduce(0L, Long::sum)
.toObservable();
}
@Override
public Single<Map<String, Long>> mapCountriesToNamePopulation(List<Country> countries) {
return Observable.fromIterable(countries)
.toMap(Country::getName, Country::getPopulation);
}
@Override
public Observable<Long> sumPopulationOfCountries(Observable<Country> countryObservable1,
Observable<Country> countryObservable2) {
return countryObservable1
.concatWith(countryObservable2)
.map(Country::getPopulation)
.reduce(0L, Long::sum)
.toObservable();
}
@Override
public Observable<Country> listPopulationMoreThanOneMillionWithTimeoutFallbackToEmpty(final FutureTask<List<Country>> countriesFromNetwork) {
return Observable.fromFuture(countriesFromNetwork)
.subscribeOn(Schedulers.io())
.timeout(100, TimeUnit.MILLISECONDS, Observable.empty())
.flatMapIterable(it -> it)
.filter(it -> it.getPopulation() > 1000000);
}
@Override
public Single<Boolean> areEmittingSameSequences(Observable<Country> countryObservable1,
Observable<Country> countryObservable2) {
return Single.zip(countryObservable1.toList(), countryObservable2.toList(),
(countries, countries2) -> countries.hashCode() == countries2.hashCode());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment