1010import java .util .List ;
1111import java .lang .Boolean ;
1212import java .util .Arrays ;
13-
14- public class BaseFluent <F extends io .kubernetes .client .fluent .Fluent <F >> implements io .kubernetes .client .fluent .Fluent <F >,io .kubernetes .client .fluent .Visitable <F > {
15-
16- public static final java .lang .String VISIT = "visit" ;
17- public final io .kubernetes .client .fluent .VisitableMap _visitables = new VisitableMap ();
18-
19- public static <T >io .kubernetes .client .fluent .VisitableBuilder <T ,?> builderOf (T item ) {
20- if (item instanceof Editable ) {
21- Object editor = ((Editable ) item ).edit ();
22- if (editor instanceof VisitableBuilder ) {
23- return (VisitableBuilder <T , ?>) editor ;
24- }
25- }
26- try {
27- return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" ).getConstructor (item .getClass ())
28- .newInstance (item );
29- } catch (Exception e ) {
30- throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e );
31- }
32- }
33-
34- public static <T >java .util .ArrayList <T > build (java .util .List <? extends io .kubernetes .client .fluent .Builder <? extends T >> list ) {
35- return list == null ? null : new ArrayList <T >(list .stream ().map (Builder ::build ).collect (Collectors .toList ()));
36- }
37-
38- public static <T >java .util .List <T > build (java .util .Set <? extends io .kubernetes .client .fluent .Builder <? extends T >> set ) {
39- return set == null ? null : set .stream ().map (Builder ::build ).collect (Collectors .toList ());
40- }
41-
42- public static <T >java .util .ArrayList <T > aggregate (java .util .List <? extends T >... lists ) {
43- return new ArrayList (Arrays .stream (lists ).filter (Objects ::nonNull ).collect (Collectors .toList ()));
44- }
45-
46- public static <T >java .util .LinkedHashSet <T > aggregate (java .util .Set <? extends T >[] sets ) {
47- return new LinkedHashSet (Arrays .stream (sets ).filter (Objects ::nonNull ).collect (Collectors .toSet ()));
48- }
49-
50- public static <V extends io .kubernetes .client .fluent .Visitor <?>,F >java .lang .Boolean canVisit (V visitor ,F fluent ) {
51- if (visitor instanceof TypedVisitor ) {
52- if (!((TypedVisitor ) visitor ).getType ().isAssignableFrom (fluent .getClass ())) {
53- return false ;
54- }
55- }
56- if (visitor instanceof PathAwareTypedVisitor ) {
57- PathAwareTypedVisitor pathAwareTypedVisitor = (PathAwareTypedVisitor ) visitor ;
58- Class parentType = pathAwareTypedVisitor .getParentType ();
59- Class actaulParentType = pathAwareTypedVisitor .getActualParentType ();
60- if (!parentType .isAssignableFrom (actaulParentType )) {
61- return false ;
62- }
63- }
64- return hasCompatibleVisitMethod (visitor , fluent );
65- }
66-
67- public static <V ,F >java .lang .Boolean hasCompatibleVisitMethod (V visitor ,F fluent ) {
68- for (java .lang .reflect .Method method : visitor .getClass ().getMethods ()) {
69- if (!method .getName ().equals (VISIT ) || method .getParameterTypes ().length != 1 ) {
70- continue ;
71- }
72- Class visitorType = method .getParameterTypes ()[0 ];
73- if (visitorType .isAssignableFrom (fluent .getClass ())) {
74- return true ;
75- } else {
76- return false ;
77- }
78- }
79- return false ;
80- }
81-
82- public F accept (io .kubernetes .client .fluent .Visitor ... visitors ) {
83- return isPathAwareVisitorArray (visitors ) ? acceptPathAware (asPathAwareVisitorArray (visitors )) : acceptInternal (visitors );
84- }
85-
86- public <V >F accept (java .lang .Class <V > type ,io .kubernetes .client .fluent .Visitor <V > visitor ) {
87- return accept (new TypedVisitor <V >() {
88- @ Override
89- public Class <V > getType () {
90- return type ;
91- }
92- @ Override
93- public void visit (V element ) {
94- visitor .visit (element );
95- }
96- });
97- }
98-
99- public F acceptInternal (io .kubernetes .client .fluent .Visitor <?>... visitors ) {
100- for (Visitor visitor : visitors ) {
101- for (Visitable visitable : _visitables ) {
102- visitable .accept (visitor );
103- }
104-
105- if (canVisit (visitor , this )) {
106- visitor .visit (this );
107- }
108- }
109- return (F ) this ;
110- }
111-
112- private F acceptPathAware (io .kubernetes .client .fluent .PathAwareTypedVisitor <?,?>... pathAwareTypedVisitors ) {
113- return acceptInternal (Arrays .stream (pathAwareTypedVisitors ).map (p -> p .next (this )).toArray (size -> new PathAwareTypedVisitor [size ]));
114- }
115-
116- private static boolean isPathAwareVisitorArray (io .kubernetes .client .fluent .Visitor <?>[] visitors ) {
117- return !Arrays .stream (visitors ).filter (v -> !(v instanceof PathAwareTypedVisitor )).findAny ().isPresent ();
118- }
119-
120- private static io .kubernetes .client .fluent .PathAwareTypedVisitor <?,?>[] asPathAwareVisitorArray (io .kubernetes .client .fluent .Visitor <?>[] visitors ) {
121- return Arrays .stream (visitors ).filter (v -> v instanceof PathAwareTypedVisitor ).map (v -> (PathAwareTypedVisitor ) v ).toArray (size -> new PathAwareTypedVisitor [size ]);
122- }
123-
13+ public class BaseFluent <F extends io .kubernetes .client .fluent .Fluent <F >> implements io .kubernetes .client .fluent .Fluent <F >,io .kubernetes .client .fluent .Visitable <F >{
14+ public static final java .lang .String VISIT = "visit" ;
15+ public final io .kubernetes .client .fluent .VisitableMap _visitables = new VisitableMap ();
16+ public static <T >io .kubernetes .client .fluent .VisitableBuilder <T ,?> builderOf (T item ) {
17+ if (item instanceof Editable ) {
18+ Object editor = ((Editable ) item ).edit ();
19+ if (editor instanceof VisitableBuilder ) {
20+ return (VisitableBuilder <T , ?>) editor ;
21+ }
22+ }
23+ try {
24+ return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" ).getConstructor (item .getClass ())
25+ .newInstance (item );
26+ } catch (Exception e ) {
27+ throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e );
28+ }
29+
30+ }
31+ public static <T >java .util .ArrayList <T > build (java .util .List <? extends io .kubernetes .client .fluent .Builder <? extends T >> list ) {
32+ return list == null ? null : new ArrayList <T >(list .stream ().map (Builder ::build ).collect (Collectors .toList ()));
33+ }
34+ public static <T >java .util .ArrayList <T > build (java .util .Set <? extends io .kubernetes .client .fluent .Builder <? extends T >> set ) {
35+ return set == null ? null : new ArrayList <>(set .stream ().map (Builder ::build ).collect (Collectors .toList ()));
36+ }
37+ public static <T >java .util .ArrayList <T > aggregate (java .util .List <? extends T >... lists ) {
38+ return new ArrayList (Arrays .stream (lists ).filter (Objects ::nonNull ).collect (Collectors .toList ()));
39+ }
40+ public static <T >java .util .LinkedHashSet <T > aggregate (java .util .Set <? extends T >[] sets ) {
41+ return new LinkedHashSet (Arrays .stream (sets ).filter (Objects ::nonNull ).collect (Collectors .toSet ()));
42+ }
43+ public static <V extends io .kubernetes .client .fluent .Visitor <?>,F >java .lang .Boolean canVisit (V visitor ,F fluent ) {
44+ if (visitor instanceof TypedVisitor ) {
45+ if (!((TypedVisitor ) visitor ).getType ().isAssignableFrom (fluent .getClass ())) {
46+ return false ;
47+ }
48+ }
49+ if (visitor instanceof PathAwareTypedVisitor ) {
50+ PathAwareTypedVisitor pathAwareTypedVisitor = (PathAwareTypedVisitor ) visitor ;
51+ Class parentType = pathAwareTypedVisitor .getParentType ();
52+ Class actaulParentType = pathAwareTypedVisitor .getActualParentType ();
53+ if (!parentType .isAssignableFrom (actaulParentType )) {
54+ return false ;
55+ }
56+ }
57+ return hasCompatibleVisitMethod (visitor , fluent );
58+
59+ }
60+ public static <V ,F >java .lang .Boolean hasCompatibleVisitMethod (V visitor ,F fluent ) {
61+ for (java .lang .reflect .Method method : visitor .getClass ().getMethods ()) {
62+ if (!method .getName ().equals (VISIT ) || method .getParameterTypes ().length != 1 ) {
63+ continue ;
64+ }
65+ Class visitorType = method .getParameterTypes ()[0 ];
66+ if (visitorType .isAssignableFrom (fluent .getClass ())) {
67+ return true ;
68+ } else {
69+ return false ;
70+ }
71+ }
72+ return false ;
73+ }
74+ public F accept (io .kubernetes .client .fluent .Visitor ... visitors ) {
75+ return isPathAwareVisitorArray (visitors ) ? acceptPathAware (asPathAwareVisitorArray (visitors )) : acceptInternal (visitors );
76+ }
77+ public <V >F accept (java .lang .Class <V > type ,io .kubernetes .client .fluent .Visitor <V > visitor ) {
78+ return accept (new TypedVisitor <V >() {
79+ @ Override
80+ public Class <V > getType () {
81+ return type ;
82+ }
83+ @ Override
84+ public void visit (V element ) {
85+ visitor .visit (element );
86+ }
87+ });
88+ }
89+ public F acceptInternal (io .kubernetes .client .fluent .Visitor <?>... visitors ) {
90+ for (Visitor visitor : visitors ) {
91+ for (Visitable visitable : _visitables ) {
92+ visitable .accept (visitor );
93+ }
94+
95+ if (canVisit (visitor , this )) {
96+ visitor .visit (this );
97+ }
12498}
99+ return (F ) this ;
100+ }
101+ private F acceptPathAware (io .kubernetes .client .fluent .PathAwareTypedVisitor <?,?>... pathAwareTypedVisitors ) {
102+ return acceptInternal (Arrays .stream (pathAwareTypedVisitors ).map (p -> p .next (this )).toArray (size -> new PathAwareTypedVisitor [size ]));
103+ }
104+ private static boolean isPathAwareVisitorArray (io .kubernetes .client .fluent .Visitor <?>[] visitors ) {
105+ return !Arrays .stream (visitors ).filter (v -> !(v instanceof PathAwareTypedVisitor )).findAny ().isPresent ();
106+ }
107+ private static io .kubernetes .client .fluent .PathAwareTypedVisitor <?,?>[] asPathAwareVisitorArray (io .kubernetes .client .fluent .Visitor <?>[] visitors ) {
108+ return Arrays .stream (visitors ).filter (v -> v instanceof PathAwareTypedVisitor ).map (v -> (PathAwareTypedVisitor ) v ).toArray (size -> new PathAwareTypedVisitor [size ]);
109+ }
110+
111+ }
0 commit comments