Skip to content

Commit d8af0cd

Browse files
committed
Code review feedback - toOptionalInstance
1 parent 5e12a5e commit d8af0cd

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.NoSuchElementException;
2020
import java.util.Objects;
21+
import java.util.Optional;
2122
import java.util.function.Consumer;
2223
import java.util.function.Function;
2324
import java.util.function.Predicate;
@@ -310,20 +311,18 @@ public <R> R toInstance(Function<T, R> factory) {
310311
}
311312

312313
/**
313-
* Complete the mapping by creating a new instance from the non-filtered value or
314-
* using the specified default instance when the value is filtered.
314+
* Complete the mapping by creating a new instance from the non-filtered value.
315315
* @param <R> the resulting type
316316
* @param factory the factory used to create the instance
317-
* @param defaultInstance the instance to use if the value has been filtered
318-
* @return the instance
317+
* @return the created instance or empty when the value is filtered
319318
*/
320-
public <R> R toInstance(Function<T, R> factory, R defaultInstance) {
319+
public <R> Optional<R> toOptionalInstance(Function<T, R> factory) {
321320
Assert.notNull(factory, "Factory must not be null");
322321
T value = this.supplier.get();
323322
if (!this.predicate.test(value)) {
324-
return defaultInstance;
323+
return Optional.empty();
325324
}
326-
return factory.apply(value);
325+
return Optional.ofNullable(factory.apply(value));
327326
}
328327

329328
/**

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.context.properties;
1818

19+
import java.util.Optional;
1920
import java.util.function.Supplier;
2021

2122
import org.junit.jupiter.api.Assertions;
@@ -209,21 +210,21 @@ void whenWhenValueMatchesShouldSupportChainedCalls() {
209210
}
210211

211212
@Test
212-
void whenValueNotFilteredIsInstanceWithDefaultShouldReturnFactoryInstance() {
213-
String result = this.map.from("123").when((s) -> s.contains("2")).toInstance(String::new, "foo");
214-
assertThat(result).isEqualTo("123");
213+
void toOptionalInstanceWithNonFilteredValueShouldReturnFactoryValue() {
214+
Optional<String> result = this.map.from("123").whenEqualTo("123").toOptionalInstance(String::new);
215+
assertThat(result).isNotEmpty().hasValue("123");
215216
}
216217

217218
@Test
218-
void whenValueFilteredIsInstanceWithDefaultShouldReturnDefault() {
219-
String result = this.map.from("123").when((s) -> s.contains("x")).toInstance(String::new, "foo");
220-
assertThat(result).isEqualTo("foo");
219+
void toOptionalInstanceWithNonFilteredValueShouldTolerateNullFactoryValue() {
220+
Optional<String> result = this.map.from("123").whenEqualTo("123").toOptionalInstance((s) -> null);
221+
assertThat(result).isEmpty();
221222
}
222223

223224
@Test
224-
void whenValueFilteredIsInstanceWithNullDefaultShouldReturnNull() {
225-
String result = this.map.from("123").when((s) -> s.contains("x")).toInstance(String::new, null);
226-
assertThat(result).isNull();
225+
void toOptionalInstanceWithFilteredValueShouldReturnEmpty() {
226+
Optional<String> result = this.map.from("123").whenEqualTo("foo").toOptionalInstance(String::new);
227+
assertThat(result).isEmpty();
227228
}
228229

229230
static class Count<T> implements Supplier<T> {

0 commit comments

Comments
 (0)