1- /*
2- Copyright 2022 The Kubernetes Authors.
3- Licensed under the Apache License, Version 2.0 (the "License");
4- you may not use this file except in compliance with the License.
5- You may obtain a copy of the License at
6- http://www.apache.org/licenses/LICENSE-2.0
7- Unless required by applicable law or agreed to in writing, software
8- distributed under the License is distributed on an "AS IS" BASIS,
9- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10- See the License for the specific language governing permissions and
11- limitations under the License.
12- */
131package io .kubernetes .client .fluent ;
142
15- import java .util .AbstractMap ;
16- import java .util .ArrayList ;
17- import java .util .Arrays ;
18- import java .util .Collections ;
193import java .util .LinkedHashSet ;
20- import java .util .List ;
214import java .util .Map .Entry ;
22- import java .util .Objects ;
23- import java .util .Set ;
245import java .util .stream .Collectors ;
25-
26- public class BaseFluent <F extends Fluent <F >> implements Fluent <F >, Visitable <F > {
6+ import java .util .Set ;
7+ import java .util .ArrayList ;
8+ import java .lang .String ;
9+ import java .util .AbstractMap ;
10+ import java .util .Objects ;
11+ import java .lang .Class ;
12+ import java .lang .Object ;
13+ import java .util .List ;
14+ import java .util .Arrays ;
15+ import java .util .Collections ;
16+ public class BaseFluent <F extends Fluent <F >> implements Fluent <F >,Visitable <F >{
2717 public static final String VISIT = "visit" ;
2818 public final VisitableMap _visitables = new VisitableMap ();
29-
30- public static <T > VisitableBuilder <T , ?> builderOf (T item ) {
19+ public static <T >VisitableBuilder <T ,?> builderOf (T item ) {
3120 if (item instanceof Editable ) {
3221 Object editor = ((Editable ) item ).edit ();
3322 if (editor instanceof VisitableBuilder ) {
@@ -36,122 +25,89 @@ public class BaseFluent<F extends Fluent<F>> implements Fluent<F>, Visitable<F>
3625 }
3726
3827 try {
39- return (VisitableBuilder <T , ?>)
40- Class .forName (item .getClass ().getName () + "Builder" )
41- .getConstructor (item .getClass ())
42- .newInstance (item );
28+ return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" ).getConstructor (item .getClass ())
29+ .newInstance (item );
4330 } catch (Exception e ) {
4431 throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e );
4532 }
4633 }
47-
48- public static <T > List <T > build (
49- List <? extends io .kubernetes .client .fluent .Builder <? extends T >> list ) {
50- return list == null
51- ? null
52- : new ArrayList <T >(list .stream ().map (Builder ::build ).collect (Collectors .toList ()));
34+ public static <T >List <T > build (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 ()));
5336 }
54-
55- public static <T > Set <T > build (
56- Set <? extends io .kubernetes .client .fluent .Builder <? extends T >> set ) {
57- return set == null
58- ? null
59- : new LinkedHashSet <T >(set .stream ().map (Builder ::build ).collect (Collectors .toSet ()));
37+ public static <T >Set <T > build (Set <? extends io .kubernetes .client .fluent .Builder <? extends T >> set ) {
38+ return set == null ? null : new LinkedHashSet <T >(set .stream ().map (Builder ::build ).collect (Collectors .toSet ()));
6039 }
61-
62- public static <T > List <T > aggregate (List <? extends T >... lists ) {
63- return new ArrayList (
64- Arrays .stream (lists ).filter (Objects ::nonNull ).collect (Collectors .toList ()));
40+ public static <T >List <T > aggregate (List <? extends T >... lists ) {
41+ return new ArrayList (Arrays .stream (lists ).filter (Objects ::nonNull ).collect (Collectors .toList ()));
6542 }
66-
67- public static <T > Set <T > aggregate (Set <? extends T >... sets ) {
68- return new LinkedHashSet (
69- Arrays .stream (sets ).filter (Objects ::nonNull ).collect (Collectors .toSet ()));
43+ public static <T >Set <T > aggregate (Set <? extends T >... sets ) {
44+ return new LinkedHashSet (Arrays .stream (sets ).filter (Objects ::nonNull ).collect (Collectors .toSet ()));
7045 }
71-
7246 public F accept (io .kubernetes .client .fluent .Visitor ... visitors ) {
7347 return accept (Collections .emptyList (), visitors );
7448 }
49+ public <V >F accept (Class <V > type ,Visitor <V > visitor ) {
50+ return accept (Collections .emptyList (), new Visitor <V >() {
51+ @ Override
52+ public Class <V > getType () {
53+ return type ;
54+ }
7555
76- public <V > F accept (Class <V > type , Visitor <V > visitor ) {
77- return accept (
78- Collections .emptyList (),
79- new Visitor <V >() {
80- @ Override
81- public Class <V > getType () {
82- return type ;
83- }
84-
85- @ Override
86- public void visit (List <Entry <String , Object >> path , V element ) {
87- visitor .visit (path , element );
88- }
56+ @ Override
57+ public void visit (List <Entry <String , Object >> path , V element ) {
58+ visitor .visit (path , element );
59+ }
8960
90- @ Override
91- public void visit (V element ) {
92- visitor .visit (element );
93- }
94- });
61+ @ Override
62+ public void visit (V element ) {
63+ visitor .visit (element );
64+ }
65+ });
9566 }
96-
97- public F accept (
98- List <Entry <String , Object >> path , io .kubernetes .client .fluent .Visitor ... visitors ) {
67+ public F accept (List <Entry <String ,Object >> path ,io .kubernetes .client .fluent .Visitor ... visitors ) {
9968 return accept (path , "" , visitors );
10069 }
101-
102- public F accept (
103- List <Entry <String , Object >> path ,
104- String currentKey ,
105- io .kubernetes .client .fluent .Visitor ... visitors ) {
70+ public F accept (List <Entry <String ,Object >> path ,String currentKey ,io .kubernetes .client .fluent .Visitor ... visitors ) {
10671 Arrays .stream (visitors )
10772 .map (v -> VisitorListener .wrap (v ))
10873 .filter (v -> ((Visitor ) v ).canVisit (path , this ))
10974 .sorted ((l , r ) -> ((Visitor ) r ).order () - ((Visitor ) l ).order ())
110- .forEach (
111- v -> {
112- ((Visitor ) v ).visit (path , this );
113- });
75+ .forEach (v -> {
76+ ((Visitor ) v ).visit (path , this );
77+ });
11478
11579 List <Entry <String , Object >> copyOfPath = path != null ? new ArrayList (path ) : new ArrayList <>();
11680 copyOfPath .add (new AbstractMap .SimpleEntry <String , Object >(currentKey , this ));
11781
118- _visitables .forEach (
119- (key , visitables ) -> {
120- List <Entry <String , Object >> newPath = Collections .unmodifiableList (copyOfPath );
121- // Copy visitables to avoid ConcurrrentModificationException when Visitors add/remove
122- // Visitables
123- new ArrayList <>(visitables )
124- .forEach (
125- visitable -> {
126- Arrays .stream (visitors )
127- .filter (
128- v ->
129- v .getType () != null
130- && v .getType ().isAssignableFrom (visitable .getClass ()))
131- .forEach (v -> visitable .accept (newPath , key , v ));
132-
133- Arrays .stream (visitors )
134- .filter (
135- v ->
136- v .getType () == null
137- || !v .getType ().isAssignableFrom (visitable .getClass ()))
138- .forEach (v -> visitable .accept (newPath , key , v ));
139- });
140- });
82+ _visitables .forEach ((key , visitables ) -> {
83+ List <Entry <String , Object >> newPath = Collections .unmodifiableList (copyOfPath );
84+ // Copy visitables to avoid ConcurrrentModificationException when Visitors add/remove Visitables
85+ new ArrayList <>(visitables ).forEach (visitable -> {
86+ Arrays .stream (visitors )
87+ .filter (v -> v .getType () != null && v .getType ().isAssignableFrom (visitable .getClass ()))
88+ .forEach (v -> visitable .accept (newPath , key , v ));
89+
90+ Arrays .stream (visitors )
91+ .filter (v -> v .getType () == null || !v .getType ().isAssignableFrom (visitable .getClass ()))
92+ .forEach (v -> visitable .accept (newPath , key , v ));
93+ });
94+ });
14195 return (F ) this ;
14296 }
143-
14497 public int hashCode () {
14598 final int prime = 31 ;
14699 int result = 1 ;
147100 result = prime * result + 0 ;
148101 return result ;
149102 }
150-
151103 public boolean equals (Object obj ) {
152- if (this == obj ) return true ;
153- if (obj == null ) return false ;
154- if (getClass () != obj .getClass ()) return false ;
104+ if (this == obj )
105+ return true ;
106+ if (obj == null )
107+ return false ;
108+ if (getClass () != obj .getClass ())
109+ return false ;
155110 return true ;
156111 }
157- }
112+
113+ }
0 commit comments