11package io .javaoperatorsdk .operator .processing .event ;
22
33import io .fabric8 .kubernetes .client .CustomResource ;
4+ import java .util .Objects ;
45import java .util .function .Predicate ;
56
67@ SuppressWarnings ("rawtypes" )
78public class DefaultEvent implements Event {
8-
9- private final String relatedCustomResourceUid ;
109 private final Predicate <CustomResource > customResourcesSelector ;
1110 private final EventSource eventSource ;
1211
1312 public DefaultEvent (String relatedCustomResourceUid , EventSource eventSource ) {
14- this .relatedCustomResourceUid = relatedCustomResourceUid ;
15- this .customResourcesSelector = null ;
13+ this .customResourcesSelector = new UIDMatchingPredicate (relatedCustomResourceUid );
1614 this .eventSource = eventSource ;
1715 }
1816
1917 public DefaultEvent (Predicate <CustomResource > customResourcesSelector , EventSource eventSource ) {
20- this .relatedCustomResourceUid = null ;
2118 this .customResourcesSelector = customResourcesSelector ;
2219 this .eventSource = eventSource ;
2320 }
2421
2522 @ Override
2623 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+ }
2830 }
2931
3032 public Predicate <CustomResource > getCustomResourcesSelector () {
@@ -40,12 +42,28 @@ public EventSource getEventSource() {
4042 public String toString () {
4143 return "{ class="
4244 + this .getClass ().getName ()
43- + ", relatedCustomResourceUid="
44- + relatedCustomResourceUid
4545 + ", customResourcesSelector="
4646 + customResourcesSelector
4747 + ", eventSource="
4848 + eventSource
4949 + " }" ;
5050 }
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+ }
5169}
0 commit comments