2020
2121import org .apache .tinkerpop .gremlin .process .traversal .Traversal ;
2222import org .apache .tinkerpop .gremlin .process .traversal .Traverser ;
23+ import org .apache .tinkerpop .gremlin .process .traversal .lambda .ConstantTraversal ;
2324import org .apache .tinkerpop .gremlin .process .traversal .lambda .GValueConstantTraversal ;
2425import org .apache .tinkerpop .gremlin .process .traversal .step .GValue ;
2526import org .apache .tinkerpop .gremlin .process .traversal .step .GValueHolder ;
@@ -55,58 +56,32 @@ public abstract class AbstractAddElementStepPlaceholder<S, E extends Element, X
5556 protected Parameters withConfiguration = new Parameters ();
5657
5758 protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final String label ) {
58- super (traversal );
59- this .label = label == null ? this .getDefaultLabel () : label ;
59+ this (traversal , (Object ) label );
6060 }
6161
6262 protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final GValue <String > label ) {
63- super (traversal );
64- this .label = label == null ? this .getDefaultLabel () : label ;
65- if (label != null && label .isVariable ()) {
66- traversal .getGValueManager ().register (label );
67- }
63+ this (traversal , (Object ) label );
6864 }
6965
7066 protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final Traversal .Admin <S ,?> labelTraversal ) {
71- super (traversal );
72- this .label = labelTraversal == null ? this .getDefaultLabel () : labelTraversal ;
73- if (labelTraversal instanceof GValueConstantTraversal ) {
74- traversal .getGValueManager ().register (((GValueConstantTraversal <S , String >) labelTraversal ).getGValue ());
75- }
76- addTraversal (labelTraversal );
77- }
78-
79- protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final Set <Object > labels ) {
80- super (traversal );
81- if (labels == null || labels .isEmpty ()) {
82- this .label = this .getDefaultLabel ();
83- } else {
84- this .label = labels ;
85- for (final Object l : labels ) {
86- if (l instanceof GValue && ((GValue <?>) l ).isVariable ()) {
87- traversal .getGValueManager ().register ((GValue <?>) l );
88- }
89- }
90- }
67+ this (traversal , (Object ) labelTraversal );
9168 }
9269
9370 /**
94- * Constructor for multiple label traversals. Each traversal resolves to a single String label
95- * at execution time.
71+ * Constructor for multiple labels, each of which may be a {@code String}, a {@link GValue}, or a
72+ * {@link Traversal.Admin} that resolves to a single {@code String} label at execution time.
9673 *
9774 * @param traversal the parent traversal
98- * @param labelTraversals the list of label-producing traversals (must have 2+ elements)
75+ * @param labels the labels to add (must have 2+ elements; {@code null} or empty results in the default label )
9976 */
100- protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final List <Traversal .Admin <?, ?>> labelTraversals ) {
77+ protected AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final Collection <Object > labels ) {
78+ this (traversal , (Object ) labels );
79+ }
80+
81+ private AbstractAddElementStepPlaceholder (final Traversal .Admin traversal , final Object label ) {
10182 super (traversal );
102- if (labelTraversals == null || labelTraversals .isEmpty ()) {
103- this .label = this .getDefaultLabel ();
104- } else {
105- this .label = labelTraversals ;
106- for (final Traversal .Admin <?, ?> t : labelTraversals ) {
107- addTraversal (t );
108- }
109- }
83+ this .label = this .getDefaultLabel ();
84+ if (label != null ) setLabel (label );
11085 }
11186
11287 protected abstract String getDefaultLabel ();
@@ -136,8 +111,8 @@ protected void addTraversal(final Traversal.Admin<?, ?> traversal) {
136111 }
137112 if (label != null && label instanceof Traversal ) {
138113 childTraversals .add (((Traversal <?, ?>) label ).asAdmin ());
139- } else if (label instanceof List ) {
140- for (final Object item : (List <?>) label ) {
114+ } else if (label instanceof Collection ) {
115+ for (final Object item : (Collection <?>) label ) {
141116 if (item instanceof Traversal ) {
142117 childTraversals .add (((Traversal <?, ?>) item ).asAdmin ());
143118 }
@@ -206,8 +181,8 @@ public boolean isParameterized() {
206181 (elementId instanceof GValue && ((GValue <?>) elementId ).isVariable ())) {
207182 return true ;
208183 }
209- if (label instanceof Set ) {
210- for (final Object l : (Set <?>) label ) {
184+ if (label instanceof Collection ) {
185+ for (final Object l : (Collection <?>) label ) {
211186 if (l instanceof GValue && ((GValue <?>) l ).isVariable ()) {
212187 return true ;
213188 }
@@ -227,41 +202,76 @@ public Object getLabel() {
227202 traversal .getGValueManager ().pinVariable (((GValue <?>) label ).getName ());
228203 return ((GValue <?>) label ).get ();
229204 }
230- if (label instanceof Set ) {
231- final Set <String > resolved = new LinkedHashSet <>();
232- for (final Object l : (Set <?>) label ) {
233- if (l instanceof GValue ) {
234- traversal .getGValueManager ().pinVariable (((GValue <?>) l ).getName ());
235- resolved .add ((String ) ((GValue <?>) l ).get ());
236- } else {
237- resolved .add ((String ) l );
238- }
205+ if (label instanceof Collection ) {
206+ final Set <Object > resolved = new LinkedHashSet <>();
207+ for (final Object l : (Collection <?>) label ) {
208+ resolved .add (resolveLabelElement (l ));
239209 }
240210 return resolved ;
241211 }
212+ if (label instanceof Traversal ) {
213+ return resolveLabelElement (label );
214+ }
242215 return label ;
243216 }
244217
218+ /**
219+ * Resolves a single label element. Handles {@code GValue}, {@code ConstantTraversal}, and
220+ * {@code GValueConstantTraversal}, which can be resolved to a {@code String} without a live
221+ * {@code Traverser}. Any other {@code Traversal} cannot be resolved statically and is returned as-is.
222+ */
223+ private Object resolveLabelElement (final Object l ) {
224+ if (l instanceof GValue ) {
225+ traversal .getGValueManager ().pinVariable (((GValue <?>) l ).getName ());
226+ return ((GValue <?>) l ).get ();
227+ }
228+ if (l instanceof GValueConstantTraversal ) {
229+ traversal .getGValueManager ().pinVariable (((GValueConstantTraversal <?, ?>) l ).getGValue ().getName ());
230+ return ((GValueConstantTraversal <?, ?>) l ).next ();
231+ }
232+ if (l instanceof ConstantTraversal ) {
233+ return ((ConstantTraversal <?, ?>) l ).next ();
234+ }
235+ return l ;
236+ }
237+
245238 @ Override
246239 public Object getLabelWithGValue () {
247240 return label ;
248241 }
249242
250243 @ Override
251244 public void setLabel (Object label ) {
252- if (this .label .equals (this .getDefaultLabel ())) {
253- if (label instanceof Traversal ) {
254- this .label = ((Traversal <S , String >) label ).asAdmin ();
255- this .integrateChild ((Traversal .Admin <?, ?>) this .label );
256- } else if (label instanceof GValue ) {
257- this .label = label ;
245+ if (!this .label .equals (this .getDefaultLabel ())) {
246+ throw new IllegalArgumentException (String .format ("Element T.label has already been set to [%s] and cannot be overridden with [%s]" ,
247+ this .label , label ));
248+ }
249+ if (label instanceof Traversal ) {
250+ this .label = ((Traversal <S , String >) label ).asAdmin ();
251+ if (label instanceof GValueConstantTraversal ) {
252+ traversal .getGValueManager ().register (((GValueConstantTraversal <S , String >) label ).getGValue ());
253+ }
254+ addTraversal ((Traversal .Admin <?, ?>) this .label );
255+ } else if (label instanceof GValue ) {
256+ this .label = label ;
257+ if (((GValue <?>) label ).isVariable ()) {
258258 traversal .getGValueManager ().register ((GValue <?>) label );
259- } else {
260- this .label = label ;
259+ }
260+ } else if (label instanceof Collection ) {
261+ final Collection <?> labels = (Collection <?>) label ;
262+ if (labels .isEmpty ()) {
263+ return ;
264+ }
265+ this .label = new LinkedHashSet <>(labels );
266+ for (final Object l : labels ) {
267+ if (l instanceof Traversal ) {
268+ addTraversal (((Traversal <?, ?>) l ).asAdmin ());
269+ } else if (l instanceof GValue && ((GValue <?>) l ).isVariable ()) {
270+ traversal .getGValueManager ().register ((GValue <?>) l );
271+ }
261272 }
262273 } else {
263- throw new IllegalArgumentException (String .format ("Element T.label has already been set to [%s] and cannot be overridden with [%s]" ,
264- this .label , label ));
274+ this .label = label ;
265275 }
266276 }
267277
@@ -364,10 +374,10 @@ public void setElementId(Object elementId) {
364374 public void updateVariable (String name , Object value ) {
365375 if (label instanceof GValue && name .equals (((GValue <?>) label ).getName ())) {
366376 label = GValue .of (name , value );
367- } else if (label instanceof Set ) {
377+ } else if (label instanceof Collection ) {
368378 final LinkedHashSet <Object > updated = new LinkedHashSet <>();
369379 boolean changed = false ;
370- for (final Object l : (Set <?>) label ) {
380+ for (final Object l : (Collection <?>) label ) {
371381 if (l instanceof GValue && name .equals (((GValue <?>) l ).getName ())) {
372382 updated .add (GValue .of (name , value ));
373383 changed = true ;
@@ -396,8 +406,8 @@ public Collection<GValue<?>> getGValues() {
396406 Set <GValue <?>> gValues = GValueHelper .getGValuesFromProperties (properties );
397407 if (label instanceof GValue && ((GValue <?>) label ).isVariable ()) {
398408 gValues .add ((GValue <?>) label );
399- } else if (label instanceof Set ) {
400- for (final Object l : (Set <?>) label ) {
409+ } else if (label instanceof Collection ) {
410+ for (final Object l : (Collection <?>) label ) {
401411 if (l instanceof GValue && ((GValue <?>) l ).isVariable ()) {
402412 gValues .add ((GValue <?>) l );
403413 }
@@ -423,11 +433,13 @@ public AbstractAddElementStepPlaceholder<S, E, X> clone() {
423433 clone .label = ((Traversal <?, ?>) this .label ).asAdmin ().clone ();
424434 } else if (this .label instanceof GValue ) {
425435 clone .label = ((GValue <?>) this .label ).clone ();
426- } else if (this .label instanceof Set ) {
436+ } else if (this .label instanceof Collection ) {
427437 final LinkedHashSet <Object > clonedSet = new LinkedHashSet <>();
428- for (final Object l : (Set <?>) this .label ) {
438+ for (final Object l : (Collection <?>) this .label ) {
429439 if (l instanceof GValue ) {
430440 clonedSet .add (((GValue <?>) l ).clone ());
441+ } else if (l instanceof Traversal ) {
442+ clonedSet .add (((Traversal <?, ?>) l ).asAdmin ().clone ());
431443 } else {
432444 clonedSet .add (l );
433445 }
0 commit comments