From bb397c6a070701089ec52d5d019836a7be9c60eb Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Mon, 25 Jun 2018 23:49:47 -0400 Subject: [PATCH] General polishing in InMemoryFunctionCatalog --- .../catalog/InMemoryFunctionCatalog.java | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java index 7ab4f7c04..e370aa19a 100644 --- a/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java +++ b/spring-cloud-function-context/src/main/java/org/springframework/cloud/function/context/catalog/InMemoryFunctionCatalog.java @@ -19,6 +19,7 @@ package org.springframework.cloud.function.context.catalog; import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -110,62 +111,37 @@ public class InMemoryFunctionCatalog @PostConstruct public void init() { - if (publisher != null) { - if (!functions.isEmpty()) { - for (Class type : functions.keySet()) { - publisher.publishEvent(new FunctionRegistrationEvent(this, type, - functions.get(type).keySet())); - } - } + if (publisher != null && !functions.isEmpty()) { + functions.keySet() + .forEach(type -> publisher.publishEvent(new FunctionRegistrationEvent(this, type, functions.get(type).keySet()))); } } @PreDestroy public void close() { - if (publisher != null) { - if (!functions.isEmpty()) { - for (Class type : functions.keySet()) { - publisher.publishEvent(new FunctionUnregistrationEvent(this, type, - functions.get(type).keySet())); - } - } + if (publisher != null && !functions.isEmpty()) { + functions.keySet() + .forEach(type -> publisher.publishEvent(new FunctionUnregistrationEvent(this, type, functions.get(type).keySet()))); } } @Override + @SuppressWarnings("unchecked") public T lookup(Class type, String name) { - Map map = null; - for (Class key : functions.keySet()) { - if (key != Object.class) { - if (key.isAssignableFrom(type)) { - map = functions.get(key); - break; - } - } - } - if (map == null) { - map = functions.get(Object.class); - } - @SuppressWarnings("unchecked") - T result = (T) map.get(name); - return result; + Map map = this.extractTypeMap(type); + return (T) map.get(name); } @Override public Set getNames(Class type) { - Map map = null; - for (Class key : functions.keySet()) { - if (key != Object.class) { - if (key.isAssignableFrom(type)) { - map = functions.get(key); - break; - } - } - } - if (map == null) { - map = functions.get(Object.class); - } + Map map = this.extractTypeMap(type); return map == null ? Collections.emptySet() : map.keySet(); } + private Map extractTypeMap(Class type) { + return functions.keySet().stream() + .filter(key -> key != Object.class && key.isAssignableFrom(type)) + .map(key -> functions.get(key)) + .findFirst().orElse(functions.get(Object.class)); + } }