Skip to content

Instantly share code, notes, and snippets.

@VladSumtsov
Last active December 22, 2016 15:28
Show Gist options
  • Save VladSumtsov/2cdec68bc12a181463b24e24b42fc236 to your computer and use it in GitHub Desktop.
Save VladSumtsov/2cdec68bc12a181463b24e24b42fc236 to your computer and use it in GitHub Desktop.
Rx list filter. Use this filter to filter list by other list or by internal list
package com.grasshopper.dialer.util;
import android.support.annotation.NonNull;
import java.util.List;
import rx.Observable;
import rx.functions.Func1;
/**
* Use this filter to filter list by other list or by internal list
* @param <T>
* @param <P>
*/
public class ListByListFilter<T, P> implements Observable.Transformer<List<T>, List<T>> {
private final Func1<T, List<P>> innerList;
private final Func1<P, Boolean> filter;
public ListByListFilter(@NonNull Func1<T, List<P>> innerList, Func1<P, Boolean> filter) {
this.innerList = innerList;
this.filter = filter;
}
@Override
public Observable<List<T>> call(Observable<List<T>> source) {
return source
.flatMap(Observable::from)
.filter(forwarding -> {
for (P p : innerList.call(forwarding)) {
if (filter.call(p)) {
return true;
}
}
return false;
}).toList();
}
}
@VladSumtsov
Copy link
Author

observe() .compose(new ListByListFilter<>(CallForwarding::getDestinations, destination -> destination.getNumber().equals(number)));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment