-
Couldn't load subscription status.
- Fork 2.4k
Add MappingItemWriter
#4890
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add MappingItemWriter
#4890
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
...rc/main/java/org/springframework/batch/infrastructure/item/support/MappingItemWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2025 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.batch.infrastructure.item.support; | ||
|
|
||
| import org.springframework.batch.infrastructure.item.Chunk; | ||
| import org.springframework.batch.infrastructure.item.ItemWriter; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * Adapts an {@link ItemWriter} accepting items of type {@link U} to one accepting items | ||
| * of type {@link T} by applying a mapping function to each item before writing. | ||
| * <p> | ||
| * The {@code MappingItemWriter} is most useful when used in combination with a | ||
| * {@link org.springframework.batch.infrastructure.item.support.CompositeItemWriter}, | ||
| * where the mapping function in front of the downstream writer can be a getter of the | ||
| * input item or a more complex transformation logic. | ||
| * <p> | ||
| * This adapter mimics the behavior of | ||
| * {@link java.util.stream.Collectors#mapping(Function, java.util.stream.Collector)}. | ||
| * | ||
| * @param <T> the type of the input items | ||
| * @param <U> type of items accepted by downstream item writer | ||
| * @author Stefano Cordio | ||
| * @since 6.0 | ||
| */ | ||
| public class MappingItemWriter<T, U> implements ItemWriter<T> { | ||
|
|
||
| private final Function<? super T, ? extends U> mapper; | ||
|
|
||
| private final ItemWriter<? super U> downstream; | ||
|
|
||
| /** | ||
| * Create a new {@link MappingItemWriter}. | ||
| * @param mapper the mapping function to apply to the input items | ||
| * @param downstream the downstream item writer that accepts mapped items | ||
| */ | ||
| public MappingItemWriter(Function<? super T, ? extends U> mapper, ItemWriter<? super U> downstream) { | ||
| this.mapper = mapper; | ||
| this.downstream = downstream; | ||
| } | ||
|
|
||
| @Override | ||
| public void write(Chunk<? extends T> chunk) throws Exception { | ||
| downstream.write(new Chunk<>(chunk.getItems().stream().map(mapper).toList())); | ||
| } | ||
|
|
||
| } | ||
54 changes: 54 additions & 0 deletions
54
.../springframework/batch/infrastructure/item/support/MappingItemWriterIntegrationTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * Copyright 2025 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.batch.infrastructure.item.support; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.batch.infrastructure.item.Chunk; | ||
| import org.springframework.batch.infrastructure.item.ItemWriter; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * @author Stefano Cordio | ||
| */ | ||
| class MappingItemWriterIntegrationTests { | ||
|
|
||
| @Test | ||
| void testWithCompositeItemWriter() throws Exception { | ||
| // given | ||
| record Person(String name, int age) { | ||
| } | ||
|
|
||
| ListItemWriter<String> nameItemWriter = new ListItemWriter<>(); | ||
| ListItemWriter<Integer> ageItemWriter = new ListItemWriter<>(); | ||
|
|
||
| ItemWriter<Person> underTest = new CompositeItemWriter<>(List.of( // | ||
| new MappingItemWriter<>(Person::name, nameItemWriter), // | ||
| new MappingItemWriter<>(Person::age, ageItemWriter))); | ||
|
|
||
| // when | ||
| underTest.write(Chunk.of(new Person("Foo", 42), new Person("Bar", 24))); | ||
| underTest.write(Chunk.of(new Person("Baz", 21), new Person("Qux", 12))); | ||
|
|
||
| // then | ||
| assertThat(nameItemWriter.getWrittenItems()).containsExactly("Foo", "Bar", "Baz", "Qux"); | ||
| assertThat(ageItemWriter.getWrittenItems()).containsExactly(42, 24, 21, 12); | ||
| } | ||
|
|
||
| } |
83 changes: 83 additions & 0 deletions
83
...st/java/org/springframework/batch/infrastructure/item/support/MappingItemWriterTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright 2025 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.batch.infrastructure.item.support; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.batch.infrastructure.item.Chunk; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
|
|
||
| /** | ||
| * @author Stefano Cordio | ||
| */ | ||
| class MappingItemWriterTests { | ||
|
|
||
| @Test | ||
| void testWithMapperAcceptingItemSuperclass() throws Exception { | ||
| // given | ||
| Function<Entity, String> mapper = Entity::name; | ||
| ListItemWriter<String> downstream = mock(); | ||
| MappingItemWriter<Person, String> underTest = new MappingItemWriter<>(mapper, downstream); | ||
|
|
||
| // when | ||
| underTest.write(Chunk.of(new Person("Foo", 42))); | ||
|
|
||
| // then | ||
| verify(downstream).write(Chunk.of("Foo")); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithMapperProducingMappedItemSubclass() throws Exception { | ||
| // given | ||
| Function<Person, String> mapper = Person::name; | ||
| ListItemWriter<CharSequence> downstream = mock(); | ||
| MappingItemWriter<Person, CharSequence> underTest = new MappingItemWriter<>(mapper, downstream); | ||
|
|
||
| // when | ||
| underTest.write(Chunk.of(new Person("Foo", 42))); | ||
|
|
||
| // then | ||
| verify(downstream).write(Chunk.of("Foo")); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithDownstreamAcceptingMappedItemSuperclass() throws Exception { | ||
| // given | ||
| Function<Person, String> mapper = Person::name; | ||
| ListItemWriter<CharSequence> downstream = mock(); | ||
| MappingItemWriter<Person, String> underTest = new MappingItemWriter<>(mapper, downstream); | ||
|
|
||
| // when | ||
| underTest.write(Chunk.of(new Person("Foo", 42))); | ||
|
|
||
| // then | ||
| verify(downstream).write(Chunk.of("Foo")); | ||
| } | ||
|
|
||
| private interface Entity { | ||
|
|
||
| String name(); | ||
|
|
||
| } | ||
|
|
||
| private record Person(String name, int age) implements Entity { | ||
| } | ||
|
|
||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.