|
| 1 | +package com.vaadin.signals.impl; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collection; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Consumer; |
| 10 | + |
| 11 | +import com.vaadin.signals.Id; |
| 12 | +import com.vaadin.signals.SignalCommand; |
| 13 | + |
| 14 | +/** |
| 15 | + * A list of signal commands together with their result handlers. |
| 16 | + */ |
| 17 | +public class CommandsAndHandlers { |
| 18 | + private final List<SignalCommand> commands = new ArrayList<>(); |
| 19 | + private final Map<Id, Consumer<CommandResult>> resultHandlers = new HashMap<>(); |
| 20 | + |
| 21 | + /** |
| 22 | + * Creates a new empty command list. |
| 23 | + */ |
| 24 | + public CommandsAndHandlers() { |
| 25 | + |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a new command list with the given commands and result handlers. |
| 30 | + * |
| 31 | + * @param commands |
| 32 | + * the commands to use, not <code>null</code> |
| 33 | + * @param resultHandlers |
| 34 | + * the result handlers to use, not <code>null</code> |
| 35 | + */ |
| 36 | + public CommandsAndHandlers(List<SignalCommand> commands, |
| 37 | + Map<Id, Consumer<CommandResult>> resultHandlers) { |
| 38 | + this.commands.addAll(commands); |
| 39 | + this.resultHandlers.putAll(resultHandlers); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Creates a new command list with a single command and optional result |
| 44 | + * handler. |
| 45 | + * |
| 46 | + * @param command |
| 47 | + * the command to use, not <code>null</code> |
| 48 | + * @param resultHandler |
| 49 | + * the result handler to use, or <code>null</code> to not use a |
| 50 | + * result handler |
| 51 | + */ |
| 52 | + public CommandsAndHandlers(SignalCommand command, |
| 53 | + Consumer<CommandResult> resultHandler) { |
| 54 | + assert command != null; |
| 55 | + commands.add(command); |
| 56 | + if (resultHandler != null) { |
| 57 | + resultHandlers.put(command.commandId(), resultHandler); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Removes commands based on a collection of handled commands. Note that the |
| 63 | + * corresponding result handlers are not removed but there's instead an |
| 64 | + * assumption that the caller will invoke {@link #notifyResultHandlers(Map)} |
| 65 | + * separately. |
| 66 | + * |
| 67 | + * @param handledCommandIds |
| 68 | + * a collection of handled commands ids, not <code>null</code> |
| 69 | + */ |
| 70 | + public void removeHandledCommands(Collection<Id> handledCommandIds) { |
| 71 | + commands.removeIf( |
| 72 | + command -> handledCommandIds.contains(command.commandId())); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Notifies and removes result handlers for the given results. |
| 77 | + * |
| 78 | + * @param results |
| 79 | + * a map of command results, not <code>null</code> |
| 80 | + */ |
| 81 | + public void notifyResultHandlers(Map<Id, CommandResult> results) { |
| 82 | + notifyResultHandlers(results, commands); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Notifies and removes result handlers for the given results in the given |
| 87 | + * order. Commands in the order that have no corresponding result are |
| 88 | + * ignored. |
| 89 | + * |
| 90 | + * @param results |
| 91 | + * the map of command results, not <code>null</code> |
| 92 | + * @param commandOrder |
| 93 | + * a list of commands in the order the results should be applied. |
| 94 | + */ |
| 95 | + public void notifyResultHandlers(Map<Id, CommandResult> results, |
| 96 | + List<SignalCommand> commandOrder) { |
| 97 | + for (SignalCommand command : commandOrder) { |
| 98 | + if (command instanceof SignalCommand.TransactionCommand tx) { |
| 99 | + notifyResultHandlers(results, tx.commands()); |
| 100 | + } |
| 101 | + Consumer<CommandResult> handler = resultHandlers |
| 102 | + .remove(command.commandId()); |
| 103 | + if (handler != null) { |
| 104 | + handler.accept(results.get(command.commandId())); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Gets an unmodifiable view of the commands. |
| 111 | + * |
| 112 | + * @return an unmodifiable list of commands, not <code>null</code> |
| 113 | + */ |
| 114 | + public List<SignalCommand> getCommands() { |
| 115 | + return Collections.unmodifiableList(commands); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Gets an unmodifiable map of the result handlers. |
| 120 | + * |
| 121 | + * @return an unmodifiable map of result handlers, not <code>null</code> |
| 122 | + */ |
| 123 | + public Map<Id, Consumer<CommandResult>> getResultHandlers() { |
| 124 | + return Collections.unmodifiableMap(resultHandlers); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Adds another collection of commands and handlers to this one. |
| 129 | + * |
| 130 | + * |
| 131 | + * @param other |
| 132 | + * the instance to import entries from, not <code>null</code> |
| 133 | + */ |
| 134 | + public void add(CommandsAndHandlers other) { |
| 135 | + this.commands.addAll(other.commands); |
| 136 | + this.resultHandlers.putAll(other.resultHandlers); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Checks whether there are any commands in this list. |
| 141 | + * |
| 142 | + * @return <code>true</code> if there are no commands in this list, |
| 143 | + * <code>false</code> if there are commands |
| 144 | + */ |
| 145 | + public boolean isEmpty() { |
| 146 | + return commands.isEmpty(); |
| 147 | + } |
| 148 | +} |
0 commit comments