General polishing in InMemoryFunctionCatalog
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.cloud.function.context.catalog;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
@@ -110,62 +111,37 @@ public class InMemoryFunctionCatalog
|
|||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
if (publisher != null) {
|
if (publisher != null && !functions.isEmpty()) {
|
||||||
if (!functions.isEmpty()) {
|
functions.keySet()
|
||||||
for (Class<?> type : functions.keySet()) {
|
.forEach(type -> publisher.publishEvent(new FunctionRegistrationEvent(this, type, functions.get(type).keySet())));
|
||||||
publisher.publishEvent(new FunctionRegistrationEvent(this, type,
|
|
||||||
functions.get(type).keySet()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
@PreDestroy
|
||||||
public void close() {
|
public void close() {
|
||||||
if (publisher != null) {
|
if (publisher != null && !functions.isEmpty()) {
|
||||||
if (!functions.isEmpty()) {
|
functions.keySet()
|
||||||
for (Class<?> type : functions.keySet()) {
|
.forEach(type -> publisher.publishEvent(new FunctionUnregistrationEvent(this, type, functions.get(type).keySet())));
|
||||||
publisher.publishEvent(new FunctionUnregistrationEvent(this, type,
|
|
||||||
functions.get(type).keySet()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public <T> T lookup(Class<?> type, String name) {
|
public <T> T lookup(Class<?> type, String name) {
|
||||||
Map<String, Object> map = null;
|
Map<String, Object> map = this.extractTypeMap(type);
|
||||||
for (Class<?> key : functions.keySet()) {
|
return (T) map.get(name);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getNames(Class<?> type) {
|
public Set<String> getNames(Class<?> type) {
|
||||||
Map<String, Object> map = null;
|
Map<String, Object> map = this.extractTypeMap(type);
|
||||||
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);
|
|
||||||
}
|
|
||||||
return map == null ? Collections.emptySet() : map.keySet();
|
return map == null ? Collections.emptySet() : map.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Map<String, Object> 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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user