8000 Merge pull request #1466 from wind57/minor-changes-in-event-type · tony-clarke-amdocs/java@2e8d23b · GitHub
[go: up one dir, main page]

Skip to content

Commit 2e8d23b

Browse files
authored
Merge pull request kubernetes-client#1466 from wind57/minor-changes-in-event-type
minor improvement
2 parents 5c35615 + 7fe4217 commit 2e8d23b

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

util/src/main/java/io/kubernetes/client/informer/EventType.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
package io.kubernetes.client.informer;
1414

15+
import java.util.Arrays;
16+
import java.util.Map;
17+
import java.util.Optional;
18+
import java.util.function.Function;
19+
import java.util.stream.Collectors;
20+
1521
public enum EventType {
1622
ADDED,
1723

@@ -23,12 +29,17 @@ public enum EventType {
2329

2430
ERROR;
2531

32+
private static final Map<String, EventType> TYPES =
33+
Arrays.stream(EventType.values()).collect(Collectors.toMap(Enum::name, Function.identity()));
34+
2635
/**
27-
* getByType returns the corresponding EventType by type.
36+
* returns the corresponding EventType by type.
2837
*
2938
* @param type specific code
3039
* @return corresponding EventType
40+
* @deprecated will be removed in a future release. use : findByType
3141
*/
42+
@Deprecated
3243
public static EventType getByType(String type) {
3344
if (type != null && type.length() > 0) {
3445
for (EventType eventType : EventType.values()) {
@@ -39,4 +50,14 @@ public static EventType getByType(String type) {
3950
}
4051
return null;
4152
}
53+
54+
/**
55+
* returns the corresponding EventType by type, wrapped in an {@link Optional}
56+
*
57+
* @param type specific code
58+
* @return an Optional describing the EventType
59+
*/
60+
public static Optional<EventType> findByType(String type) {
61+
return Optional.ofNullable(TYPES.get(String.valueOf(type).toUpperCase()));
62+
}
4263
}

util/src/main/java/io/kubernetes/client/informer/cache/ReflectorRunnable.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.net.ConnectException;
2626
import java.time.Duration;
2727
import java.util.List;
28+
import java.util.Optional;
2829
import java.util.concurrent.atomic.AtomicBoolean;
2930
import java.util.function.BiConsumer;
3031
import org.slf4j.Logger;
@@ -186,12 +187,12 @@ private void watchHandler(Watchable<ApiType> watch) {
186187
while (watch.hasNext()) {
187188
io.kubernetes.client.util.Watch.Response<ApiType> item = watch.next();
188189

189-
EventType eventType = EventType.getByType(item.type);
190-
if (eventType == null) {
190+
Optional<EventType> eventType = EventType.findByType(item.type);
191+
if (!eventType.isPresent()) {
191192
log.error("unrecognized event {}", item);
192193
continue;
193194
}
194-
if (eventType == EventType.ERROR) {
195+
if (eventType.get() == EventType.ERROR) {
195196
String errorMessage =
196197
String.format("got ERROR event and its status: %s", item.status.toString());
197198
log.error(errorMessage);
@@ -203,7 +204,7 @@ private void watchHandler(Watchable<ApiType> watch) {
203204
V1ObjectMeta meta = obj.getMetadata();
204205

205206
String newResourceVersion = meta.getResourceVersion();
206-
switch (eventType) {
207+
switch (eventType.get()) {
207208
case ADDED:
208209
store.add(obj);
209210
break;

0 commit comments

Comments
 (0)
0