Package: TransportAsync
TransportAsync
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
TransportAsync(ExecutorService) |
|
|
|
|
|
||||||||||||||||||||
fireEvent(boolean) |
|
|
|
|
|
||||||||||||||||||||
handleConnected() |
|
|
|
|
|
||||||||||||||||||||
handleDisconnected() |
|
|
|
|
|
||||||||||||||||||||
lambda$fireEvent$1(CopyOnWriteArraySet, boolean) |
|
|
|
|
|
||||||||||||||||||||
lambda$listen$2(Transport.Listener) |
|
|
|
|
|
||||||||||||||||||||
lambda$null$0(boolean, Transport.Listener) |
|
|
|
|
|
||||||||||||||||||||
listen(Transport.Listener) |
|
|
|
|
|
||||||||||||||||||||
removeListener(Transport.Listener) |
|
|
|
|
|
Coverage
1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others.
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.client.gateway.spi.util;
14:
15: import java.util.concurrent.CompletableFuture;
16: import java.util.concurrent.CopyOnWriteArraySet;
17: import java.util.concurrent.ExecutorService;
18: import java.util.concurrent.Future;
19:
20: import org.eclipse.kapua.client.gateway.Transport;
21:
22: public class TransportAsync implements Transport {
23:
24: private final ExecutorService executor;
25: private final CopyOnWriteArraySet<Listener> listeners = new CopyOnWriteArraySet<>();
26: private boolean state;
27:
28: public TransportAsync(final ExecutorService executor) {
29: this.executor = executor;
30: }
31:
32: private Future<?> fireEvent(final boolean state) {
33:• if (listeners.isEmpty()) {
34: return CompletableFuture.completedFuture(null);
35: }
36:
37: final CopyOnWriteArraySet<Listener> listeners = new CopyOnWriteArraySet<>(this.listeners);
38: return executor.submit(() -> listeners.stream().forEach(l -> l.stateChange(state)));
39: }
40:
41: public synchronized Future<?> handleConnected() {
42:• if (!state) {
43: state = true;
44: return fireEvent(true);
45: }
46:
47: return CompletableFuture.completedFuture(null);
48: }
49:
50: public synchronized Future<?> handleDisconnected() {
51:• if (state) {
52: state = false;
53: return fireEvent(false);
54: }
55:
56: return CompletableFuture.completedFuture(null);
57: }
58:
59: @Override
60: public synchronized ListenerHandle listen(final Listener listener) {
61: listeners.add(listener);
62: fireEvent(state);
63: return () -> removeListener(listener);
64: }
65:
66: private synchronized void removeListener(final Listener listener) {
67: listeners.remove(listener);
68: }
69: }