-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add variants of skip().repetitions() and select().distinct() acceptin…
…g custom comparators
- Loading branch information
1 parent
9f16bb4
commit c3db8c5
Showing
8 changed files
with
316 additions
and
86 deletions.
There are no files selected for viewing
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
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
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
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
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
67 changes: 0 additions & 67 deletions
67
implementation/src/main/java/io/smallrye/mutiny/operators/multi/MultiDropRepetitionsOp.java
This file was deleted.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
implementation/src/main/java/io/smallrye/mutiny/operators/multi/MultiSkipRepetitionsOp.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package io.smallrye.mutiny.operators.multi; | ||
|
||
import static io.smallrye.mutiny.helpers.ParameterValidation.nonNullNpe; | ||
|
||
import java.util.Comparator; | ||
|
||
import io.smallrye.mutiny.Multi; | ||
import io.smallrye.mutiny.subscription.MultiSubscriber; | ||
|
||
/** | ||
* Eliminates the duplicated items from the upstream. | ||
* | ||
* @param <T> the type of items | ||
*/ | ||
public final class MultiSkipRepetitionsOp<T> extends AbstractMultiOperator<T, T> { | ||
|
||
private final Comparator<? super T> comparator; | ||
|
||
public MultiSkipRepetitionsOp(Multi<T> upstream) { | ||
this(upstream, null); | ||
} | ||
|
||
public MultiSkipRepetitionsOp(Multi<T> upstream, Comparator<? super T> comparator) { | ||
super(upstream); | ||
this.comparator = comparator; | ||
} | ||
|
||
@Override | ||
public void subscribe(MultiSubscriber<? super T> subscriber) { | ||
nonNullNpe(subscriber, "subscriber"); | ||
upstream.subscribe().withSubscriber(new MultiSkipRepetitionsProcessor<>(subscriber, comparator)); | ||
} | ||
|
||
static final class MultiSkipRepetitionsProcessor<T> extends MultiOperatorProcessor<T, T> { | ||
|
||
private final Comparator<? super T> comparator; | ||
private T last; | ||
|
||
public MultiSkipRepetitionsProcessor(MultiSubscriber<? super T> subscriber, Comparator<? super T> comparator) { | ||
super(subscriber); | ||
if (comparator == null) { | ||
this.comparator = (a, b) -> a.equals(b) ? 0 : 1; | ||
} else { | ||
this.comparator = comparator; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public void onItem(T t) { | ||
if (isDone()) { | ||
return; | ||
} | ||
try { | ||
if (last == null || comparator.compare(last, t) != 0) { | ||
last = t; | ||
downstream.onItem(t); | ||
} else { | ||
// Request the next one, as that item is dropped. | ||
request(1); | ||
} | ||
} catch (Exception e) { | ||
onFailure(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Throwable t) { | ||
super.onFailure(t); | ||
last = null; | ||
} | ||
|
||
@Override | ||
public void onCompletion() { | ||
super.onCompletion(); | ||
last = null; | ||
} | ||
|
||
@Override | ||
public void cancel() { | ||
super.cancel(); | ||
last = null; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.