Skip to content
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

OperatorDistinctUntilChanged #1085

Merged
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import rx.operators.OperationDelay;
import rx.operators.OperationDematerialize;
import rx.operators.OperationDistinct;
import rx.operators.OperationDistinctUntilChanged;
import rx.operators.OperationFinally;
import rx.operators.OperationFlatMap;
import rx.operators.OperationGroupByUntil;
Expand Down Expand Up @@ -95,6 +94,7 @@
import rx.operators.OperatorAsObservable;
import rx.operators.OperatorCache;
import rx.operators.OperatorCast;
import rx.operators.OperatorDistinctUntilChanged;
import rx.operators.OperatorDoOnEach;
import rx.operators.OperatorElementAt;
import rx.operators.OperatorFilter;
Expand Down Expand Up @@ -3638,7 +3638,7 @@ public final <U> Observable<T> distinct(Func1<? super T, ? extends U> keySelecto
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229494.aspx">MSDN: Observable.distinctUntilChanged</a>
*/
public final Observable<T> distinctUntilChanged() {
return create(OperationDistinctUntilChanged.distinctUntilChanged(this));
return lift(new OperatorDistinctUntilChanged<T, T>(Functions.<T>identity()));
}

/**
Expand All @@ -3656,7 +3656,7 @@ public final Observable<T> distinctUntilChanged() {
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229508.aspx">MSDN: Observable.distinctUntilChanged</a>
*/
public final <U> Observable<T> distinctUntilChanged(Func1<? super T, ? extends U> keySelector) {
return create(OperationDistinctUntilChanged.distinctUntilChanged(this, keySelector));
return lift(new OperatorDistinctUntilChanged<T, U>(keySelector));
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package rx.operators;

import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Func1;

/**
* Returns an Observable that emits all sequentially distinct items emitted by the source.
* @param <T> the value type
* @param <U> the key type
*/
public final class OperatorDistinctUntilChanged<T, U> implements Operator<T, T> {
final Func1<? super T, ? extends U> keySelector;

public OperatorDistinctUntilChanged(Func1<? super T, ? extends U> keySelector) {
this.keySelector = keySelector;
}

@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
return new Subscriber<T>(child) {
U previousKey;
boolean hasPrevious;
@Override
public void onNext(T t) {
U currentKey = previousKey;
U key = keySelector.call(t);
previousKey = key;

if (hasPrevious) {
if (!(currentKey == key || (key != null && key.equals(currentKey)))) {
child.onNext(t);
}
} else {
hasPrevious = true;
child.onNext(t);
}
}

@Override
public void onError(Throwable e) {
child.onError(e);
}

@Override
public void onCompleted() {
child.onCompleted();
}

};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks;
import static rx.operators.OperationDistinctUntilChanged.distinctUntilChanged;

import java.util.Comparator;

import org.junit.Before;
import org.junit.Test;
Expand All @@ -35,7 +33,7 @@
import rx.Observer;
import rx.functions.Func1;

public class OperationDistinctUntilChangedTest {
public class OperatorDistinctUntilChangedTest {

@Mock
Observer<String> w;
Expand All @@ -53,13 +51,6 @@ public String call(String s) {
}
};

final Comparator<String> COMPARE_LENGTH = new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.length() - s2.length();
}
};

@Before
public void before() {
initMocks(this);
Expand All @@ -68,7 +59,7 @@ public void before() {
@Test
public void testDistinctUntilChangedOfNone() {
Observable<String> src = Observable.empty();
Observable.create(distinctUntilChanged(src)).subscribe(w);
src.distinctUntilChanged().subscribe(w);

verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
Expand All @@ -78,7 +69,7 @@ public void testDistinctUntilChangedOfNone() {
@Test
public void testDistinctUntilChangedOfNoneWithKeySelector() {
Observable<String> src = Observable.empty();
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w);
src.distinctUntilChanged(TO_UPPER_WITH_EXCEPTION).subscribe(w);

verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
Expand All @@ -88,7 +79,7 @@ public void testDistinctUntilChangedOfNoneWithKeySelector() {
@Test
public void testDistinctUntilChangedOfNormalSource() {
Observable<String> src = Observable.from("a", "b", "c", "c", "c", "b", "b", "a", "e");
Observable.create(distinctUntilChanged(src)).subscribe(w);
src.distinctUntilChanged().subscribe(w);

InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
Expand All @@ -105,7 +96,7 @@ public void testDistinctUntilChangedOfNormalSource() {
@Test
public void testDistinctUntilChangedOfNormalSourceWithKeySelector() {
Observable<String> src = Observable.from("a", "b", "c", "C", "c", "B", "b", "a", "e");
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w);
src.distinctUntilChanged(TO_UPPER_WITH_EXCEPTION).subscribe(w);

InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
Expand All @@ -122,7 +113,7 @@ public void testDistinctUntilChangedOfNormalSourceWithKeySelector() {
@Test
public void testDistinctUntilChangedOfSourceWithNulls() {
Observable<String> src = Observable.from(null, "a", "a", null, null, "b", null, null);
Observable.create(distinctUntilChanged(src)).subscribe(w);
src.distinctUntilChanged().subscribe(w);

InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext(null);
Expand All @@ -138,7 +129,7 @@ public void testDistinctUntilChangedOfSourceWithNulls() {
@Test
public void testDistinctUntilChangedOfSourceWithExceptionsFromKeySelector() {
Observable<String> src = Observable.from("a", "b", null, "c");
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION)).subscribe(w);
src.distinctUntilChanged(TO_UPPER_WITH_EXCEPTION).subscribe(w);

InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
Expand All @@ -147,56 +138,4 @@ public void testDistinctUntilChangedOfSourceWithExceptionsFromKeySelector() {
inOrder.verify(w, never()).onNext(anyString());
inOrder.verify(w, never()).onCompleted();
}

@Test
public void testDistinctUntilChangedWithComparator() {
Observable<String> src = Observable.from("a", "b", "c", "aa", "bb", "c", "ddd");
Observable.create(distinctUntilChanged(src, COMPARE_LENGTH)).subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("aa");
inOrder.verify(w, times(1)).onNext("c");
inOrder.verify(w, times(1)).onNext("ddd");
inOrder.verify(w, times(1)).onCompleted();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
}

@Test
public void testDistinctUntilChangedWithComparatorAndKeySelector() {
Observable<String> src = Observable.from("a", "b", "x", "aa", "bb", "c", "ddd");
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("x");
inOrder.verify(w, times(1)).onNext("c");
inOrder.verify(w, times(1)).onNext("ddd");
inOrder.verify(w, times(1)).onCompleted();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));
}

@Test
public void testDistinctUntilChangedWithComparatorAndKeySelectorandTwoSubscriptions() {
Observable<String> src = Observable.from("a", "b", "x", "aa", "bb", "c", "ddd");
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w);
InOrder inOrder = inOrder(w);
inOrder.verify(w, times(1)).onNext("a");
inOrder.verify(w, times(1)).onNext("x");
Observable.create(distinctUntilChanged(src, TO_UPPER_WITH_EXCEPTION, COMPARE_LENGTH)).subscribe(w2);
inOrder.verify(w, times(1)).onNext("c");
inOrder.verify(w, times(1)).onNext("ddd");
inOrder.verify(w, times(1)).onCompleted();
inOrder.verify(w, never()).onNext(anyString());
verify(w, never()).onError(any(Throwable.class));

InOrder inOrder2 = inOrder(w2);
inOrder2.verify(w2, times(1)).onNext("a");
inOrder2.verify(w2, times(1)).onNext("x");
inOrder2.verify(w2, times(1)).onNext("c");
inOrder2.verify(w2, times(1)).onNext("ddd");
inOrder2.verify(w2, times(1)).onCompleted();
inOrder2.verify(w2, never()).onNext(anyString());
verify(w2, never()).onError(any(Throwable.class));
}
}