1
1
package edu .wpi .grip .core ;
2
2
3
3
import edu .wpi .grip .core .events .OperationAddedEvent ;
4
+ import edu .wpi .grip .core .sockets .Socket ;
4
5
6
+ import com .google .common .collect .ImmutableList ;
5
7
import com .google .common .eventbus .Subscribe ;
6
8
7
9
import java .util .Collection ;
8
10
import java .util .LinkedHashMap ;
11
+ import java .util .List ;
9
12
import java .util .Map ;
10
13
import java .util .Optional ;
14
+ import java .util .function .Function ;
15
+ import java .util .stream .Collectors ;
11
16
12
17
import javax .inject .Singleton ;
13
18
@@ -24,10 +29,54 @@ public class Palette {
24
29
25
30
@ Subscribe
26
31
public void onOperationAdded (OperationAddedEvent event ) {
27
- final OperationMetaData operation = event .getOperation ();
28
- map (operation .getDescription ().name (), operation );
29
- for (String alias : operation .getDescription ().aliases ()) {
30
- map (alias , operation );
32
+ final OperationMetaData operationData = event .getOperation ();
33
+ map (operationData .getDescription ().name (), operationData );
34
+ for (String alias : operationData .getDescription ().aliases ()) {
35
+ map (alias , operationData );
36
+ }
37
+ // Validate that every input and output socket has a unique name and UID
38
+ Operation operation = operationData .getOperationSupplier ().get ();
39
+ try {
40
+ final List <? extends Socket > sockets = new ImmutableList .Builder <Socket >()
41
+ .addAll (operation .getInputSockets ())
42
+ .addAll (operation .getOutputSockets ())
43
+ .build ();
44
+ checkDuplicates (
45
+ operationData ,
46
+ "input socket names" ,
47
+ operation .getInputSockets (), s -> s .getSocketHint ().getIdentifier ()
48
+ );
49
+ checkDuplicates (
50
+ operationData ,
51
+ "output socket names" ,
52
+ operation .getOutputSockets (), s -> s .getSocketHint ().getIdentifier ()
53
+ );
54
+ checkDuplicates (operationData , "socket IDs" , sockets , Socket ::getUid );
55
+ } finally {
56
+ operation .cleanUp ();
57
+ }
58
+ }
59
+
60
+ private static <T , U > void checkDuplicates (OperationMetaData operationMetaData ,
61
+ String type ,
62
+ List <T > list ,
63
+ Function <T , U > extractionFunction ) {
64
+ List <U > duplicates = list .stream ()
65
+ .map (extractionFunction )
66
+ .collect (Collectors .toList ());
67
+ list .stream ()
68
+ .map (extractionFunction )
69
+ .distinct ()
70
+ .forEach (duplicates ::remove );
71
+ if (!duplicates .isEmpty ()) {
72
+ throw new IllegalArgumentException (
73
+ String .format (
74
+ "Duplicate %s found in operation %s: %s" ,
75
+ type ,
76
+ operationMetaData .getDescription ().name (),
77
+ duplicates
78
+ )
79
+ );
31
80
}
32
81
}
33
82
@@ -36,6 +85,7 @@ public void onOperationAdded(OperationAddedEvent event) {
36
85
*
37
86
* @param key The key the operation should be mapped to
38
87
* @param operation The operation to map the key to
88
+ *
39
89
* @throws IllegalArgumentException if the key is already in the {@link #operations} map.
40
90
*/
41
91
private void map (String key , OperationMetaData operation ) {
0 commit comments