1
1
package io .kubernetes .client .fluent ;
2
2
3
3
import java .util .LinkedHashSet ;
4
- import java .util .Map .Entry ;
5
4
import java .util .stream .Collectors ;
6
5
import java .util .Set ;
6
+ import java .util .Optional ;
7
7
import java .util .ArrayList ;
8
- import java .lang .String ;
9
- import java .util .AbstractMap ;
10
8
import java .util .Objects ;
11
- import java .lang .Class ;
12
9
import java .lang .Object ;
13
10
import java .util .List ;
11
+ import java .lang .String ;
14
12
import java .util .Arrays ;
15
- import java . util . Collections ;
16
- public class BaseFluent < F extends Fluent < F >> implements Fluent < F >, Visitable < F >{
13
+ public class BaseFluent < F >{
14
+
17
15
public static final String VISIT = "visit" ;
18
16
public final VisitableMap _visitables = new VisitableMap ();
17
+
19
18
public static <T >VisitableBuilder <T ,?> builderOf (T item ) {
20
19
if (item instanceof Editable ) {
21
- Object editor = ((Editable ) item ).edit ();
22
- if (editor instanceof VisitableBuilder ) {
23
- return (VisitableBuilder <T , ?>) editor ;
24
- }
25
- }
26
-
27
- try {
28
- return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" , true , item .getClass ().getClassLoader ()).getConstructor (item .getClass ())
29
- .newInstance (item );
30
- } catch (Exception e ) {
31
- try {
32
- return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" ).getConstructor (item .getClass ())
33
- .newInstance (item );
34
- } catch (Exception e1 ) {
35
- throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e1 );
36
- }
37
- }
20
+ Object editor = ((Editable ) item ).edit ();
21
+ if (editor instanceof VisitableBuilder ) {
22
+ return (VisitableBuilder <T , ?>) editor ;
23
+ }
24
+ }
25
+
26
+ try {
27
+ return (VisitableBuilder <T , ?>) Class
28
+ .forName (item .getClass ().getName () + "Builder" , true , item .getClass ().getClassLoader ())
29
+ .getConstructor (item .getClass ())
30
+ .newInstance (item );
31
+ } catch (Exception e ) {
32
+ try {
33
+ return (VisitableBuilder <T , ?>) Class .forName (item .getClass ().getName () + "Builder" ).getConstructor (item .getClass ())
34
+ .newInstance (item );
35
+ } catch (Exception e1 ) {
36
+ throw new IllegalStateException ("Failed to create builder for: " + item .getClass (), e1 );
37
+ }
38
+ }
38
39
}
40
+
39
41
public static <T >List <T > build (List <? extends io .kubernetes .client .fluent .Builder <? extends T >> list ) {
40
42
return list == null ? null : list .stream ().map (Builder ::build ).collect (Collectors .toList ());
41
43
}
44
+
42
45
public static <T >Set <T > build (Set <? extends io .kubernetes .client .fluent .Builder <? extends T >> set ) {
43
46
return set == null ? null : new LinkedHashSet <T >(set .stream ().map (Builder ::build ).collect (Collectors .toSet ()));
44
47
}
48
+
45
49
public static <T >List <T > aggregate (List <? extends T >... lists ) {
46
50
return new ArrayList (Arrays .stream (lists ).filter (Objects ::nonNull ).collect (Collectors .toList ()));
47
51
}
52
+
48
53
public static <T >Set <T > aggregate (Set <? extends T >... sets ) {
49
54
return new LinkedHashSet (Arrays .stream (sets ).filter (Objects ::nonNull ).collect (Collectors .toSet ()));
50
55
}
51
- public F accept (io .kubernetes .client .fluent .Visitor ... visitors ) {
52
- return accept (Collections .emptyList (), visitors );
53
- }
54
- public <V >F accept (Class <V > type ,Visitor <V > visitor ) {
55
- return accept (Collections .emptyList (), new Visitor <V >() {
56
- @ Override
57
- public Class <V > getType () {
58
- return type ;
59
- }
60
-
61
- @ Override
62
- public void visit (List <Entry <String , Object >> path , V element ) {
63
- visitor .visit (path , element );
64
- }
65
-
66
- @ Override
67
- public void visit (V element ) {
68
- visitor .visit (element );
69
- }
70
- });
71
- }
72
- public F accept (List <Entry <String ,Object >> path ,io .kubernetes .client .fluent .Visitor ... visitors ) {
73
- return accept (path , "" , visitors );
74
- }
75
- public F accept (List <Entry <String ,Object >> path ,String currentKey ,io .kubernetes .client .fluent .Visitor ... visitors ) {
76
- List <Visitor > sortedVisitor = new ArrayList <>();
77
- for (Visitor visitor : visitors ) {
78
- visitor = VisitorListener .wrap (visitor );
79
- if (!visitor .canVisit (path , this )) {
80
- continue ;
81
- }
82
- sortedVisitor .add (visitor );
83
- }
84
- sortedVisitor .sort ((l , r ) -> ((Visitor ) r ).order () - ((Visitor ) l ).order ());
85
- for (Visitor visitor : sortedVisitor ) {
86
- visitor .visit (path , this );
87
- }
88
-
89
- List <Entry <String , Object >> copyOfPath = path != null ? new ArrayList (path ) : new ArrayList <>();
90
- copyOfPath .add (new AbstractMap .SimpleEntry <>(currentKey , this ));
91
-
92
- for (Entry <String , ?> entry : _visitables .entrySet ()) {
93
- List <Entry <String , Object >> newPath = Collections .unmodifiableList (copyOfPath );
94
-
95
- // Copy visitables to avoid ConcurrentModificationException when Visitors add/remove Visitables
96
- for (Visitable <F > visitable : new ArrayList <>((List <Visitable <F >>) entry .getValue ())) {
97
- for (Visitor visitor : visitors ) {
98
- if (visitor .getType () != null && visitor .getType ().isAssignableFrom (visitable .getClass ())) {
99
- visitable .accept (newPath , entry .getKey (), visitor );
100
- }
101
- }
102
-
103
- for (Visitor visitor : visitors ) {
104
- if (visitor .getType () == null || !visitor .getType ().isAssignableFrom (visitable .getClass ())) {
105
- visitable .accept (newPath , entry .getKey (), visitor );
106
- }
107
- }
108
- }
109
- }
110
- return (F ) this ;
56
+
57
+ public Optional <VisitableMap > getVisitableMap () {
58
+ return Optional .of (_visitables );
111
59
}
60
+
112
61
public int hashCode () {
113
62
final int prime = 31 ;
114
- int result = 1 ;
115
- result = prime * result + 0 ;
116
- return result ;
63
+ int result = 1 ;
64
+ result = prime * result + 0 ;
65
+ return result ;
117
66
}
67
+
118
68
public boolean equals (Object obj ) {
119
69
if (this == obj )
120
- return true ;
121
- if (obj == null )
122
- return false ;
123
- if (getClass () != obj .getClass ())
124
- return false ;
125
- return true ;
70
+ return true ;
71
+ if (obj == null )
72
+ return false ;
73
+ if (getClass () != obj .getClass ())
74
+ return false ;
75
+ return true ;
126
76
}
127
77
78
+
128
79
}
0 commit comments