@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.springframework.batch.item.Chunk;
|
||||
import org.springframework.batch.item.ItemWriter;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapter for a {@link Consumer} to an {@link ItemWriter}.
|
||||
*
|
||||
* @param <T> type of items to write
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 5.2
|
||||
*/
|
||||
public class ConsumerItemWriter<T> implements ItemWriter<T> {
|
||||
|
||||
private final Consumer<T> consumer;
|
||||
|
||||
/**
|
||||
* Create a new {@link ConsumerItemWriter}.
|
||||
* @param consumer the consumer to use to write items. Must not be {@code null}.
|
||||
*/
|
||||
public ConsumerItemWriter(Consumer<T> consumer) {
|
||||
Assert.notNull(consumer, "A consumer is required");
|
||||
this.consumer = consumer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(Chunk<? extends T> items) throws Exception {
|
||||
items.forEach(this.consumer);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.batch.item.ItemProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A filtering {@link ItemProcessor} that is based on a {@link Predicate}. Items for which
|
||||
* the predicate returns {@code true} will be filtered.
|
||||
*
|
||||
* @param <T> type of item to process
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 5.2
|
||||
*/
|
||||
public class PredicateFilteringItemProcessor<T> implements ItemProcessor<T, T> {
|
||||
|
||||
private final Predicate<T> predicate;
|
||||
|
||||
/**
|
||||
* Create a new {@link PredicateFilteringItemProcessor}.
|
||||
* @param predicate the predicate to use to filter items. Must not be {@code null}.
|
||||
*/
|
||||
public PredicateFilteringItemProcessor(Predicate<T> predicate) {
|
||||
Assert.notNull(predicate, "A predicate is required");
|
||||
this.predicate = predicate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T process(T item) throws Exception {
|
||||
return this.predicate.test(item) ? null : item;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Adapter for a {@link Supplier} to an {@link ItemReader}.
|
||||
*
|
||||
* @param <T> type of items to read
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @since 5.2
|
||||
*/
|
||||
public class SupplierItemReader<T> implements ItemReader<T> {
|
||||
|
||||
private final Supplier<T> supplier;
|
||||
|
||||
/**
|
||||
* Create a new {@link SupplierItemReader}.
|
||||
* @param supplier the supplier to use to read items. Must not be {@code null}.
|
||||
*/
|
||||
public SupplierItemReader(Supplier<T> supplier) {
|
||||
Assert.notNull(supplier, "A supplier is required");
|
||||
this.supplier = supplier;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read() throws Exception {
|
||||
return this.supplier.get();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.batch.item.Chunk;
|
||||
|
||||
/**
|
||||
* Test class for {@link ConsumerItemWriter}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
class ConsumerItemWriterTests {
|
||||
|
||||
private final List<String> items = new ArrayList<>();
|
||||
|
||||
private final Consumer<String> consumer = items::add;
|
||||
|
||||
@Test
|
||||
void testMandatoryConsumer() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> new ConsumerItemWriter<String>(null),
|
||||
"A consumer is required");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testWrite() throws Exception {
|
||||
// given
|
||||
Chunk<String> chunk = Chunk.of("foo", "bar");
|
||||
ConsumerItemWriter<String> consumerItemWriter = new ConsumerItemWriter<>(this.consumer);
|
||||
|
||||
// when
|
||||
consumerItemWriter.write(chunk);
|
||||
|
||||
// then
|
||||
Assertions.assertIterableEquals(chunk, this.items);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test class for {@link PredicateFilteringItemProcessor}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
class PredicateFilteringItemProcessorTests {
|
||||
|
||||
private final Predicate<String> foos = item -> item.startsWith("foo");
|
||||
|
||||
@Test
|
||||
void testMandatoryPredicate() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> new PredicateFilteringItemProcessor<String>(null),
|
||||
"A predicate is required");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testProcess() throws Exception {
|
||||
// given
|
||||
PredicateFilteringItemProcessor<String> processor = new PredicateFilteringItemProcessor<>(this.foos);
|
||||
|
||||
// when & then
|
||||
Assertions.assertNull(processor.process("foo1"));
|
||||
Assertions.assertNull(processor.process("foo2"));
|
||||
Assertions.assertEquals("bar", processor.process("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2024 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.item.function;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Test class for {@link SupplierItemReader}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
*/
|
||||
class SupplierItemReaderTests {
|
||||
|
||||
private final Supplier<String> supplier = new Supplier<>() {
|
||||
private int count = 1;
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
return count <= 2 ? "foo" + count++ : null;
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
void testMandatorySupplier() {
|
||||
Assertions.assertThrows(IllegalArgumentException.class, () -> new SupplierItemReader<String>(null),
|
||||
"A supplier is required");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRead() throws Exception {
|
||||
// given
|
||||
SupplierItemReader<String> supplierItemReader = new SupplierItemReader<>(supplier);
|
||||
|
||||
// when & then
|
||||
Assertions.assertEquals("foo1", supplierItemReader.read());
|
||||
Assertions.assertEquals("foo2", supplierItemReader.read());
|
||||
Assertions.assertNull(supplierItemReader.read());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user