FunctionUtils polishing
- attempted to make code more functional (eat our own dog food) and concise - addressed PR comment - additional cleanup/polishing of FunctionUtils and related classes - Removed Function/Supplier/ConsumerProxy classes by extending type info on their super interface - Renamed FunctionUtils to FunctionFactoryUtils - Added javadoc to FunctionFactoryUtils to explain its design considerations as well as what it can and can not doi Fixes gh-90
This commit is contained in:
committed by
Dave Syer
parent
2644ab3178
commit
a973b678f1
@@ -31,13 +31,13 @@ import org.springframework.util.ReflectionUtils;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, FunctionFactoryMetadata {
|
||||
public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBean, FunctionFactoryMetadata<T> {
|
||||
|
||||
private final Resource resource;
|
||||
|
||||
private final Class<?> type;
|
||||
|
||||
private CompilationResultFactory<T> factory;
|
||||
private CompilationResultFactory<T> factory;
|
||||
|
||||
private final SimpleClassLoader classLoader = new SimpleClassLoader(AbstractByteCodeLoadingProxy.class.getClassLoader());
|
||||
|
||||
@@ -56,7 +56,7 @@ public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBea
|
||||
String functionName = filename == null ? type.getSimpleName() : filename.replaceAll(".fun$", "");
|
||||
String firstLetter = functionName.substring(0, 1).toUpperCase();
|
||||
String upperCasedName = (functionName.length() > 1) ? firstLetter + functionName.substring(1) : firstLetter;
|
||||
String className = String.format("%s.%s%sFactory", FunctionCompiler.class.getPackage().getName(), upperCasedName, this.type.getSimpleName());
|
||||
String className = String.format("%s.%s%sFactory", FunctionCompiler.class.getPackage().getName(), upperCasedName, this.type.getSimpleName());
|
||||
Class<?> factoryClass = this.classLoader.defineClass(className, bytes);
|
||||
try {
|
||||
this.factory = (CompilationResultFactory<T>) factoryClass.newInstance();
|
||||
@@ -78,6 +78,7 @@ public abstract class AbstractByteCodeLoadingProxy<T> implements InitializingBea
|
||||
return method.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T getTarget() {
|
||||
return this.factory.getResult();
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.util.FileCopyUtils;
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNameAware, FunctionFactoryMetadata {
|
||||
public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNameAware, FunctionFactoryMetadata<T> {
|
||||
|
||||
private final Resource resource;
|
||||
|
||||
@@ -65,6 +65,7 @@ public class AbstractLambdaCompilingProxy<T> implements InitializingBean, BeanNa
|
||||
this.factory = this.compiler.compile(this.beanName, lambda, this.typeParameterizations);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final T getTarget() {
|
||||
return this.factory.getResult();
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.cloud.function.compiler.proxy;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.cloud.function.support.ConsumerProxy;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@@ -26,7 +26,7 @@ import org.springframework.core.io.Resource;
|
||||
*
|
||||
* @param <T> type
|
||||
*/
|
||||
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements ConsumerProxy<T> {
|
||||
public class ByteCodeLoadingConsumer<T> extends AbstractByteCodeLoadingProxy<Consumer<T>> implements FunctionFactoryMetadata<Consumer<T>>, Consumer<T> {
|
||||
|
||||
public ByteCodeLoadingConsumer(Resource resource) {
|
||||
super(resource, Consumer.class);
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.cloud.function.compiler.proxy;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.cloud.function.support.FunctionProxy;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@@ -27,7 +27,7 @@ import org.springframework.core.io.Resource;
|
||||
* @param <T> Function input type
|
||||
* @param <R> Function result type
|
||||
*/
|
||||
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionProxy<T, R> {
|
||||
public class ByteCodeLoadingFunction<T, R> extends AbstractByteCodeLoadingProxy<Function<T, R>> implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
|
||||
|
||||
public ByteCodeLoadingFunction(Resource resource) {
|
||||
super(resource, Function.class);
|
||||
|
||||
@@ -18,7 +18,7 @@ package org.springframework.cloud.function.compiler.proxy;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.cloud.function.support.SupplierProxy;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
@@ -26,7 +26,7 @@ import org.springframework.core.io.Resource;
|
||||
*
|
||||
* @param <T> type
|
||||
*/
|
||||
public class ByteCodeLoadingSupplier<T> extends AbstractByteCodeLoadingProxy<Supplier<T>> implements SupplierProxy<T> {
|
||||
public class ByteCodeLoadingSupplier<T> extends AbstractByteCodeLoadingProxy<Supplier<T>> implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
|
||||
|
||||
public ByteCodeLoadingSupplier(Resource resource) {
|
||||
super(resource, Supplier.class);
|
||||
|
||||
@@ -19,13 +19,13 @@ package org.springframework.cloud.function.compiler.proxy;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||
import org.springframework.cloud.function.support.FunctionProxy;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class LambdaCompilingFunction<T, R> extends AbstractLambdaCompilingProxy<Function<T, R>> implements FunctionProxy<T, R> {
|
||||
public class LambdaCompilingFunction<T, R> extends AbstractLambdaCompilingProxy<Function<T, R>> implements FunctionFactoryMetadata<Function<T, R>>, Function<T, R> {
|
||||
|
||||
public LambdaCompilingFunction(Resource resource, FunctionCompiler<T, R> compiler) {
|
||||
super(resource, compiler);
|
||||
|
||||
@@ -19,13 +19,13 @@ package org.springframework.cloud.function.compiler.proxy;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.cloud.function.compiler.SupplierCompiler;
|
||||
import org.springframework.cloud.function.support.SupplierProxy;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.core.io.Resource;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class LambdaCompilingSupplier<T> extends AbstractLambdaCompilingProxy<Supplier<T>> implements SupplierProxy<T> {
|
||||
public class LambdaCompilingSupplier<T> extends AbstractLambdaCompilingProxy<Supplier<T>> implements FunctionFactoryMetadata<Supplier<T>>, Supplier<T> {
|
||||
|
||||
public LambdaCompilingSupplier(Resource resource, SupplierCompiler<T> compiler) {
|
||||
super(resource, compiler);
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.function.Consumer;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.function.support.FunctionUtils;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -35,14 +35,14 @@ public class ConsumerCompilerTests {
|
||||
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
|
||||
String.class.getName()).compile("foos",
|
||||
"flux -> flux.subscribe(System.out::println)", "Flux<String>");
|
||||
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isTrue();
|
||||
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void consumesString() {
|
||||
CompiledFunctionFactory<Consumer<String>> compiled = new ConsumerCompiler<String>(
|
||||
String.class.getName()).compile("foos", "System.out::println", "String");
|
||||
assertThat(FunctionUtils.isFluxConsumer(compiled.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxConsumer(compiled.getFactoryMethod())).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.function.Function;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.function.support.FunctionUtils;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -35,14 +35,14 @@ public class FunctionCompilerTests {
|
||||
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
|
||||
String.class.getName()).compile("foos",
|
||||
"flux -> flux.map(v -> v.toUpperCase())", "Flux<String>", "Flux<String>");
|
||||
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
|
||||
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void transformsString() {
|
||||
CompiledFunctionFactory<Function<String, String>> compiled = new FunctionCompiler<String, String>(
|
||||
String.class.getName()).compile("foos", "v -> v.toUpperCase()", "String", "String");
|
||||
assertThat(FunctionUtils.isFluxFunction(compiled.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxFunction(compiled.getFactoryMethod())).isFalse();
|
||||
assertThat(compiled.getResult().apply("hello")).isEqualTo("HELLO");
|
||||
}
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ import java.util.function.Supplier;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.cloud.function.support.FunctionUtils;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -35,7 +35,7 @@ public class SupplierCompilerTests {
|
||||
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
|
||||
String.class.getName()).compile("foos",
|
||||
"() -> Flux.just(\"foo\", \"bar\")", "Flux<String>");
|
||||
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
|
||||
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod())).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -43,7 +43,7 @@ public class SupplierCompilerTests {
|
||||
CompiledFunctionFactory<Supplier<String>> compiled = new SupplierCompiler<String>(
|
||||
String.class.getName()).compile("foos",
|
||||
"() -> \"foo\"", "String");
|
||||
assertThat(FunctionUtils.isFluxSupplier(compiled.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxSupplier(compiled.getFactoryMethod())).isFalse();
|
||||
assertThat(compiled.getResult().get()).isEqualTo("foo");
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.cloud.function.compiler.ConsumerCompiler;
|
||||
import org.springframework.cloud.function.compiler.FunctionCompiler;
|
||||
import org.springframework.cloud.function.compiler.SupplierCompiler;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryMetadata;
|
||||
import org.springframework.cloud.function.support.FunctionUtils;
|
||||
import org.springframework.cloud.function.support.FunctionFactoryUtils;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -53,7 +53,7 @@ public class ByteCodeLoadingFunctionTests {
|
||||
ByteCodeLoadingConsumer<String> consumer = new ByteCodeLoadingConsumer<>(resource);
|
||||
consumer.afterPropertiesSet();
|
||||
assertThat(consumer instanceof FunctionFactoryMetadata);
|
||||
assertThat(FunctionUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxConsumer(consumer.getFactoryMethod())).isFalse();
|
||||
consumer.accept("foo");
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class ByteCodeLoadingFunctionTests {
|
||||
ByteCodeLoadingSupplier<String> supplier = new ByteCodeLoadingSupplier<>(resource);
|
||||
supplier.afterPropertiesSet();
|
||||
assertThat(supplier instanceof FunctionFactoryMetadata);
|
||||
assertThat(FunctionUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxSupplier(supplier.getFactoryMethod())).isFalse();
|
||||
assertThat(supplier.get()).isEqualTo("foo");
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ public class ByteCodeLoadingFunctionTests {
|
||||
ByteCodeLoadingFunction<String, String> function = new ByteCodeLoadingFunction<>(resource);
|
||||
function.afterPropertiesSet();
|
||||
assertThat(function instanceof FunctionFactoryMetadata);
|
||||
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
|
||||
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod())).isFalse();
|
||||
assertThat(function.apply("foo")).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ public class ByteCodeLoadingFunctionTests {
|
||||
ByteCodeLoadingFunction<Flux<String>, Flux<String>> function = new ByteCodeLoadingFunction<>(resource);
|
||||
function.afterPropertiesSet();
|
||||
assertThat(function instanceof FunctionFactoryMetadata);
|
||||
assertThat(FunctionUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
|
||||
assertThat(FunctionFactoryUtils.isFluxFunction(function.getFactoryMethod())).isTrue();
|
||||
assertThat(function.apply(Flux.just("foo")).blockFirst()).isEqualTo("FOO");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user