Polish "Add PropertyMapper.from(value)"

Closes gh-13837
pull/13837/merge
Stephane Nicoll 6 years ago
parent 1bd52bc432
commit 597fe237b5

@ -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");
* you may not use this file except in compliance with the License.

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

@ -120,24 +120,24 @@ public class PropertyMapperTests {
@Test
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();
}
@Test
public void whenTrueWhenValueIsFalseShouldNotMap() {
this.map.from(() -> false).whenTrue().toCall(Assert::fail);
this.map.from(false).whenTrue().toCall(Assert::fail);
}
@Test
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();
}
@Test
public void whenFalseWhenValueIsTrueShouldNotMap() {
this.map.from(() -> true).whenFalse().toCall(Assert::fail);
this.map.from(true).whenFalse().toCall(Assert::fail);
}
@Test
@ -147,30 +147,29 @@ public class PropertyMapperTests {
@Test
public void whenHasTextWhenValueIsEmptyShouldNotMap() {
this.map.from(() -> "").whenHasText().toCall(Assert::fail);
this.map.from("").whenHasText().toCall(Assert::fail);
}
@Test
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);
}
@Test
public void whenEqualToWhenValueIsEqualShouldMatch() {
String result = this.map.from(() -> "123").whenEqualTo("123")
.toInstance(String::new);
String result = this.map.from("123").whenEqualTo("123").toInstance(String::new);
assertThat(result).isEqualTo("123");
}
@Test
public void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
this.map.from(() -> "123").whenEqualTo("321").toCall(Assert::fail);
this.map.from("123").whenEqualTo("321").toCall(Assert::fail);
}
@Test
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);
assertThat(result).isEqualTo(124L);
}
@ -183,14 +182,13 @@ public class PropertyMapperTests {
@Test
public void whenWhenValueMatchesShouldMap() {
String result = this.map.from(() -> "123").when("123"::equals)
.toInstance(String::new);
String result = this.map.from("123").when("123"::equals).toInstance(String::new);
assertThat(result).isEqualTo("123");
}
@Test
public void whenWhenValueDoesNotMatchShouldNotMap() {
this.map.from(() -> "123").when("321"::equals).toCall(Assert::fail);
this.map.from("123").when("321"::equals).toCall(Assert::fail);
}
@Test

Loading…
Cancel
Save