Skip to content

Commit f9207dd

Browse files
committed
Merge pull request #13837 from nosan:polish-property-mapper
* pr/13837: Polish "Add PropertyMapper.from(value)" Add PropertyMapper.from(value)
2 parents dd9209c + 597fe23 commit f9207dd

File tree

10 files changed

+74
-41
lines changed

10 files changed

+74
-41
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BasicBatchConfigurer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -122,7 +122,7 @@ protected JobLauncher createJobLauncher() throws Exception {
122122
protected JobRepository createJobRepository() throws Exception {
123123
JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
124124
PropertyMapper map = PropertyMapper.get();
125-
map.from(() -> this.dataSource).to(factory::setDataSource);
125+
map.from(this.dataSource).to(factory::setDataSource);
126126
map.from(this::determineIsolationLevel).whenNonNull()
127127
.to(factory::setIsolationLevelForCreate);
128128
map.from(this.properties::getTablePrefix).whenHasText()

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/kafka/ConcurrentKafkaListenerContainerFactoryConfigurer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ private void configureListenerFactory(
8585
PropertyMapper map = PropertyMapper.get();
8686
Listener properties = this.properties.getListener();
8787
map.from(properties::getConcurrency).whenNonNull().to(factory::setConcurrency);
88-
map.from(() -> this.messageConverter).whenNonNull()
89-
.to(factory::setMessageConverter);
90-
map.from(() -> this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
88+
map.from(this.messageConverter).whenNonNull().to(factory::setMessageConverter);
89+
map.from(this.replyTemplate).whenNonNull().to(factory::setReplyTemplate);
9190
map.from(properties::getType).whenEqualTo(Listener.Type.BATCH)
9291
.toCall(() -> factory.setBatchListener(true));
9392
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void customize(ConfigurableTomcatWebServerFactory factory) {
8484
tomcatProperties.getMaxThreads()));
8585
propertyMapper.from(tomcatProperties::getMinSpareThreads).when(this::isPositive)
8686
.to((minSpareThreads) -> customizeMinThreads(factory, minSpareThreads));
87-
propertyMapper.from(() -> determineMaxHttpHeaderSize()).when(this::isPositive)
87+
propertyMapper.from(this::determineMaxHttpHeaderSize).when(this::isPositive)
8888
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,
8989
maxHttpHeaderSize));
9090
propertyMapper.from(tomcatProperties::getMaxSwallowSize).whenNonNull()

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/UndertowWebServerFactoryCustomizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public void customize(ConfigurableUndertowWebServerFactory factory) {
8181
.to(factory::setAccessLogSuffix);
8282
propertyMapper.from(accesslogProperties::isRotate)
8383
.to(factory::setAccessLogRotate);
84-
propertyMapper.from(() -> getOrDeduceUseForwardHeaders())
84+
propertyMapper.from(this::getOrDeduceUseForwardHeaders)
8585
.to(factory::setUseForwardHeaders);
8686
propertyMapper.from(properties::getMaxHttpHeaderSize).when(this::isPositive)
8787
.to((maxHttpHeaderSize) -> customizeMaxHttpHeaderSize(factory,

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/servlet/MultipartProperties.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ public void setResolveLazily(boolean resolveLazily) {
138138
public MultipartConfigElement createMultipartConfig() {
139139
MultipartConfigFactory factory = new MultipartConfigFactory();
140140
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
141-
map.from(() -> this.fileSizeThreshold).to(factory::setFileSizeThreshold);
142-
map.from(() -> this.location).whenHasText().to(factory::setLocation);
143-
map.from(() -> this.maxRequestSize).to(factory::setMaxRequestSize);
144-
map.from(() -> this.maxFileSize).to(factory::setMaxFileSize);
141+
map.from(this.fileSizeThreshold).to(factory::setFileSizeThreshold);
142+
map.from(this.location).whenHasText().to(factory::setLocation);
143+
map.from(this.maxRequestSize).to(factory::setMaxRequestSize);
144+
map.from(this.maxFileSize).to(factory::setMaxFileSize);
145145
return factory.createMultipartConfig();
146146
}
147147

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/PropertyMapper.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public PropertyMapper alwaysApplying(SourceOperator operator) {
9797
* @param <T> the source type
9898
* @param supplier the value supplier
9999
* @return a {@link Source} that can be used to complete the mapping
100+
* @see #from(Object)
100101
*/
101102
public <T> Source<T> from(Supplier<T> supplier) {
102103
Assert.notNull(supplier, "Supplier must not be null");
@@ -107,6 +108,17 @@ public <T> Source<T> from(Supplier<T> supplier) {
107108
return source;
108109
}
109110

111+
/**
112+
* Return a new {@link Source} from the specified value that can be used to perform
113+
* the mapping.
114+
* @param <T> the source type
115+
* @param value the value
116+
* @return a {@link Source} that can be used to complete the mapping
117+
*/
118+
public <T> Source<T> from(T value) {
119+
return from(() -> value);
120+
}
121+
110122
@SuppressWarnings("unchecked")
111123
private <T> Source<T> getSource(Supplier<T> supplier) {
112124
if (this.parent != null) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskExecutorBuilder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,16 +279,15 @@ public <T extends ThreadPoolTaskExecutor> T build(Class<T> taskExecutorClass) {
279279
*/
280280
public <T extends ThreadPoolTaskExecutor> T configure(T taskExecutor) {
281281
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
282-
map.from(() -> this.queueCapacity).to(taskExecutor::setQueueCapacity);
283-
map.from(() -> this.corePoolSize).to(taskExecutor::setCorePoolSize);
284-
map.from(() -> this.maxPoolSize).to(taskExecutor::setMaxPoolSize);
285-
map.from(() -> this.keepAlive).asInt(Duration::getSeconds)
282+
map.from(this.queueCapacity).to(taskExecutor::setQueueCapacity);
283+
map.from(this.corePoolSize).to(taskExecutor::setCorePoolSize);
284+
map.from(this.maxPoolSize).to(taskExecutor::setMaxPoolSize);
285+
map.from(this.keepAlive).asInt(Duration::getSeconds)
286286
.to(taskExecutor::setKeepAliveSeconds);
287-
map.from(() -> this.allowCoreThreadTimeOut)
288-
.to(taskExecutor::setAllowCoreThreadTimeOut);
289-
map.from(() -> this.threadNamePrefix).whenHasText()
287+
map.from(this.allowCoreThreadTimeOut).to(taskExecutor::setAllowCoreThreadTimeOut);
288+
map.from(this.threadNamePrefix).whenHasText()
290289
.to(taskExecutor::setThreadNamePrefix);
291-
map.from(() -> this.taskDecorator).to(taskExecutor::setTaskDecorator);
290+
map.from(this.taskDecorator).to(taskExecutor::setTaskDecorator);
292291

293292
if (!CollectionUtils.isEmpty(this.taskExecutorCustomizers)) {
294293
for (TaskExecutorCustomizer customizer : this.taskExecutorCustomizers) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/task/TaskSchedulerBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public ThreadPoolTaskScheduler build() {
167167
*/
168168
public <T extends ThreadPoolTaskScheduler> T configure(T taskScheduler) {
169169
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
170-
map.from(() -> this.poolSize).to(taskScheduler::setPoolSize);
171-
map.from(() -> this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
170+
map.from(this.poolSize).to(taskScheduler::setPoolSize);
171+
map.from(this.threadNamePrefix).to(taskScheduler::setThreadNamePrefix);
172172

173173
if (!CollectionUtils.isEmpty(this.taskSchedulerCustomizers)) {
174174
for (TaskSchedulerCustomizer customizer : this.taskSchedulerCustomizers) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/webservices/client/WebServiceTemplateBuilder.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,12 @@ public <T extends WebServiceTemplate> T configure(T webServiceTemplate) {
502502
configureMessageSenders(webServiceTemplate);
503503
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
504504
applyCustomizers(webServiceTemplate, this.internalCustomizers);
505-
map.from(() -> this.marshaller).to(webServiceTemplate::setMarshaller);
506-
map.from(() -> this.unmarshaller).to(webServiceTemplate::setUnmarshaller);
507-
map.from(() -> this.destinationProvider)
508-
.to(webServiceTemplate::setDestinationProvider);
509-
map.from(() -> this.transformerFactoryClass)
505+
map.from(this.marshaller).to(webServiceTemplate::setMarshaller);
506+
map.from(this.unmarshaller).to(webServiceTemplate::setUnmarshaller);
507+
map.from(this.destinationProvider).to(webServiceTemplate::setDestinationProvider);
508+
map.from(this.transformerFactoryClass)
510509
.to(webServiceTemplate::setTransformerFactoryClass);
511-
map.from(() -> this.messageFactory).to(webServiceTemplate::setMessageFactory);
510+
map.from(this.messageFactory).to(webServiceTemplate::setMessageFactory);
512511
if (!CollectionUtils.isEmpty(this.interceptors)) {
513512
Set<ClientInterceptor> merged = new LinkedHashSet<>(this.interceptors);
514513
if (webServiceTemplate.getInterceptors() != null) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,37 @@ public class PropertyMapperTests {
3737
@Rule
3838
public ExpectedException thrown = ExpectedException.none();
3939

40+
@Test
41+
public void fromNullValue() {
42+
ExampleDest dest = new ExampleDest();
43+
this.map.from((String) null).to(dest::setName);
44+
assertThat(dest.getName()).isNull();
45+
}
46+
47+
@Test
48+
public void fromValue() {
49+
ExampleDest dest = new ExampleDest();
50+
this.map.from("Hello World").to(dest::setName);
51+
assertThat(dest.getName()).isEqualTo("Hello World");
52+
}
53+
54+
@Test
55+
public void fromValueAsIntShouldAdaptSupplier() {
56+
Integer result = this.map.from("123").asInt(Long::valueOf)
57+
.toInstance(Integer::new);
58+
assertThat(result).isEqualTo(123);
59+
}
60+
61+
@Test
62+
public void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
63+
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assert::fail);
64+
}
65+
4066
@Test
4167
public void fromWhenSupplierIsNullShouldThrowException() {
4268
this.thrown.expect(IllegalArgumentException.class);
4369
this.thrown.expectMessage("Supplier must not be null");
44-
this.map.from(null);
70+
this.map.from((Supplier<?>) null);
4571
}
4672

4773
@Test
@@ -94,24 +120,24 @@ public void whenNonNullWhenSuppliedThrowsNullPointerExceptionShouldNotMap() {
94120

95121
@Test
96122
public void whenTrueWhenValueIsTrueShouldMap() {
97-
Boolean result = this.map.from(() -> true).whenTrue().toInstance(Boolean::new);
123+
Boolean result = this.map.from(true).whenTrue().toInstance(Boolean::new);
98124
assertThat(result).isTrue();
99125
}
100126

101127
@Test
102128
public void whenTrueWhenValueIsFalseShouldNotMap() {
103-
this.map.from(() -> false).whenTrue().toCall(Assert::fail);
129+
this.map.from(false).whenTrue().toCall(Assert::fail);
104130
}
105131

106132
@Test
107133
public void whenFalseWhenValueIsFalseShouldMap() {
108-
Boolean result = this.map.from(() -> false).whenFalse().toInstance(Boolean::new);
134+
Boolean result = this.map.from(false).whenFalse().toInstance(Boolean::new);
109135
assertThat(result).isFalse();
110136
}
111137

112138
@Test
113139
public void whenFalseWhenValueIsTrueShouldNotMap() {
114-
this.map.from(() -> true).whenFalse().toCall(Assert::fail);
140+
this.map.from(true).whenFalse().toCall(Assert::fail);
115141
}
116142

117143
@Test
@@ -121,30 +147,29 @@ public void whenHasTextWhenValueIsNullShouldNotMap() {
121147

122148
@Test
123149
public void whenHasTextWhenValueIsEmptyShouldNotMap() {
124-
this.map.from(() -> "").whenHasText().toCall(Assert::fail);
150+
this.map.from("").whenHasText().toCall(Assert::fail);
125151
}
126152

127153
@Test
128154
public void whenHasTextWhenValueHasTextShouldMap() {
129-
Integer result = this.map.from(() -> 123).whenHasText().toInstance(Integer::new);
155+
Integer result = this.map.from(123).whenHasText().toInstance(Integer::new);
130156
assertThat(result).isEqualTo(123);
131157
}
132158

133159
@Test
134160
public void whenEqualToWhenValueIsEqualShouldMatch() {
135-
String result = this.map.from(() -> "123").whenEqualTo("123")
136-
.toInstance(String::new);
161+
String result = this.map.from("123").whenEqualTo("123").toInstance(String::new);
137162
assertThat(result).isEqualTo("123");
138163
}
139164

140165
@Test
141166
public void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
142-
this.map.from(() -> "123").whenEqualTo("321").toCall(Assert::fail);
167+
this.map.from("123").whenEqualTo("321").toCall(Assert::fail);
143168
}
144169

145170
@Test
146171
public void whenInstanceOfWhenValueIsTargetTypeShouldMatch() {
147-
Long result = this.map.from(() -> 123L).whenInstanceOf(Long.class)
172+
Long result = this.map.from(123L).whenInstanceOf(Long.class)
148173
.toInstance((value) -> value + 1);
149174
assertThat(result).isEqualTo(124L);
150175
}
@@ -157,14 +182,13 @@ public void whenInstanceOfWhenValueIsNotTargetTypeShouldNotMatch() {
157182

158183
@Test
159184
public void whenWhenValueMatchesShouldMap() {
160-
String result = this.map.from(() -> "123").when("123"::equals)
161-
.toInstance(String::new);
185+
String result = this.map.from("123").when("123"::equals).toInstance(String::new);
162186
assertThat(result).isEqualTo("123");
163187
}
164188

165189
@Test
166190
public void whenWhenValueDoesNotMatchShouldNotMap() {
167-
this.map.from(() -> "123").when("321"::equals).toCall(Assert::fail);
191+
this.map.from("123").when("321"::equals).toCall(Assert::fail);
168192
}
169193

170194
@Test

0 commit comments

Comments
 (0)