Last active
December 22, 2016 15:28
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
observe() .compose(new ListByListFilter<>(CallForwarding::getDestinations, destination -> destination.getNumber().equals(number)));