Merge pull request #13837 from nosan:polish-property-mapper

* pr/13837:
  Polish "Add PropertyMapper.from(value)"
  Add PropertyMapper.from(value)
pull/13837/merge
Stephane Nicoll 6 years ago
commit f9207dd946

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2017 the original author or authors. * Copyright 2012-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -122,7 +122,7 @@ public class BasicBatchConfigurer implements BatchConfigurer {
protected JobRepository createJobRepository() throws Exception { protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
PropertyMapper map = PropertyMapper.get(); PropertyMapper map = PropertyMapper.get();
map.from(() -> this.dataSource).to(factory::setDataSource); map.from(this.dataSource).to(factory::setDataSource);
map.from(this::determineIsolationLevel).whenNonNull() map.from(this::determineIsolationLevel).whenNonNull()
.to(factory::setIsolationLevelForCreate); .to(factory::setIsolationLevelForCreate);
map.from(this.properties::getTablePrefix).whenHasText() map.from(this.properties::getTablePrefix).whenHasText()

@ -85,9 +85,8 @@ public class ConcurrentKafkaListenerContainerFactoryConfigurer {
PropertyMapper map = PropertyMapper.get(); PropertyMapper map = PropertyMapper.get();
Listener properties = this.properties.getListener(); Listener properties = this.properties.getListener();
map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency); map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency);
map.from(() -> this.messageConverter).whenNonNull() map.from(this.messageConverter).whenNonNull().to(factory::setMessageConverter);
.to(factory::setMessageConverter); map.from(this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
map.from(() -> this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
map.from(properties::getType).whenEqualTo(Listener.Type.BATCH) map.from(properties::getType).whenEqualTo(Listener.Type.BATCH)
.toCall(() -> factory.setBatchListener(true)); .toCall(() -> factory.setBatchListener(true));
} }

@ -84,7 +84,7 @@ public class TomcatWebServerFactoryCustomizer implements
tomcatProperties.getMaxThreads())); tomcatProperties.getMaxThreads()));
propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive) propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads)); .to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive) propertyMapper.from(this::determineMaxHttpHeaderSize).when(this::isPositive)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
maxHttpHeaderSize)); maxHttpHeaderSize));
propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull() propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()

@ -81,7 +81,7 @@ public class UndertowWebServerFactoryCustomizer implements
.to(factory::setAccessLogSuffix); .to(factory::setAccessLogSuffix);
propertyMapper.from(accesslogProperties::isRotate) propertyMapper.from(accesslogProperties::isRotate)
.to(factory::setAccessLogRotate); .to(factory::setAccessLogRotate);
propertyMapper.from(() -> getOrDeduceUseForwardHeaders()) propertyMapper.from(this::getOrDeduceUseForwardHeaders)
.to(factory::setUseForwardHeaders); .to(factory::setUseForwardHeaders);
propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive) propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive)
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory, .to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,

@ -138,10 +138,10 @@ public class MultipartProperties {
public MultipartConfigElement createMultipartConfig() { public MultipartConfigElement createMultipartConfig() {
MultipartConfigFactory factory = new MultipartConfigFactory(); MultipartConfigFactory factory = new MultipartConfigFactory();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(() -> this.fileSizeThreshold).to(factory::setFileSizeThreshold); map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold);
map.from(() -> this.location).whenHasText().to(factory::setLocation); map.from(this.location).whenHasText().to(factory::setLocation);
map.from(() -> this.maxRequestSize).to(factory::setMaxRequestSize); map.from(this.maxRequestSize).to(factory::setMaxRequestSize);
map.from(() -> this.maxFileSize).to(factory::setMaxFileSize); map.from(this.maxFileSize).to(factory::setMaxFileSize);
return factory.createMultipartConfig(); return factory.createMultipartConfig();
} }

@ -97,6 +97,7 @@ public final class PropertyMapper {
* @param <T> the source type * @param <T> the source type
* @param supplier the value supplier * @param supplier the value supplier
* @return a {@link Source} that can be used to complete the mapping * @return a {@link Source} that can be used to complete the mapping
* @see #from(Object)
*/ */
public <T> Source<T> from(Supplier<T> supplier) { public <T> Source<T> from(Supplier<T> supplier) {
Assert.notNull(supplier, "Supplier must not be null"); Assert.notNull(supplier, "Supplier must not be null");
@ -107,6 +108,17 @@ public final class PropertyMapper {
return source; return source;
} }
/**
* Return a new {@link Source} from the specified value that can be used to perform
* the mapping.
* @param <T> the source type
* @param value the value
* @return a {@link Source} that can be used to complete the mapping
*/
public <T> Source<T> from(T value) {
return from(() -> value);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> Source<T> getSource(Supplier<T> supplier) { private <T> Source<T> getSource(Supplier<T> supplier) {
if (this.parent != null) { if (this.parent != null) {

@ -279,16 +279,15 @@ public class TaskExecutorBuilder {
*/ */
public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) { public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(() -> this.queueCapacity).to(taskExecutor::setQueueCapacity); map.from(this.queueCapacity).to(taskExecutor::setQueueCapacity);
map.from(() -> this.corePoolSize).to(taskExecutor::setCorePoolSize); map.from(this.corePoolSize).to(taskExecutor::setCorePoolSize);
map.from(() -> this.maxPoolSize).to(taskExecutor::setMaxPoolSize); map.from(this.maxPoolSize).to(taskExecutor::setMaxPoolSize);
map.from(() -> this.keepAlive).asInt(Duration::getSeconds) map.from(this.keepAlive).asInt(Duration::getSeconds)
.to(taskExecutor::setKeepAliveSeconds); .to(taskExecutor::setKeepAliveSeconds);
map.from(() -> this.allowCoreThreadTimeOut) map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut);
.to(taskExecutor::setAllowCoreThreadTimeOut); map.from(this.threadNamePrefix).whenHasText()
map.from(() -> this.threadNamePrefix).whenHasText()
.to(taskExecutor::setThreadNamePrefix); .to(taskExecutor::setThreadNamePrefix);
map.from(() -> this.taskDecorator).to(taskExecutor::setTaskDecorator); map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) { if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) {
for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) { for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) {

@ -167,8 +167,8 @@ public class TaskSchedulerBuilder {
*/ */
public <T extends ThreadPoolTaskScheduler> T configure(T taskScheduler) { public <T extends ThreadPoolTaskScheduler> T configure(T taskScheduler) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(() -> this.poolSize).to(taskScheduler::setPoolSize); map.from(this.poolSize).to(taskScheduler::setPoolSize);
map.from(() -> this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix); map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) { if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) {
for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) { for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) {

@ -502,13 +502,12 @@ public class WebServiceTemplateBuilder {
configureMessageSenders(webServiceTemplate); configureMessageSenders(webServiceTemplate);
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
applyCustomizers(webServiceTemplate, this.internalCustomizers); applyCustomizers(webServiceTemplate, this.internalCustomizers);
map.from(() -> this.marshaller).to(webServiceTemplate::setMarshaller); map.from(this.marshaller).to(webServiceTemplate::setMarshaller);
map.from(() -> this.unmarshaller).to(webServiceTemplate::setUnmarshaller); map.from(this.unmarshaller).to(webServiceTemplate::setUnmarshaller);
map.from(() -> this.destinationProvider) map.from(this.destinationProvider).to(webServiceTemplate::setDestinationProvider);
.to(webServiceTemplate::setDestinationProvider); map.from(this.transformerFactoryClass)
map.from(() -> this.transformerFactoryClass)
.to(webServiceTemplate::setTransformerFactoryClass); .to(webServiceTemplate::setTransformerFactoryClass);
map.from(() -> this.messageFactory).to(webServiceTemplate::setMessageFactory); map.from(this.messageFactory).to(webServiceTemplate::setMessageFactory);
if (!CollectionUtils.isEmpty(this.interceptors)) { if (!CollectionUtils.isEmpty(this.interceptors)) {
Set<ClientInterceptor> merged = new LinkedHashSet<>(this.interceptors); Set<ClientInterceptor> merged = new LinkedHashSet<>(this.interceptors);
if (webServiceTemplate.getInterceptors() != null) { if (webServiceTemplate.getInterceptors() != null) {

@ -37,11 +37,37 @@ public class PropertyMapperTests {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Test
public void fromNullValue() {
ExampleDest dest = new ExampleDest();
this.map.from((String) null).to(dest::setName);
assertThat(dest.getName()).isNull();
}
@Test
public void fromValue() {
ExampleDest dest = new ExampleDest();
this.map.from("Hello World").to(dest::setName);
assertThat(dest.getName()).isEqualTo("Hello World");
}
@Test
public void fromValueAsIntShouldAdaptSupplier() {
Integer result = this.map.from("123").asInt(Long::valueOf)
.toInstance(Integer::new);
assertThat(result).isEqualTo(123);
}
@Test
public void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assert::fail);
}
@Test @Test
public void fromWhenSupplierIsNullShouldThrowException() { public void fromWhenSupplierIsNullShouldThrowException() {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
this.thrown.expectMessage("Supplier must not be null"); this.thrown.expectMessage("Supplier must not be null");
this.map.from(null); this.map.from((Supplier<?>) null);
} }
@Test @Test
@ -94,24 +120,24 @@ public class PropertyMapperTests {
@Test @Test
public void whenTrueWhenValueIsTrueShouldMap() { public void whenTrueWhenValueIsTrueShouldMap() {
Boolean result = this.map.from(() -> true).whenTrue().toInstance(Boolean::new); Boolean result = this.map.from(true).whenTrue().toInstance(Boolean::new);
assertThat(result).isTrue(); assertThat(result).isTrue();
} }
@Test @Test
public void whenTrueWhenValueIsFalseShouldNotMap() { public void whenTrueWhenValueIsFalseShouldNotMap() {
this.map.from(() -> false).whenTrue().toCall(Assert::fail); this.map.from(false).whenTrue().toCall(Assert::fail);
} }
@Test @Test
public void whenFalseWhenValueIsFalseShouldMap() { public void whenFalseWhenValueIsFalseShouldMap() {
Boolean result = this.map.from(() -> false).whenFalse().toInstance(Boolean::new); Boolean result = this.map.from(false).whenFalse().toInstance(Boolean::new);
assertThat(result).isFalse(); assertThat(result).isFalse();
} }
@Test @Test
public void whenFalseWhenValueIsTrueShouldNotMap() { public void whenFalseWhenValueIsTrueShouldNotMap() {
this.map.from(() -> true).whenFalse().toCall(Assert::fail); this.map.from(true).whenFalse().toCall(Assert::fail);
} }
@Test @Test
@ -121,30 +147,29 @@ public class PropertyMapperTests {
@Test @Test
public void whenHasTextWhenValueIsEmptyShouldNotMap() { public void whenHasTextWhenValueIsEmptyShouldNotMap() {
this.map.from(() -> "").whenHasText().toCall(Assert::fail); this.map.from("").whenHasText().toCall(Assert::fail);
} }
@Test @Test
public void whenHasTextWhenValueHasTextShouldMap() { public void whenHasTextWhenValueHasTextShouldMap() {
Integer result = this.map.from(() -> 123).whenHasText().toInstance(Integer::new); Integer result = this.map.from(123).whenHasText().toInstance(Integer::new);
assertThat(result).isEqualTo(123); assertThat(result).isEqualTo(123);
} }
@Test @Test
public void whenEqualToWhenValueIsEqualShouldMatch() { public void whenEqualToWhenValueIsEqualShouldMatch() {
String result = this.map.from(() -> "123").whenEqualTo("123") String result = this.map.from("123").whenEqualTo("123").toInstance(String::new);
.toInstance(String::new);
assertThat(result).isEqualTo("123"); assertThat(result).isEqualTo("123");
} }
@Test @Test
public void whenEqualToWhenValueIsNotEqualShouldNotMatch() { public void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
this.map.from(() -> "123").whenEqualTo("321").toCall(Assert::fail); this.map.from("123").whenEqualTo("321").toCall(Assert::fail);
} }
@Test @Test
public void whenInstanceOfWhenValueIsTargetTypeShouldMatch() { public void whenInstanceOfWhenValueIsTargetTypeShouldMatch() {
Long result = this.map.from(() -> 123L).whenInstanceOf(Long.class) Long result = this.map.from(123L).whenInstanceOf(Long.class)
.toInstance((value) -> value + 1); .toInstance((value) -> value + 1);
assertThat(result).isEqualTo(124L); assertThat(result).isEqualTo(124L);
} }
@ -157,14 +182,13 @@ public class PropertyMapperTests {
@Test @Test
public void whenWhenValueMatchesShouldMap() { public void whenWhenValueMatchesShouldMap() {
String result = this.map.from(() -> "123").when("123"::equals) String result = this.map.from("123").when("123"::equals).toInstance(String::new);
.toInstance(String::new);
assertThat(result).isEqualTo("123"); assertThat(result).isEqualTo("123");
} }
@Test @Test
public void whenWhenValueDoesNotMatchShouldNotMap() { public void whenWhenValueDoesNotMatchShouldNotMap() {
this.map.from(() -> "123").when("321"::equals).toCall(Assert::fail); this.map.from("123").when("321"::equals).toCall(Assert::fail);
} }
@Test @Test

Loading…
Cancel
Save