/** * Subscribe this command handler to the given {@code commandBus}. The command handler will be subscribed * for each of the supported commands. * * @param commandBus The command bus instance to subscribe to * @return A handle that can be used to unsubscribe */ public Registration subscribe(CommandBus commandBus) { List<Registration> subscriptions = supportedCommandNames() .stream() .map(supportedCommand -> commandBus.subscribe(supportedCommand, this)) .filter(Objects::nonNull).collect(Collectors.toList()); return () -> subscriptions.stream().map(Registration::cancel).reduce(Boolean::logicalOr).orElse(false); }
/** * Subscribe this command handler to the given {@code commandBus}. The command handler will be subscribed * for each of the supported commands. * * @param commandBus The command bus instance to subscribe to * @return A handle that can be used to unsubscribe */ public Registration subscribe(CommandBus commandBus) { Collection<Registration> subscriptions = new ArrayList<>(); for (String supportedCommand : supportedCommandNames()) { Registration subscription = commandBus.subscribe(supportedCommand, this); if (subscription != null) { subscriptions.add(subscription); } } return () -> { subscriptions.forEach(Registration::cancel); return true; }; }