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");
|
* 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.
|
||||||
|
|
|
||||||
|
|
@ -91,24 +91,13 @@ public final class PropertyMapper {
|
||||||
return new PropertyMapper(this, operator);
|
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
|
* Return a new {@link Source} from the specified value supplier that can be used to
|
||||||
* perform the mapping.
|
* perform the mapping.
|
||||||
* @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");
|
||||||
|
|
@ -119,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) {
|
||||||
|
|
|
||||||
|
|
@ -120,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
|
||||||
|
|
@ -147,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);
|
||||||
}
|
}
|
||||||
|
|
@ -183,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…
Reference in New Issue