Source:
public UnionInputGate(IndexedInputGate... inputGates) { this.inputGates = checkNotNull(inputGates); checkArgument(inputGates.length > 1, "Union input gate should union at least two input gates."); if (Arrays.stream(inputGates).map(IndexedInputGate::getGateIndex).distinct().count() != inputGates.length) { throw new IllegalArgumentException("Union of two input gates with the same gate index. Given indices: " + Arrays.stream(inputGates).map(IndexedInputGate::getGateIndex).collect(Collectors.toList())); } this.inputGatesWithRemainingData = Sets.newHashSetWithExpectedSize(inputGates.length); final int maxGateIndex = Arrays.stream(inputGates).mapToInt(IndexedInputGate::getGateIndex).max().orElse(0); inputGateChannelIndexOffsets = new int[maxGateIndex + 1]; int totalNumberOfInputChannels = Arrays.stream(inputGates).mapToInt(IndexedInputGate::getNumberOfInputChannels).sum(); inputChannelToInputGateIndex = new int[totalNumberOfInputChannels]; int currentNumberOfInputChannels = 0; for (int index = 0; index < inputGates.length; index++) { inputGateChannelIndexOffsets[index] = currentNumberOfInputChannels; int previousNumberOfInputChannels = currentNumberOfInputChannels; currentNumberOfInputChannels += inputGates[index].getNumberOfInputChannels(); Arrays.fill(inputChannelToInputGateIndex, previousNumberOfInputChannels, currentNumberOfInputChannels, index); } synchronized (inputGatesWithData) { for (IndexedInputGate inputGate : inputGates) { inputGatesWithRemainingData.add(inputGate); CompletableFuture<?> available = inputGate.getAvailableFuture(); if (available.isDone()) { inputGatesWithData.add(inputGate); } else { available.thenRun(() -> queueInputGate(inputGate)); } } if (!inputGatesWithData.isEmpty()) { availabilityHelper.resetAvailable(); } } }

Target:
public UnionInputGate(IndexedInputGate... inputGates) { inputGatesByGateIndex = Arrays.stream(inputGates).collect(Collectors.toMap(IndexedInputGate::getGateIndex, ig -> ig)); checkArgument(inputGates.length > 1, "Union input gate should union at least two input gates."); if (Arrays.stream(inputGates).map(IndexedInputGate::getGateIndex).distinct().count() != inputGates.length) { throw new IllegalArgumentException("Union of two input gates with the same gate index. Given indices: " + Arrays.stream(inputGates).map(IndexedInputGate::getGateIndex).collect(Collectors.toList())); } this.inputGatesWithRemainingData = Sets.newHashSetWithExpectedSize(inputGates.length); final int maxGateIndex = Arrays.stream(inputGates).mapToInt(IndexedInputGate::getGateIndex).max().orElse(0); int totalNumberOfInputChannels = Arrays.stream(inputGates).mapToInt(IndexedInputGate::getNumberOfInputChannels).sum(); inputGateChannelIndexOffsets = new int[maxGateIndex + 1]; inputChannelToInputGateIndex = new int[totalNumberOfInputChannels]; int currentNumberOfInputChannels = 0; for (final IndexedInputGate inputGate : inputGates) { inputGateChannelIndexOffsets[inputGate.getGateIndex()] = currentNumberOfInputChannels; int previousNumberOfInputChannels = currentNumberOfInputChannels; currentNumberOfInputChannels += inputGate.getNumberOfInputChannels(); Arrays.fill(inputChannelToInputGateIndex, previousNumberOfInputChannels, currentNumberOfInputChannels, inputGate.getGateIndex()); } synchronized (inputGatesWithData) { for (IndexedInputGate inputGate : inputGates) { inputGatesWithRemainingData.add(inputGate); CompletableFuture<?> available = inputGate.getAvailableFuture(); if (available.isDone()) { inputGatesWithData.add(inputGate); } else { available.thenRun(() -> queueInputGate(inputGate)); } } if (!inputGatesWithData.isEmpty()) { availabilityHelper.resetAvailable(); } } }