1616
1717import com .google .auto .value .AutoValue ;
1818import dev .cel .common .ast .CelExpr ;
19- import dev .cel .common .ast .CelExpr .CelComprehension ;
20- import dev .cel .common .ast .CelExpr .ExprKind ;
2119import java .util .Optional ;
2220import java .util .stream .Stream ;
2321
2422/**
25- * CelNavigableExpr represents the base navigable expression value with methods to inspect the
26- * parent and child expressions .
23+ * CelNavigableExpr decorates {@link CelExpr} with capabilities to inspect the parent and its
24+ * descendants with ease .
2725 */
2826@ AutoValue
29- public abstract class CelNavigableExpr {
30-
31- /**
32- * Specifies the traversal order of AST navigation.
33- *
34- * <p>For call expressions, the target is visited before its arguments.
35- *
36- * <p>For comprehensions, the visiting order is as follows:
37- *
38- * <ol>
39- * <li>{@link CelComprehension#iterRange}
40- * <li>{@link CelComprehension#accuInit}
41- * <li>{@link CelComprehension#loopCondition}
42- * <li>{@link CelComprehension#loopStep}
43- * <li>{@link CelComprehension#result}
44- * </ol>
45- */
46- public enum TraversalOrder {
47- PRE_ORDER ,
48- POST_ORDER
49- }
50-
51- public abstract CelExpr expr ();
52-
53- public long id () {
54- return expr ().id ();
55- }
56-
57- public abstract Optional <CelNavigableExpr > parent ();
58-
59- /** Represents the count of transitive parents. Depth of an AST's root is 0. */
60- public abstract int depth ();
61-
62- /**
63- * Represents the maximum count of children from any of its branches. Height of a leaf node is 0.
64- * For example, the height of the call node 'func' in expression `(1 + 2 + 3).func(4 + 5)` is 3.
65- */
66- public abstract int height ();
67-
27+ // unchecked: Generic types are properly bound to BaseNavigableExpr
28+ // redundant override: Overriding is required to specify the return type to a concrete type.
29+ @ SuppressWarnings ({"unchecked" , "RedundantOverride" })
30+ public abstract class CelNavigableExpr extends BaseNavigableExpr <CelExpr > {
6831 /** Constructs a new instance of {@link CelNavigableExpr} from {@link CelExpr}. */
6932 public static CelNavigableExpr fromExpr (CelExpr expr ) {
70- ExprHeightCalculator exprHeightCalculator = new ExprHeightCalculator (expr );
71-
72- return CelNavigableExpr .builder ()
73- .setExpr (expr )
74- .setHeight (exprHeightCalculator .getHeight (expr .id ()))
75- .build ();
33+ ExprHeightCalculator <CelExpr > exprHeightCalculator = new ExprHeightCalculator <>(expr );
34+ return builder ().setExpr (expr ).setHeight (exprHeightCalculator .getHeight (expr .id ())).build ();
7635 }
7736
78- /**
79- * Returns a stream of {@link CelNavigableExpr} collected from the current node down to the last
80- * leaf-level member using post-order traversal.
81- */
37+ @ Override
8238 public Stream <CelNavigableExpr > allNodes () {
83- return allNodes (TraversalOrder . POST_ORDER );
39+ return super . allNodes ();
8440 }
8541
86- /**
87- * Returns a stream of {@link CelNavigableExpr} collected from the current node down to the last
88- * leaf-level member using the specified traversal order.
89- */
42+ @ Override
9043 public Stream <CelNavigableExpr > allNodes (TraversalOrder traversalOrder ) {
91- return CelNavigableExprVisitor . collect ( this , traversalOrder );
44+ return super . allNodes ( traversalOrder );
9245 }
9346
94- /**
95- * Returns a stream of {@link CelNavigableExpr} collected down to the last leaf-level member using
96- * post-order traversal.
97- */
47+ @ Override
48+ public abstract Optional < CelNavigableExpr > parent ();
49+
50+ @ Override
9851 public Stream <CelNavigableExpr > descendants () {
99- return descendants (TraversalOrder . POST_ORDER );
52+ return super . descendants ();
10053 }
10154
102- /**
103- * Returns a stream of {@link CelNavigableExpr} collected down to the last leaf-level member using
104- * the specified traversal order.
105- */
55+ @ Override
10656 public Stream <CelNavigableExpr > descendants (TraversalOrder traversalOrder ) {
107- return CelNavigableExprVisitor .collect (this , traversalOrder )
108- .filter (node -> node .depth () > this .depth ());
57+ return super .descendants (traversalOrder );
10958 }
11059
111- /**
112- * Returns a stream of {@link CelNavigableExpr} collected from its immediate children using
113- * post-order traversal.
114- */
60+ @ Override
11561 public Stream <CelNavigableExpr > children () {
116- return children (TraversalOrder . POST_ORDER );
62+ return super . children ();
11763 }
11864
119- /**
120- * Returns a stream of {@link CelNavigableExpr} collected from its immediate children using the
121- * specified traversal order.
122- */
65+ @ Override
12366 public Stream <CelNavigableExpr > children (TraversalOrder traversalOrder ) {
124- return CelNavigableExprVisitor .collect (this , this .depth () + 1 , traversalOrder )
125- .filter (node -> node .depth () > this .depth ());
67+ return super .children (traversalOrder );
12668 }
12769
128- /** Returns the underlying kind of the {@link CelExpr}. */
129- public ExprKind . Kind getKind () {
130- return expr (). exprKind (). getKind ();
70+ @ Override
71+ public Builder builderFromInstance () {
72+ return builder ();
13173 }
13274
13375 /** Create a new builder to construct a {@link CelNavigableExpr} instance. */
@@ -137,25 +79,20 @@ public static Builder builder() {
13779
13880 /** Builder to configure {@link CelNavigableExpr}. */
13981 @ AutoValue .Builder
140- public abstract static class Builder {
82+ public abstract static class Builder
83+ implements BaseNavigableExpr .Builder <CelExpr , CelNavigableExpr > {
14184
142- public abstract CelExpr expr ();
143-
144- public abstract int depth ();
145-
146- public ExprKind .Kind getKind () {
147- return expr ().exprKind ().getKind ();
148- }
85+ @ Override
86+ public abstract Builder setParent (CelNavigableExpr value );
14987
88+ @ Override
15089 public abstract Builder setExpr (CelExpr value );
15190
152- abstract Builder setParent (CelNavigableExpr value );
153-
91+ @ Override
15492 public abstract Builder setDepth (int value );
15593
94+ @ Override
15695 public abstract Builder setHeight (int value );
157-
158- public abstract CelNavigableExpr build ();
15996 }
16097
16198 public abstract Builder toBuilder ();
0 commit comments