22
33
44import java .util .ArrayList ;
5+ import java .util .Deque ;
56import java .util .HashSet ;
67import java .util .LinkedHashMap ;
8+ import java .util .LinkedList ;
79import java .util .List ;
810import java .util .Map ;
911
@@ -25,7 +27,6 @@ public class NoFragmentCycles extends AbstractRule {
2527 private final Map <String , List <FragmentSpread >> fragmentSpreads = new LinkedHashMap <>();
2628 private final HashSet <String > checked = new HashSet <>();
2729
28-
2930 public NoFragmentCycles (ValidationContext validationContext , ValidationErrorCollector validationErrorCollector ) {
3031 super (validationContext , validationErrorCollector );
3132 prepareFragmentMap ();
@@ -65,16 +66,17 @@ public void leave(Node node, List<Node> path) {
6566
6667 @ Override
6768 public void checkFragmentDefinition (FragmentDefinition fragmentDefinition ) {
68- List <FragmentSpread > spreadPath = new ArrayList <>();
69+ Deque <FragmentSpread > spreadPath = new LinkedList <>();
6970 detectCycleRecursive (fragmentDefinition .getName (), fragmentDefinition .getName (), spreadPath );
7071 }
7172
72- private void detectCycleRecursive (String fragmentName , String initialName , List <FragmentSpread > spreadPath ) {
73- List <FragmentSpread > fragmentSpreads = this .fragmentSpreads .get (fragmentName );
73+ private void detectCycleRecursive (String fragmentName , String initialName , Deque <FragmentSpread > spreadPath ) {
7474 if (checked .contains (fragmentName )) {
7575 return ;
7676 }
7777
78+ List <FragmentSpread > fragmentSpreads = this .fragmentSpreads .get (fragmentName );
79+
7880 if (fragmentSpreads == null ) {
7981 // KnownFragmentNames will have picked this up. Lets not NPE
8082 return ;
@@ -91,8 +93,6 @@ private void detectCycleRecursive(String fragmentName, String initialName, List<
9193 * It also *certainly* repeats work
9294 */
9395
94-
95-
9696 outer :
9797 for (FragmentSpread fragmentSpread : fragmentSpreads ) {
9898
@@ -106,9 +106,9 @@ private void detectCycleRecursive(String fragmentName, String initialName, List<
106106 continue outer ;
107107 }
108108 }
109- spreadPath .add (fragmentSpread );
109+ spreadPath .push (fragmentSpread );
110110 detectCycleRecursive (fragmentSpread .getName (), initialName , spreadPath );
111- spreadPath .remove ( spreadPath . size () - 1 );
111+ spreadPath .pop ( );
112112 }
113113 checked .add (fragmentName );
114114 }
0 commit comments