Skip to content

Commit 88b8114

Browse files
committed
Merge pull request #558 from JLLeitschuh/refactor/stepSocketsToImmutableList
Changes Step getInput/OutputSockets() to return ImmutableList
2 parents aa999d4 + 308d158 commit 88b8114

File tree

11 files changed

+91
-89
lines changed

11 files changed

+91
-89
lines changed

core/src/main/java/edu/wpi/grip/core/Source.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.grip.core;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.google.inject.Inject;
45
import edu.wpi.grip.core.sockets.OutputSocket;
56
import edu.wpi.grip.core.sources.CameraSource;
@@ -59,13 +60,13 @@ protected Source(ExceptionWitness.Factory exceptionWitnessFactory) {
5960
*
6061
* @return @return An array of {@link OutputSocket}s for the outputs that the source produces.
6162
*/
62-
public final OutputSocket[] getOutputSockets() {
63+
public final ImmutableList<OutputSocket> getOutputSockets() {
6364
final OutputSocket[] outputSockets = this.createOutputSockets();
6465
for (OutputSocket socket : outputSockets) {
6566
socket.setSource(Optional.of(this));
6667
}
6768

68-
return outputSockets;
69+
return ImmutableList.copyOf(outputSockets);
6970
}
7071

7172
protected abstract OutputSocket[] createOutputSockets();

core/src/main/java/edu/wpi/grip/core/Step.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package edu.wpi.grip.core;
22

3+
import com.google.common.collect.ImmutableList;
34
import com.google.common.eventbus.EventBus;
45
import com.google.inject.Inject;
56
import com.google.inject.Singleton;
@@ -98,17 +99,17 @@ public Operation getOperation() {
9899
}
99100

100101
/**
101-
* @return An array of <code>Socket</code>s that hold the inputs to this step
102+
* @return An array of {@link InputSocket InputSockets} that hold the inputs to this step
102103
*/
103-
public InputSocket<?>[] getInputSockets() {
104-
return inputSockets;
104+
public ImmutableList<InputSocket<?>> getInputSockets() {
105+
return ImmutableList.copyOf(inputSockets);
105106
}
106107

107108
/**
108-
* @return An array of <code>Socket</code>s that hold the outputs of this step
109+
* @return A list of {@link OutputSocket OutputSockets} that hold the outputs of this step
109110
*/
110-
public OutputSocket<?>[] getOutputSockets() {
111-
return outputSockets;
111+
public ImmutableList<OutputSocket<?>> getOutputSockets() {
112+
return ImmutableList.copyOf(outputSockets);
112113
}
113114

114115
/**

core/src/main/java/edu/wpi/grip/core/serialization/SocketConverter.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import com.thoughtworks.xstream.converters.UnmarshallingContext;
77
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
88
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
9-
import edu.wpi.grip.core.*;
9+
import edu.wpi.grip.core.Pipeline;
10+
import edu.wpi.grip.core.Source;
11+
import edu.wpi.grip.core.Step;
1012
import edu.wpi.grip.core.sockets.InputSocket;
1113
import edu.wpi.grip.core.sockets.OutputSocket;
1214
import edu.wpi.grip.core.sockets.Socket;
@@ -15,7 +17,6 @@
1517
import javax.inject.Inject;
1618
import java.lang.reflect.Modifier;
1719
import java.util.ArrayList;
18-
import java.util.Arrays;
1920
import java.util.List;
2021

2122
/**
@@ -47,16 +48,16 @@ public void marshal(Object obj, HierarchicalStreamWriter writer, MarshallingCont
4748

4849
// Save the location of the socket in the pipeline.
4950
socket.getStep().ifPresent(step -> {
50-
final Socket<?>[] sockets = socket.getDirection() == Socket.Direction.INPUT ?
51+
final List<? extends Socket> sockets = socket.getDirection() == Socket.Direction.INPUT ?
5152
step.getInputSockets() : step.getOutputSockets();
5253
writer.addAttribute(STEP_ATTRIBUTE, String.valueOf(pipeline.getSteps().indexOf(step)));
53-
writer.addAttribute(SOCKET_ATTRIBUTE, String.valueOf(Arrays.asList(sockets).indexOf(socket)));
54+
writer.addAttribute(SOCKET_ATTRIBUTE, String.valueOf(sockets.indexOf(socket)));
5455
});
5556

5657
socket.getSource().ifPresent(source -> {
57-
final Socket<?>[] sockets = source.getOutputSockets();
58+
final List<? extends Socket> sockets = source.getOutputSockets();
5859
writer.addAttribute(SOURCE_ATTRIBUTE, String.valueOf(pipeline.getSources().indexOf(source)));
59-
writer.addAttribute(SOCKET_ATTRIBUTE, String.valueOf(Arrays.asList(sockets).indexOf(socket)));
60+
writer.addAttribute(SOCKET_ATTRIBUTE, String.valueOf(sockets.indexOf(socket)));
6061
});
6162

6263
// Save whether or not output sockets are previewed
@@ -110,13 +111,13 @@ public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext co
110111

111112
final Step step = pipeline.getSteps().get(stepIndex);
112113
socket = direction == Socket.Direction.INPUT ?
113-
step.getInputSockets()[socketIndex] : step.getOutputSockets()[socketIndex];
114+
step.getInputSockets().get(socketIndex) : step.getOutputSockets().get(socketIndex);
114115
} else if (reader.getAttribute(SOURCE_ATTRIBUTE) != null) {
115116
final int sourceIndex = Integer.parseInt(reader.getAttribute(SOURCE_ATTRIBUTE));
116117
final int socketIndex = Integer.parseInt(reader.getAttribute(SOCKET_ATTRIBUTE));
117118

118119
final Source source = pipeline.getSources().get(sourceIndex);
119-
socket = source.getOutputSockets()[socketIndex];
120+
socket = source.getOutputSockets().get(socketIndex);
120121
} else {
121122
throw new ConversionException("Sockets must have either a step or source attribute");
122123
}

core/src/test/java/edu/wpi/grip/core/PipelineTest.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ public void testRemoveConnection() {
191191
public void testPipeline() {
192192
Step step1 = stepFactory.create(addition);
193193
Step step2 = stepFactory.create(addition);
194-
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets()[0];
195-
InputSocket<Double> b1 = (InputSocket<Double>) step1.getInputSockets()[1];
196-
OutputSocket<Double> sum1 = (OutputSocket<Double>) step1.getOutputSockets()[0];
197-
InputSocket<Double> a2 = (InputSocket<Double>) step2.getInputSockets()[0];
198-
InputSocket<Double> b2 = (InputSocket<Double>) step2.getInputSockets()[1];
199-
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets()[0];
194+
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets().get(0);
195+
InputSocket<Double> b1 = (InputSocket<Double>) step1.getInputSockets().get(1);
196+
OutputSocket<Double> sum1 = (OutputSocket<Double>) step1.getOutputSockets().get(0);
197+
InputSocket<Double> a2 = (InputSocket<Double>) step2.getInputSockets().get(0);
198+
InputSocket<Double> b2 = (InputSocket<Double>) step2.getInputSockets().get(1);
199+
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets().get(0);
200200

201201
// The result of this is the following equalities:
202202
// sum1 = a1+b1
@@ -226,12 +226,12 @@ public void testPipeline() {
226226
public void testPipelineRemoved() {
227227
Step step1 = stepFactory.create(addition);
228228
Step step2 = stepFactory.create(addition);
229-
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets()[0];
230-
InputSocket<Double> b1 = (InputSocket<Double>) step1.getInputSockets()[1];
231-
OutputSocket<Double> sum1 = (OutputSocket<Double>) step1.getOutputSockets()[0];
232-
InputSocket<Double> a2 = (InputSocket<Double>) step2.getInputSockets()[0];
233-
InputSocket<Double> b2 = (InputSocket<Double>) step2.getInputSockets()[1];
234-
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets()[0];
229+
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets().get(0);
230+
InputSocket<Double> b1 = (InputSocket<Double>) step1.getInputSockets().get(1);
231+
OutputSocket<Double> sum1 = (OutputSocket<Double>) step1.getOutputSockets().get(0);
232+
InputSocket<Double> a2 = (InputSocket<Double>) step2.getInputSockets().get(0);
233+
InputSocket<Double> b2 = (InputSocket<Double>) step2.getInputSockets().get(1);
234+
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets().get(0);
235235

236236
a2.setValue(0.0);
237237

@@ -259,8 +259,8 @@ public void testPipelineRemoved() {
259259
public void testCannotConnectBackwards() {
260260
Step step1 = stepFactory.create(addition);
261261
Step step2 = stepFactory.create(addition);
262-
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets()[0];
263-
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets()[0];
262+
InputSocket<Double> a1 = (InputSocket<Double>) step1.getInputSockets().get(0);
263+
OutputSocket<Double> sum2 = (OutputSocket<Double>) step2.getOutputSockets().get(0);
264264

265265
pipeline.addStep(step1);
266266
pipeline.addStep(step2);

core/src/test/java/edu/wpi/grip/core/PythonTest.java

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public void setUp () {
2323
public void testPython() throws Exception {
2424
Operation addition = new PythonScriptOperation(PythonTest.class.getResource("/edu/wpi/grip/scripts/addition.py"));
2525
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(addition);
26-
Socket aSocket = step.getInputSockets()[0];
27-
Socket bSocket = step.getInputSockets()[1];
28-
Socket sumSocket = step.getOutputSockets()[0];
26+
Socket aSocket = step.getInputSockets().get(0);
27+
Socket bSocket = step.getInputSockets().get(1);
28+
Socket sumSocket = step.getOutputSockets().get(0);
2929

3030
aSocket.setValue(a);
3131
bSocket.setValue(b);
@@ -42,9 +42,9 @@ public void testPythonAdditionFromString() throws Exception {
4242
"\"b\", 0.0),\n]\n\noutputs = [\n grip.SocketHints.Outputs.createNumberSocketHint(\"sum\", 0.0)," +
4343
"\n]\n\ndef perform(a, b):\n return a + b\n");
4444
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(additionFromString);
45-
Socket aSocket = step.getInputSockets()[0];
46-
Socket bSocket = step.getInputSockets()[1];
47-
Socket sumSocket = step.getOutputSockets()[0];
45+
Socket aSocket = step.getInputSockets().get(0);
46+
Socket bSocket = step.getInputSockets().get(1);
47+
Socket sumSocket = step.getOutputSockets().get(0);
4848

4949
aSocket.setValue(a);
5050
bSocket.setValue(b);
@@ -58,10 +58,10 @@ public void testPythonAdditionFromString() throws Exception {
5858
public void testPythonMultipleOutputs() throws Exception {
5959
Operation additionSubtraction = new PythonScriptOperation(PythonTest.class.getResource("/edu/wpi/grip/scripts/addition-subtraction.py"));
6060
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(additionSubtraction);
61-
Socket aSocket = step.getInputSockets()[0];
62-
Socket bSocket = step.getInputSockets()[1];
63-
Socket sumSocket = step.getOutputSockets()[0];
64-
Socket differenceSocket = step.getOutputSockets()[1];
61+
Socket aSocket = step.getInputSockets().get(0);
62+
Socket bSocket = step.getInputSockets().get(1);
63+
Socket sumSocket = step.getOutputSockets().get(0);
64+
Socket differenceSocket = step.getOutputSockets().get(1);
6565

6666
aSocket.setValue(a);
6767
bSocket.setValue(b);
@@ -76,9 +76,9 @@ public void testPythonMultipleOutputs() throws Exception {
7676
public void testPythonWrongOutputCount() throws Exception {
7777
Operation additionWrongOutputCount = new PythonScriptOperation(PythonTest.class.getResource("/edu/wpi/grip/scripts/addition-wrong-output-count.py"));
7878
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(additionWrongOutputCount);
79-
Socket aSocket = step.getInputSockets()[0];
80-
Socket bSocket = step.getInputSockets()[1];
81-
Socket sumSocket = step.getOutputSockets()[0];
79+
Socket aSocket = step.getInputSockets().get(0);
80+
Socket bSocket = step.getInputSockets().get(1);
81+
Socket sumSocket = step.getOutputSockets().get(0);
8282

8383
aSocket.setValue(a);
8484
bSocket.setValue(b);
@@ -90,9 +90,9 @@ public void testPythonWrongOutputCount() throws Exception {
9090
public void testPythonWrongOutputType() throws Exception {
9191
Operation additionWrongOutputType = new PythonScriptOperation(PythonTest.class.getResource("/edu/wpi/grip/scripts/addition-wrong-output-type.py"));
9292
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(additionWrongOutputType);
93-
Socket aSocket = step.getInputSockets()[0];
94-
Socket bSocket = step.getInputSockets()[1];
95-
Socket sumSocket = step.getOutputSockets()[0];
93+
Socket aSocket = step.getInputSockets().get(0);
94+
Socket bSocket = step.getInputSockets().get(1);
95+
Socket sumSocket = step.getOutputSockets().get(0);
9696

9797
aSocket.setValue(a);
9898
bSocket.setValue(b);

core/src/test/java/edu/wpi/grip/core/StepTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public void testOperationNotNull() {
2626
@Test
2727
public void testStep() {
2828
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(addition);
29-
Socket<Double> a = (Socket<Double>) step.getInputSockets()[0];
30-
Socket<Double> b = (Socket<Double>) step.getInputSockets()[1];
31-
Socket<Double> c = (Socket<Double>) step.getOutputSockets()[0];
29+
Socket<Double> a = (Socket<Double>) step.getInputSockets().get(0);
30+
Socket<Double> b = (Socket<Double>) step.getInputSockets().get(1);
31+
Socket<Double> c = (Socket<Double>) step.getOutputSockets().get(0);
3232

3333
a.setValue(1234.0);
3434
b.setValue(5678.0);
@@ -41,9 +41,9 @@ public void testStep() {
4141
@Test
4242
public void testSocketDirection() {
4343
Step step = new Step.Factory(eventBus, (origin) -> new MockExceptionWitness(eventBus, origin)).create(addition);
44-
Socket<Double> a = (Socket<Double>) step.getInputSockets()[0];
45-
Socket<Double> b = (Socket<Double>) step.getInputSockets()[1];
46-
Socket<Double> c = (Socket<Double>) step.getOutputSockets()[0];
44+
Socket<Double> a = (Socket<Double>) step.getInputSockets().get(0);
45+
Socket<Double> b = (Socket<Double>) step.getInputSockets().get(1);
46+
Socket<Double> c = (Socket<Double>) step.getOutputSockets().get(0);
4747

4848
assertEquals(Socket.Direction.INPUT, a.getDirection());
4949
assertEquals(Socket.Direction.INPUT, b.getDirection());

core/src/test/java/edu/wpi/grip/core/serialization/ProjectTest.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,14 @@ public void testSerializePipelineWithSteps() throws Exception {
122122
@SuppressWarnings("unchecked")
123123
public void testSerializePipelineWithStepsAndConnections() throws Exception {
124124
Step step1 = stepFactory.create(pythonAdditionOperationFromSource);
125-
InputSocket<Number> a1 = (InputSocket<Number>) step1.getInputSockets()[0];
126-
InputSocket<Number> b1 = (InputSocket<Number>) step1.getInputSockets()[1];
127-
OutputSocket<Number> sum1 = (OutputSocket<Number>) step1.getOutputSockets()[0];
125+
InputSocket<Number> a1 = (InputSocket<Number>) step1.getInputSockets().get(0);
126+
InputSocket<Number> b1 = (InputSocket<Number>) step1.getInputSockets().get(1);
127+
OutputSocket<Number> sum1 = (OutputSocket<Number>) step1.getOutputSockets().get(0);
128128

129129
Step step2 = stepFactory.create(pythonAdditionOperationFromURL);
130-
InputSocket<Number> a2 = (InputSocket<Number>) step2.getInputSockets()[0];
131-
InputSocket<Number> b2 = (InputSocket<Number>) step2.getInputSockets()[1];
132-
OutputSocket<Number> sum2 = (OutputSocket<Number>) step2.getOutputSockets()[0];
130+
InputSocket<Number> a2 = (InputSocket<Number>) step2.getInputSockets().get(0);
131+
InputSocket<Number> b2 = (InputSocket<Number>) step2.getInputSockets().get(1);
132+
OutputSocket<Number> sum2 = (OutputSocket<Number>) step2.getOutputSockets().get(0);
133133

134134
a1.setValue(12);
135135
b1.setValue(34);
@@ -156,9 +156,9 @@ public void testPerformSerializedStep() throws Exception {
156156

157157

158158
final Step fromPipeline = pipeline.getSteps().get(0);
159-
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets()[0];
160-
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets()[1];
161-
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets()[0];
159+
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets().get(0);
160+
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets().get(1);
161+
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets().get(0);
162162

163163
a.setValue(123.4);
164164
b.setValue(567.8);
@@ -175,9 +175,9 @@ public void testPerformSerializedPythonStepFromURL() throws Exception {
175175
serializeAndDeserialize();
176176

177177
final Step fromPipeline = pipeline.getSteps().get(0);
178-
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets()[0];
179-
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets()[1];
180-
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets()[0];
178+
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets().get(0);
179+
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets().get(1);
180+
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets().get(0);
181181

182182

183183
a.setValue(1234);
@@ -195,9 +195,9 @@ public void testPerformSerializedPythonStepFromSource() throws Exception {
195195
serializeAndDeserialize();
196196

197197
final Step fromPipeline = pipeline.getSteps().get(0);
198-
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets()[0];
199-
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets()[1];
200-
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets()[0];
198+
InputSocket<Number> a = (InputSocket<Number>) fromPipeline.getInputSockets().get(0);
199+
InputSocket<Number> b = (InputSocket<Number>) fromPipeline.getInputSockets().get(1);
200+
OutputSocket<Number> sum = (OutputSocket<Number>) fromPipeline.getOutputSockets().get(0);
201201

202202

203203
a.setValue(1234);
@@ -217,18 +217,18 @@ public void testPerformSerializedPipeline() throws Exception {
217217
pipeline.addStep(step2);
218218
eventBus.post(new ConnectionAddedEvent(
219219
connectionFactory.create(
220-
(OutputSocket) step1.getOutputSockets()[0],
221-
(InputSocket) step2.getInputSockets()[0]
220+
(OutputSocket) step1.getOutputSockets().get(0),
221+
(InputSocket) step2.getInputSockets().get(0)
222222
)));
223223
serializeAndDeserialize();
224224

225225
final Step step1Out = pipeline.getSteps().get(0);
226226
final Step step2Out = pipeline.getSteps().get(1);
227227

228-
InputSocket<Number> a1 = (InputSocket<Number>) step1Out.getInputSockets()[0];
229-
InputSocket<Number> b1 = (InputSocket<Number>) step1Out.getInputSockets()[1];
230-
InputSocket<Number> b2 = (InputSocket<Number>) step2Out.getInputSockets()[1];
231-
OutputSocket<Number> sum2 = (OutputSocket<Number>) step2Out.getOutputSockets()[0];
228+
InputSocket<Number> a1 = (InputSocket<Number>) step1Out.getInputSockets().get(0);
229+
InputSocket<Number> b1 = (InputSocket<Number>) step1Out.getInputSockets().get(1);
230+
InputSocket<Number> b2 = (InputSocket<Number>) step2Out.getInputSockets().get(1);
231+
OutputSocket<Number> sum2 = (OutputSocket<Number>) step2Out.getOutputSockets().get(0);
232232

233233

234234
a1.setValue(123);
@@ -247,9 +247,9 @@ public void testPerformSerializedPipelineWithMats() throws Exception {
247247
serializeAndDeserialize();
248248

249249
Step step1 = pipeline.getSteps().get(0);
250-
InputSocket<Mat> a = (InputSocket<Mat>) step1.getInputSockets()[0];
251-
InputSocket<Mat> b = (InputSocket<Mat>) step1.getInputSockets()[1];
252-
OutputSocket<Mat> sum = (OutputSocket<Mat>) step1.getOutputSockets()[0];
250+
InputSocket<Mat> a = (InputSocket<Mat>) step1.getInputSockets().get(0);
251+
InputSocket<Mat> b = (InputSocket<Mat>) step1.getInputSockets().get(1);
252+
OutputSocket<Mat> sum = (OutputSocket<Mat>) step1.getOutputSockets().get(0);
253253

254254

255255
a.setValue(new Mat(1, 1, CV_32F, new Scalar(1234.5)));

0 commit comments

Comments
 (0)