Re-org so that default methods are used everywhere

This commit is contained in:
Dave Syer
2018-02-28 10:28:14 +00:00
parent 33b33adb4b
commit 955e99bfe3
3 changed files with 55 additions and 48 deletions

View File

@@ -16,6 +16,9 @@
package org.springframework.cloud.function.context.catalog; package org.springframework.cloud.function.context.catalog;
import java.util.Collections;
import java.util.Set;
import org.springframework.cloud.function.context.FunctionRegistration; import org.springframework.cloud.function.context.FunctionRegistration;
/** /**
@@ -57,7 +60,9 @@ public interface FunctionInspector {
default String getName(Object function) { default String getName(Object function) {
FunctionRegistration<?> registration = getRegistration(function); FunctionRegistration<?> registration = getRegistration(function);
return registration == null ? null : registration.getNames().iterator().next(); Set<String> names = registration == null ? Collections.emptySet()
: registration.getNames();
return names.isEmpty() ? null : names.iterator().next();
} }
} }

View File

@@ -160,7 +160,8 @@ public class ContextFunctionCatalogAutoConfiguration {
@Override @Override
public FunctionRegistration<?> getRegistration(Object function) { public FunctionRegistration<?> getRegistration(Object function) {
return processor.getRegistration(function); FunctionRegistration<?> registration = processor.getRegistration(function);
return registration;
} }
} }

View File

@@ -100,9 +100,10 @@ public class FunctionExtractingFunctionCatalog
} }
private FunctionType findType(Object function) { private FunctionType findType(Object function) {
FunctionType type = FunctionType.from(getInputType(function)) FunctionType type = FunctionType.from((Class<?>) type(function, "getInputType"))
.to(getOutputType(function)).wrap(getInputWrapper(function)); .to((Class<?>) type(function, "getOutputType"))
if (isMessage(function)) { .wrap((Class<?>) type(function, "getInputWrapper"));
if ((Boolean) type(function, "isMessage")) {
type = type.message(); type = type.message();
} }
return type; return type;
@@ -120,35 +121,10 @@ public class FunctionExtractingFunctionCatalog
return (Set<String>) getNames("getNames", type); return (Set<String>) getNames("getNames", type);
} }
@Override
public boolean isMessage(Object function) {
return (Boolean) type(function, "isMessage");
}
@Override
public Class<?> getInputType(Object function) {
return (Class<?>) type(function, "getInputType");
}
@Override
public Class<?> getOutputType(Object function) {
return (Class<?>) type(function, "getOutputType");
}
@Override
public Class<?> getInputWrapper(Object function) {
return (Class<?>) type(function, "getInputWrapper");
}
@Override
public Class<?> getOutputWrapper(Object function) {
return (Class<?>) type(function, "getOutputWrapper");
}
@SuppressWarnings("unchecked")
@Override @Override
public String getName(Object function) { public String getName(Object function) {
return ((Set<String>) inspect(function, "getNames")).iterator().next(); Set<String> names = getNames(function);
return names.isEmpty() ? null : names.iterator().next();
} }
public String deploy(String name, String path, String... args) { public String deploy(String name, String path, String... args) {
@@ -216,21 +192,45 @@ public class FunctionExtractingFunctionCatalog
} }
} }
private Object inspect(Object arg, String method) { private Set<String> getNames(Object arg) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Inspecting " + method); logger.debug("Inspecting names");
} }
return invoke(FunctionInspector.class, "getRegistration", (id, result) -> { @SuppressWarnings("unchecked")
return prefix(id, invoke(result, method)); Set<String> result = (Set<String>) invoke(FunctionInspector.class,
}, arg); "getRegistration", this::extractNames, arg);
return result;
}
private Set<String> extractNames(String id, Object result) {
@SuppressWarnings("unchecked")
Set<String> prefixed = (Set<String>) prefix(id, invoke(result, "getNames"));
if (logger.isDebugEnabled()) {
logger.debug("Result (from " + this.ids.get(id) + "): " + prefixed);
}
if (prefixed.isEmpty()) {
return null;
}
return prefixed;
} }
private Object type(Object arg, String method) { private Object type(Object arg, String method) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Inspecting " + method); logger.debug("Inspecting type " + method);
} }
return invoke(invoke(invoke(FunctionInspector.class, "getRegistration", arg), Object result = invoke(invoke(invoke(FunctionInspector.class, "getRegistration",
"getType"), method); this::discardEmpty, arg), "getType"), method);
if (logger.isDebugEnabled()) {
logger.debug("Result: " + result);
}
return result;
}
private Object discardEmpty(String id, Object result) {
if (result == null || invoke(result, "getTarget") == null) {
return null;
}
return result;
} }
private Object prefix(String id, Object result) { private Object prefix(String id, Object result) {
@@ -252,9 +252,6 @@ public class FunctionExtractingFunctionCatalog
} }
else { else {
if (logger.isDebugEnabled()) {
logger.debug("Result (from " + name + "): " + result);
}
return result; return result;
} }
} }
@@ -279,7 +276,7 @@ public class FunctionExtractingFunctionCatalog
return invoke(type, method, null, arg); return invoke(type, method, null, arg);
} }
private Object invoke(Class<?> type, String method, Callback callback, private Object invoke(Class<?> type, String method, Callback<?> callback,
Object... arg) { Object... arg) {
Set<Object> results = new LinkedHashSet<>(); Set<Object> results = new LinkedHashSet<>();
Object fallback = null; Object fallback = null;
@@ -301,7 +298,11 @@ public class FunctionExtractingFunctionCatalog
continue; continue;
} }
if (callback != null) { if (callback != null) {
return callback.call(id, result); result = callback.call(id, result);
if (result != null) {
return result;
}
continue;
} }
return result; return result;
} }
@@ -338,7 +339,7 @@ public class FunctionExtractingFunctionCatalog
return prefix(id, result); return prefix(id, result);
} }
catch (Exception e) { catch (Exception e) {
throw new IllegalStateException("Cannot extract catalog", e); throw new IllegalStateException("Cannot extract", e);
} }
} }
@@ -365,8 +366,8 @@ public class FunctionExtractingFunctionCatalog
return result; return result;
} }
interface Callback { interface Callback<T> {
Object call(String id, Object result); T call(String id, Object result);
} }
} }