Skip to content

Commit a6bede2

Browse files
committed
Polish contribution
See gh-23379
1 parent a0c0036 commit a6bede2

File tree

2 files changed

+57
-21
lines changed

2 files changed

+57
-21
lines changed

spring-core/src/main/java/org/springframework/core/convert/converter/Converter.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,19 +45,20 @@ public interface Converter<S, T> {
4545
T convert(S source);
4646

4747
/**
48-
* Construct a composed {@link Converter} that first applies this {@link Converter} to
49-
* its input, and then applies the {@code after} {@link Converter} to the result.
50-
*
51-
* @since 5.2
52-
* @param <U> the type of output of both the {@code after} {@link Converter} and the
53-
* composed {@link Converter}
48+
* Construct a composed {@link Converter} that first applies this {@link Converter}
49+
* to its input, and then applies the {@code after} {@link Converter} to the
50+
* result.
5451
* @param after the {@link Converter} to apply after this {@link Converter}
5552
* is applied
56-
* @return a composed {@link Converter} that first applies this {@link Converter} and then
57-
* applies the {@code after} {@link Converter}
53+
* @param <U> the type of output of both the {@code after} {@link Converter}
54+
* and the composed {@link Converter}
55+
* @return a composed {@link Converter} that first applies this {@link Converter}
56+
* and then applies the {@code after} {@link Converter}
57+
* @since 5.3
5858
*/
5959
default <U> Converter<S, U> andThen(Converter<? super T, ? extends U> after) {
60-
Assert.notNull(after, "after cannot be null");
60+
Assert.notNull(after, "after must not be null");
6161
return (S s) -> after.convert(convert(s));
6262
}
63+
6364
}
Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
1+
/*
2+
* Copyright 2002-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.springframework.core.convert.converter;
218

319
import org.junit.jupiter.api.Test;
420

521
import static org.assertj.core.api.Assertions.assertThat;
6-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
22+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
723

824
/**
925
* Tests for {@link Converter}
1026
*
1127
* @author Josh Cummings
28+
* @author Sam Brannen
29+
* @since 5.3
1230
*/
13-
public class ConverterTests {
14-
Converter<Integer, Integer> moduloTwo = number -> number % 2;
31+
class ConverterTests {
32+
33+
private final Converter<Integer, Integer> moduloTwo = number -> number % 2;
34+
private final Converter<Integer, Integer> addOne = number -> number + 1;
35+
1536

1637
@Test
17-
public void andThenWhenGivenANullConverterThenThrowsException() {
18-
assertThatExceptionOfType(IllegalArgumentException.class)
19-
.isThrownBy(() -> this.moduloTwo.andThen(null));
38+
void andThenWhenGivenANullConverterThenThrowsException() {
39+
assertThatIllegalArgumentException().isThrownBy(() -> this.moduloTwo.andThen(null));
2040
}
2141

2242
@Test
23-
public void andThenWhenGivenConverterThenComposesInOrder() {
24-
Converter<Integer, Integer> addOne = number-> number + 1;
25-
assertThat(this.moduloTwo.andThen(addOne).convert(13)).isEqualTo(2);
26-
assertThat(addOne.andThen(this.moduloTwo).convert(13)).isEqualTo(0);
43+
void andThenWhenGivenConverterThenComposesInOrder() {
44+
assertThat(this.moduloTwo.andThen(this.addOne).convert(13)).isEqualTo(2);
45+
assertThat(this.addOne.andThen(this.moduloTwo).convert(13)).isEqualTo(0);
2746
}
28-
}
47+
48+
@Test
49+
void andThenCanConvertfromDifferentSourceType() {
50+
Converter<String, Integer> length = String::length;
51+
assertThat(length.andThen(this.moduloTwo).convert("example")).isEqualTo(1);
52+
assertThat(length.andThen(this.addOne).convert("example")).isEqualTo(8);
53+
}
54+
55+
@Test
56+
void andThenCanConvertToDifferentTargetType() {
57+
Converter<String, Integer> length = String::length;
58+
Converter<Integer, String> toString = Object::toString;
59+
assertThat(length.andThen(toString).convert("example")).isEqualTo("7");
60+
assertThat(toString.andThen(length).convert(1_000)).isEqualTo(4);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)