From a6bede22c113e69206c13735dbf820c7cb043e2e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Tue, 29 Sep 2020 14:04:40 +0200 Subject: [PATCH] Polish contribution See gh-23379 --- .../core/convert/converter/Converter.java | 21 +++---- .../convert/converter/ConverterTests.java | 57 +++++++++++++++---- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java b/spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java index a9c893b12d..e089081c5d 100644 --- a/spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java +++ b/spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java @@ -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 { 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 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 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 Converter andThen(Converter after) { - Assert.notNull(after, "after cannot be null"); + Assert.notNull(after, "after must not be null"); return (S s) -> after.convert(convert(s)); } + } diff --git a/spring-core/src/test/java/org/springframework/core/convert/converter/ConverterTests.java b/spring-core/src/test/java/org/springframework/core/convert/converter/ConverterTests.java index 0cb4a320fa..65d519f540 100644 --- a/spring-core/src/test/java/org/springframework/core/convert/converter/ConverterTests.java +++ b/spring-core/src/test/java/org/springframework/core/convert/converter/ConverterTests.java @@ -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 moduloTwo = number -> number % 2; +class ConverterTests { + + private final Converter moduloTwo = number -> number % 2; + private final Converter 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 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); } -} \ No newline at end of file + + @Test + void andThenCanConvertfromDifferentSourceType() { + Converter 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 length = String::length; + Converter toString = Object::toString; + assertThat(length.andThen(toString).convert("example")).isEqualTo("7"); + assertThat(toString.andThen(length).convert(1_000)).isEqualTo(4); + } + +}