Skip to content

Commit 6a9ed80

Browse files
committed
Code review feedback - rename toOptionalInstance -> toInstance (with a boolean param)
1 parent 45a52c6 commit 6a9ed80

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Predicate;
2525
import java.util.function.Supplier;
2626

27+
import org.springframework.lang.Nullable;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.StringUtils;
2930
import org.springframework.util.function.SingletonSupplier;
@@ -302,27 +303,31 @@ public void to(Consumer<T> consumer) {
302303
* @throws NoSuchElementException if the value has been filtered
303304
*/
304305
public <R> R toInstance(Function<T, R> factory) {
305-
Assert.notNull(factory, "Factory must not be null");
306-
T value = this.supplier.get();
307-
if (!this.predicate.test(value)) {
308-
throw new NoSuchElementException("No value present");
309-
}
310-
return factory.apply(value);
306+
return toInstance(factory, true);
311307
}
312308

313309
/**
314-
* Complete the mapping by creating a new instance
310+
* Complete the mapping by creating a new instance from the non-filtered value.
315311
* @param <R> the resulting type
316312
* @param factory the factory used to create the instance
317-
* @return the created instance or empty when the value is filtered
313+
* @param failIfFiltered whether to throw exception or return {@code null} if the
314+
* value has been filtered
315+
* @return the instance or {@code null} if the value has been filtered and
316+
* {@code failIfFiltered} is {@code false}
317+
* @throws NoSuchElementException if the value has been filtered and
318+
* {@code failIfFiltered} is {@code true}
318319
*/
319-
public <R> Optional<R> toOptionalInstance(Function<T, R> factory) {
320+
@Nullable
321+
public <R> R toInstance(Function<T, R> factory, boolean failIfFiltered) {
320322
Assert.notNull(factory, "Factory must not be null");
321323
T value = this.supplier.get();
322324
if (!this.predicate.test(value)) {
323-
return Optional.empty();
325+
if (failIfFiltered) {
326+
throw new NoSuchElementException("No value present");
327+
}
328+
return null;
324329
}
325-
return Optional.ofNullable(factory.apply(value));
330+
return factory.apply(value);
326331
}
327332

328333
/**

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

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

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

19-
import java.util.Optional;
2019
import java.util.function.Supplier;
2120

2221
import org.junit.jupiter.api.Assertions;
@@ -210,21 +209,21 @@ void whenWhenValueMatchesShouldSupportChainedCalls() {
210209
}
211210

212211
@Test
213-
void toOptionalInstanceWithNonFilteredValueShouldReturnFactoryValue() {
214-
Optional<String> result = this.map.from("123").whenEqualTo("123").toOptionalInstance(String::new);
215-
assertThat(result).isNotEmpty().hasValue("123");
212+
void toInstanceWithoutFailFlagWithNonFilteredValueShouldReturnFactoryValue() {
213+
String result = this.map.from("123").whenEqualTo("123").toInstance(String::new, false);
214+
assertThat(result).isEqualTo("123");
216215
}
217216

218217
@Test
219-
void toOptionalInstanceWithNonFilteredValueShouldTolerateNullFactoryValue() {
220-
Optional<String> result = this.map.from("123").whenEqualTo("123").toOptionalInstance((s) -> null);
221-
assertThat(result).isEmpty();
218+
void toInstanceWithoutFailFlagWithNonFilteredValueShouldTolerateNullFactoryValue() {
219+
String result = this.map.from("123").whenEqualTo("123").toInstance((s) -> null, false);
220+
assertThat(result).isNull();
222221
}
223222

224223
@Test
225-
void toOptionalInstanceWithFilteredValueShouldReturnEmpty() {
226-
Optional<String> result = this.map.from("123").whenEqualTo("foo").toOptionalInstance(String::new);
227-
assertThat(result).isEmpty();
224+
void toInstanceWithoutFailFlagWithFilteredValueShouldReturnNull() {
225+
String result = this.map.from("123").whenEqualTo("foo").toInstance(String::new, false);
226+
assertThat(result).isNull();
228227
}
229228

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

0 commit comments

Comments
 (0)