Skip to content

Add mapping adapter to ItemWriter #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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.batch.item;

import java.util.function.Function;

import org.springframework.lang.NonNull;

/**
Expand All @@ -37,6 +38,7 @@
* @author Lucas Ward
* @author Taeik Lim
* @author Mahmoud Ben Hassine
* @author Stefano Cordio
*/
@FunctionalInterface
public interface ItemWriter<T> {
Expand All @@ -50,4 +52,27 @@ public interface ItemWriter<T> {
*/
void write(@NonNull Chunk<? extends T> chunk) throws Exception;

/**
* Adapts an {@code 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 mapping()} item writers are most useful when used in combination with a
* {@link org.springframework.batch.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
* @param mapper a function to be applied to the input items
* @param downstream an item writer which will accept mapped items
* @return an item writer which applies the mapping function to the input items and
* provides the mapped results to the downstream item writer
* @since 6.0
*/
static <T, U> ItemWriter<T> mapping(Function<? super T, ? extends U> mapper, ItemWriter<? super U> downstream) {
return chunk -> downstream.write(new Chunk<>(chunk.getItems().stream().map(mapper).toList()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.springframework.batch.item;

import org.junit.jupiter.api.Test;
import org.springframework.batch.item.support.CompositeItemWriter;
import org.springframework.batch.item.support.ListItemWriter;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.batch.item.ItemWriter.mapping;

class ItemWriterIntegrationTests {

@Test
void testMappingWithCompositeItemWriter() throws Exception {
ListItemWriter<String> nameItemWriter = new ListItemWriter<>();
ListItemWriter<Integer> ageItemWriter = new ListItemWriter<>();

record Person(String name, int age) {
}

ItemWriter<Person> personItemWriter = new CompositeItemWriter<>(List.of( //
mapping(Person::name, nameItemWriter), //
mapping(Person::age, ageItemWriter)));

personItemWriter.write(Chunk.of(new Person("Foo", 42), new Person("Bar", 24)));
personItemWriter.write(Chunk.of(new Person("Baz", 21), new Person("Qux", 12)));

assertThat(nameItemWriter.getWrittenItems()).containsExactly("Foo", "Bar", "Baz", "Qux");
assertThat(ageItemWriter.getWrittenItems()).containsExactly(42, 24, 21, 12);
}

}
Loading