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
- */
13
1
package io .kubernetes .client .fluent ;
14
2
15
- import java .util .AbstractMap ;
16
- import java .util .ArrayList ;
17
- import java .util .Arrays ;
18
- import java .util .Collections ;
19
3
import java .util .LinkedHashSet ;
20
- import java .util .List ;
21
4
import java .util .Map .Entry ;
22
- import java .util .Objects ;
23
- import java .util .Set ;
24
5
import 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 >{
27
17
public static final String VISIT = "visit" ;
28
18
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 ) {
31
20
if (item instanceof Editable ) {
32
21
Object editor = ((Editable ) item ).edit ();
33
22
if (editor instanceof VisitableBuilder ) {
@@ -36,122 +25,89 @@ public class BaseFluent<F extends Fluent<F>> implements Fluent<F>, Visitable<F>
36
25
}
37
26
38
27
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 );
43
30
} catch (Exception e ) {
44
31
throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e );
45
32
}
46
33
}
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 ()));
53
36
}
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 ()));
60
39
}
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 ()));
65
42
}
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 ()));
70
45
}
71
-
72
46
public F accept (io .kubernetes .client .fluent .Visitor ... visitors ) {
73
47
return accept (Collections .emptyList (), visitors );
74
48
}
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
+ }
75
55
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
+ }
89
60
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
+ });
95
66
}
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 ) {
99
68
return accept (path , "" , visitors );
100
69
}
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 ) {
106
71
Arrays .stream (visitors )
107
72
.map (v -> VisitorListener .wrap (v ))
108
73
.filter (v -> ((Visitor ) v ).canVisit (path , this ))
109
74
.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
+ });
114
78
115
79
List <Entry <String , Object >> copyOfPath = path != null ? new ArrayList (path ) : new ArrayList <>();
116
80
copyOfPath .add (new AbstractMap .SimpleEntry <String , Object >(currentKey , this ));
117
81
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
+ });
141
95
return (F ) this ;
142
96
}
143
-
144
97
public int hashCode () {
145
98
final int prime = 31 ;
146
99
int result = 1 ;
147
100
result = prime * result + 0 ;
148
101
return result ;
149
102
}
150
-
151
103
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 ;
155
110
return true ;
156
111
}
157
- }
112
+
113
+ }
0 commit comments