Skip to content

Implemented SequenceEqual Operator #138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,50 @@ public static <R, T0, T1> Observable<R> zip(Observable<T0> w0, Observable<T1> w1
return _create(OperationZip.zip(w0, w1, reduceFunction));
}


/**
* Determines whether two sequences are equal by comparing the elements pairwise.
*
* @param first observable to compare
* @param second observable to compare
* @param <T> type of sequence
* @return sequence of booleans, true if two sequences are equal by comparing the elements pairwise; otherwise, false.
*/
public static <T> Observable<Boolean> sequenceEqual(Observable<T> first, Observable<T> second) {
return sequenceEqual(first, second, new Func2<T, T, Boolean>() {
@Override
public Boolean call(T first, T second) {
return first.equals(second);
}
});
}

/**
* Determines whether two sequences are equal by comparing the elements pairwise using a specified equality function.
*
* @param first observable sequence to compare
* @param second observable sequence to compare
* @param equality a function used to compare elements of both sequences
* @param <T> type of sequence
* @return sequence of booleans, true if two sequences are equal by comparing the elements pairwise; otherwise, false.
*/
private static <T> Observable<Boolean> sequenceEqual(Observable<T> first, Observable<T> second, Func2<T, T, Boolean> equality) {
return zip(first, second, equality);
}

/**
* Determines whether two sequences are equal by comparing the elements pairwise using a specified equality function.
*
* @param first observable sequence to compare
* @param second observable sequence to compare
* @param equality a function used to compare elements of both sequences
* @param <T> type of sequence
* @return sequence of booleans, true if two sequences are equal by comparing the elements pairwise; otherwise, false.
*/
private static <T> Observable<Boolean> sequenceEqual(Observable<T> first, Observable<T> second, Object equality) {
return zip(first, second, equality);
}

/**
* Returns an Observable that applies a function of your choosing to the combination of items
* emitted, in sequence, by two other Observables, with the results of this function becoming the
Expand Down Expand Up @@ -2345,5 +2389,18 @@ public Integer call(Integer t1, Integer t2) {
verify(w).onNext(60);
}

@Test
public void testSequenceEqual() {
Observable<Integer> first = toObservable(1, 2, 3);
Observable<Integer> second = toObservable(1, 2, 4);
@SuppressWarnings("unchecked")
Observer<Boolean> result = mock(Observer.class);
sequenceEqual(first, second).subscribe(result);
verify(result, times(2)).onNext(true);
verify(result, times(1)).onNext(false);
}



}
}