1
1
package io .javaoperatorsdk .operator .processing .event ;
2
2
3
3
import io .fabric8 .kubernetes .client .CustomResource ;
4
+ import java .util .Objects ;
4
5
import java .util .function .Predicate ;
5
6
6
7
@ SuppressWarnings ("rawtypes" )
7
8
public class DefaultEvent implements Event {
8
-
9
- private final String relatedCustomResourceUid ;
10
9
private final Predicate <CustomResource > customResourcesSelector ;
11
10
private final EventSource eventSource ;
12
11
13
12
public DefaultEvent (String relatedCustomResourceUid , EventSource eventSource ) {
14
- this .relatedCustomResourceUid = relatedCustomResourceUid ;
15
- this .customResourcesSelector = null ;
13
+ this .customResourcesSelector = new UIDMatchingPredicate (relatedCustomResourceUid );
16
14
this .eventSource = eventSource ;
17
15
}
18
16
19
17
public DefaultEvent (Predicate <CustomResource > customResourcesSelector , EventSource eventSource ) {
20
- this .relatedCustomResourceUid = null ;
21
18
this .customResourcesSelector = customResourcesSelector ;
22
19
this .eventSource = eventSource ;
23
20
}
24
21
25
22
@ Override
26
23
public String getRelatedCustomResourceUid () {
27
- return relatedCustomResourceUid ;
24
+ if (customResourcesSelector instanceof UIDMatchingPredicate ) {
25
+ UIDMatchingPredicate resourcesSelector = (UIDMatchingPredicate ) customResourcesSelector ;
26
+ return resourcesSelector .uid ;
27
+ } else {
28
+ return null ;
29
+ }
28
30
}
29
31
30
32
public Predicate <CustomResource > getCustomResourcesSelector () {
@@ -40,12 +42,28 @@ public EventSource getEventSource() {
40
42
public String toString () {
41
43
return "{ class="
42
44
+ this .getClass ().getName ()
43
- + ", relatedCustomResourceUid="
44
- + relatedCustomResourceUid
45
45
+ ", customResourcesSelector="
46
46
+ customResourcesSelector
47
47
+ ", eventSource="
48
48
+ eventSource
49
49
+ " }" ;
50
50
}
51
+
52
+ private static class UIDMatchingPredicate implements Predicate <CustomResource > {
53
+ private final String uid ;
54
+
55
+ public UIDMatchingPredicate (String uid ) {
56
+ this .uid = uid ;
57
+ }
58
+
59
+ @ Override
60
+ public boolean test (CustomResource customResource ) {
61
+ return Objects .equals (uid , customResource .getMetadata ().getUid ());
62
+ }
63
+
64
+ @ Override
65
+ public String toString () {
66
+ return "UIDMatchingPredicate{uid='" + uid + "'}" ;
67
+ }
68
+ }
51
69
}
0 commit comments