Skip to content

Commit 7058126

Browse files
punitdaakarnokd
authored andcommitted
Javadoc: explain that distinctUntilChanged requires non-mutating data to work as expected (#6311)
* Javadoc : Explain distinctUntilChanged requires non-mutating data type in flow(Observable) Add few tests for scenarios related to mutable data type flow in distinctUntilChanged() methods for Observable * Javadoc : Explain distinctUntilChanged requires non-mutating data type in flow(Flowable) Add few tests for scenarios related to mutable data type flow in distinctUntilChanged() methods for Flowable * Remove tests and fix typo in javadoc
1 parent f800b30 commit 7058126

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/main/java/io/reactivex/Flowable.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8740,6 +8740,13 @@ public final <K> Flowable<T> distinct(Function<? super T, K> keySelector,
87408740
* <p>
87418741
* Note that the operator always retains the latest item from upstream regardless of the comparison result
87428742
* and uses it in the next comparison with the next upstream item.
8743+
* <p>
8744+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
8745+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
8746+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
8747+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
8748+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
8749+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
87438750
* <dl>
87448751
* <dt><b>Backpressure:</b></dt>
87458752
* <dd>The operator doesn't interfere with backpressure which is determined by the source {@code Publisher}'s
@@ -8776,6 +8783,13 @@ public final Flowable<T> distinctUntilChanged() {
87768783
* <p>
87778784
* Note that the operator always retains the latest key from upstream regardless of the comparison result
87788785
* and uses it in the next comparison with the next key derived from the next upstream item.
8786+
* <p>
8787+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
8788+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
8789+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
8790+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
8791+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
8792+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
87798793
* <dl>
87808794
* <dt><b>Backpressure:</b></dt>
87818795
* <dd>The operator doesn't interfere with backpressure which is determined by the source {@code Publisher}'s
@@ -8808,6 +8822,13 @@ public final <K> Flowable<T> distinctUntilChanged(Function<? super T, K> keySele
88088822
* <p>
88098823
* Note that the operator always retains the latest item from upstream regardless of the comparison result
88108824
* and uses it in the next comparison with the next upstream item.
8825+
* <p>
8826+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
8827+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
8828+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
8829+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
8830+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
8831+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
88118832
* <dl>
88128833
* <dt><b>Backpressure:</b></dt>
88138834
* <dd>The operator doesn't interfere with backpressure which is determined by the source {@code Publisher}'s

src/main/java/io/reactivex/Observable.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7824,6 +7824,13 @@ public final <K> Observable<T> distinct(Function<? super T, K> keySelector, Call
78247824
* <p>
78257825
* Note that the operator always retains the latest item from upstream regardless of the comparison result
78267826
* and uses it in the next comparison with the next upstream item.
7827+
* <p>
7828+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
7829+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
7830+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
7831+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
7832+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
7833+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
78277834
* <dl>
78287835
* <dt><b>Scheduler:</b></dt>
78297836
* <dd>{@code distinctUntilChanged} does not operate by default on a particular {@link Scheduler}.</dd>
@@ -7856,6 +7863,13 @@ public final Observable<T> distinctUntilChanged() {
78567863
* <p>
78577864
* Note that the operator always retains the latest key from upstream regardless of the comparison result
78587865
* and uses it in the next comparison with the next key derived from the next upstream item.
7866+
* <p>
7867+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
7868+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
7869+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
7870+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
7871+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
7872+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
78597873
* <dl>
78607874
* <dt><b>Scheduler:</b></dt>
78617875
* <dd>{@code distinctUntilChanged} does not operate by default on a particular {@link Scheduler}.</dd>
@@ -7884,6 +7898,13 @@ public final <K> Observable<T> distinctUntilChanged(Function<? super T, K> keySe
78847898
* <p>
78857899
* Note that the operator always retains the latest item from upstream regardless of the comparison result
78867900
* and uses it in the next comparison with the next upstream item.
7901+
* <p>
7902+
* Note that if element type {@code T} in the flow is mutable, the comparison of the previous and current
7903+
* item may yield unexpected results if the items are mutated externally. Common cases are mutable
7904+
* {@code CharSequence}s or {@code List}s where the objects will actually have the same
7905+
* references when they are modified and {@code distinctUntilChanged} will evaluate subsequent items as same.
7906+
* To avoid such situation, it is recommended that mutable data is converted to an immutable one,
7907+
* for example using `map(CharSequence::toString)` or `map(Collections::unmodifiableList)`.
78877908
* <dl>
78887909
* <dt><b>Scheduler:</b></dt>
78897910
* <dd>{@code distinctUntilChanged} does not operate by default on a particular {@link Scheduler}.</dd>

0 commit comments

Comments
 (0)