Skip to content

Commit

Permalink
Expose the splitter key type
Browse files Browse the repository at this point in the history
  • Loading branch information
jponge committed Jul 3, 2023
1 parent 8c653fe commit 7f4f398
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ public class MultiSplitter<T, K extends Enum<K>> {
private final Function<T, K> splitter;
private final ConcurrentHashMap<K, SplitMulti.Split> splits;
private final int requiredNumberOfSubscribers;
private final Class<K> keyType;

public MultiSplitter(Multi<? extends T> upstream, Class<K> keyType, Function<T, K> splitter) {
this.upstream = nonNull(upstream, "upstream");
if (!nonNull(keyType, "keyType").isEnum()) {
// Note: the Java compiler enforces a type check on keyType being some enum, so this branch is only here for added peace of mind
throw new IllegalArgumentException("The key type must be that of an enumeration");
}
this.keyType = keyType;
this.splitter = nonNull(splitter, "splitter");
this.splits = new ConcurrentHashMap<>();
this.requiredNumberOfSubscribers = keyType.getEnumConstants().length;
Expand All @@ -74,6 +76,15 @@ public Multi<T> get(K key) {
return Infrastructure.onMultiCreation(new SplitMulti(key));
}

/**
* Get the (enum) key type.
*
* @return the key type
*/
public Class<K> keyType() {
return keyType;
}

private enum State {
INIT,
AWAITING_SUBSCRIPTION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ void rejectNegativeDemand() {
@Test
void checkBasicBehavior() {
var splitter = evenOddSplitter();
assertThat(splitter.keyType()).isEqualTo(OddEven.class);

var odd = splitter.get(OddEven.ODD)
.subscribe().withSubscriber(AssertSubscriber.create());
var even = splitter.get(OddEven.EVEN)
Expand Down

0 comments on commit 7f4f398

Please sign in to comment.