|
| 1 | +package edu.wpi.grip.core; |
| 2 | + |
| 3 | +import com.google.common.eventbus.EventBus; |
| 4 | +import edu.wpi.grip.core.events.ConnectionRemovedEvent; |
| 5 | + |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 11 | + |
| 12 | +/** |
| 13 | + * A {@link SocketHint} that's type is linked between many other sockets and who's type is defined by |
| 14 | + * whatever {@link InputSocket} was connected to it first. |
| 15 | + */ |
| 16 | +public final class LinkedSocketHint extends SocketHint.SocketHintDecorator { |
| 17 | + /** |
| 18 | + * Keeps track of the sockets that control the type of this socket hint |
| 19 | + */ |
| 20 | + private final Set<InputSocket> controllingSockets = new HashSet<>(); |
| 21 | + private final Set<OutputSocket> controlledOutputSockets = new HashSet<>(); |
| 22 | + private final EventBus eventBus; |
| 23 | + private Optional<Class> connectedType = Optional.empty(); |
| 24 | + |
| 25 | + @SuppressWarnings("unchecked") |
| 26 | + public LinkedSocketHint(EventBus eventBus) { |
| 27 | + super(new Builder<>(Object.class).identifier("").build()); |
| 28 | + this.eventBus = checkNotNull(eventBus, "EventBus cannot be null"); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Creates an {@link InputSocket} that is linked to this SocketHint |
| 33 | + * |
| 34 | + * @param hintIdentifier The identifier for this socket's SocketHint |
| 35 | + * @return A socket hint that's socket type is determined by this SocketHint |
| 36 | + */ |
| 37 | + @SuppressWarnings("unchecked") |
| 38 | + public InputSocket linkedInputSocket(String hintIdentifier) { |
| 39 | + // Our own custom implementation of socket hint that interacts on this class when connections are |
| 40 | + // added and removed |
| 41 | + return new InputSocket(eventBus, new IdentiferOverridingSocketHintDecorator(this, hintIdentifier)) { |
| 42 | + @Override |
| 43 | + public void addConnection(Connection connection) { |
| 44 | + synchronized (this) { |
| 45 | + controllingSockets.add(this); |
| 46 | + connectedType = Optional.of(connection.getOutputSocket().getSocketHint().getType()); |
| 47 | + } |
| 48 | + super.addConnection(connection); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onDisconnected() { |
| 53 | + synchronized (this) { |
| 54 | + // Remove this socket because it is no longer controlling the type of socket |
| 55 | + controllingSockets.remove(this); |
| 56 | + if (controllingSockets.isEmpty()) { // When the set is empty, the socket can support any type again |
| 57 | + connectedType = Optional.empty(); |
| 58 | + // XXX: TODO: This is breaking the law of Demeter fix this |
| 59 | + controlledOutputSockets.forEach(outputSocket -> { |
| 60 | + final Set<Connection<?>> connections = outputSocket.getConnections(); |
| 61 | + connections.stream().map(ConnectionRemovedEvent::new).forEach(this.eventBus::post); |
| 62 | + outputSocket.setPreviewed(false); |
| 63 | + outputSocket.setValueOptional(Optional.empty()); |
| 64 | + }); |
| 65 | + } |
| 66 | + } |
| 67 | + super.onDisconnected(); |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Creates an input socket that is linked to this SocketHint. |
| 74 | + * This output socket will automatically be disconnected when there is no longer an input socket to guarantee the type |
| 75 | + * of this SocketHint |
| 76 | + * |
| 77 | + * @param hintIdentifier The identifier for this socket's SocketHint |
| 78 | + * @return An OutputSocket that's type is dynamically linked to this SocketHint |
| 79 | + */ |
| 80 | + @SuppressWarnings("unchecked") |
| 81 | + public OutputSocket linkedOutputSocket(String hintIdentifier) { |
| 82 | + final OutputSocket outSocket = new OutputSocket(eventBus, new IdentiferOverridingSocketHintDecorator(this, hintIdentifier)); |
| 83 | + controlledOutputSockets.add(outSocket); |
| 84 | + return outSocket; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String getTypeLabel() { |
| 89 | + return "<Generic>"; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public Class getType() { |
| 94 | + // If the type is known because one of the input sockets is connected then return that. Otherwise, return Object |
| 95 | + return connectedType.orElse(Object.class); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + @SuppressWarnings("unchecked") |
| 100 | + public boolean isCompatibleWith(SocketHint other) { |
| 101 | + if (connectedType.isPresent()) { // If the type is present |
| 102 | + // Then use this socket hint to determine if this socket can be connected |
| 103 | + return connectedType.get().isAssignableFrom(other.getType()); |
| 104 | + } else { |
| 105 | + // Otherwise use the socket hint we are decorating to determine the supported type |
| 106 | + return getDecorated().isCompatibleWith(other); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments