parent
a0c00362c3
commit
a6bede22c1
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
|
@ -45,19 +45,20 @@ public interface Converter<S, T> {
|
|||
T convert(S source);
|
||||
|
||||
/**
|
||||
* Construct a composed {@link Converter} that first applies this {@link Converter} to
|
||||
* its input, and then applies the {@code after} {@link Converter} to the result.
|
||||
*
|
||||
* @since 5.2
|
||||
* @param <U> the type of output of both the {@code after} {@link Converter} and the
|
||||
* composed {@link Converter}
|
||||
* Construct a composed {@link Converter} that first applies this {@link Converter}
|
||||
* to its input, and then applies the {@code after} {@link Converter} to the
|
||||
* result.
|
||||
* @param after the {@link Converter} to apply after this {@link Converter}
|
||||
* is applied
|
||||
* @return a composed {@link Converter} that first applies this {@link Converter} and then
|
||||
* applies the {@code after} {@link Converter}
|
||||
* @param <U> the type of output of both the {@code after} {@link Converter}
|
||||
* and the composed {@link Converter}
|
||||
* @return a composed {@link Converter} that first applies this {@link Converter}
|
||||
* and then applies the {@code after} {@link Converter}
|
||||
* @since 5.3
|
||||
*/
|
||||
default <U> Converter<S, U> andThen(Converter<? super T, ? extends U> after) {
|
||||
Assert.notNull(after, "after cannot be null");
|
||||
Assert.notNull(after, "after must not be null");
|
||||
return (S s) -> after.convert(convert(s));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,28 +1,63 @@
|
|||
/*
|
||||
* Copyright 2002-2020 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.core.convert.converter;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link Converter}
|
||||
*
|
||||
* @author Josh Cummings
|
||||
* @author Sam Brannen
|
||||
* @since 5.3
|
||||
*/
|
||||
public class ConverterTests {
|
||||
Converter<Integer, Integer> moduloTwo = number -> number % 2;
|
||||
class ConverterTests {
|
||||
|
||||
private final Converter<Integer, Integer> moduloTwo = number -> number % 2;
|
||||
private final Converter<Integer, Integer> addOne = number -> number + 1;
|
||||
|
||||
|
||||
@Test
|
||||
public void andThenWhenGivenANullConverterThenThrowsException() {
|
||||
assertThatExceptionOfType(IllegalArgumentException.class)
|
||||
.isThrownBy(() -> this.moduloTwo.andThen(null));
|
||||
void andThenWhenGivenANullConverterThenThrowsException() {
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> this.moduloTwo.andThen(null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void andThenWhenGivenConverterThenComposesInOrder() {
|
||||
Converter<Integer, Integer> addOne = number-> number + 1;
|
||||
assertThat(this.moduloTwo.andThen(addOne).convert(13)).isEqualTo(2);
|
||||
assertThat(addOne.andThen(this.moduloTwo).convert(13)).isEqualTo(0);
|
||||
void andThenWhenGivenConverterThenComposesInOrder() {
|
||||
assertThat(this.moduloTwo.andThen(this.addOne).convert(13)).isEqualTo(2);
|
||||
assertThat(this.addOne.andThen(this.moduloTwo).convert(13)).isEqualTo(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void andThenCanConvertfromDifferentSourceType() {
|
||||
Converter<String, Integer> length = String::length;
|
||||
assertThat(length.andThen(this.moduloTwo).convert("example")).isEqualTo(1);
|
||||
assertThat(length.andThen(this.addOne).convert("example")).isEqualTo(8);
|
||||
}
|
||||
|
||||
@Test
|
||||
void andThenCanConvertToDifferentTargetType() {
|
||||
Converter<String, Integer> length = String::length;
|
||||
Converter<Integer, String> toString = Object::toString;
|
||||
assertThat(length.andThen(toString).convert("example")).isEqualTo("7");
|
||||
assertThat(toString.andThen(length).convert(1_000)).isEqualTo(4);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue