Created
January 29, 2019 22:12
-
-
Save fmbenhassine/5e0a1f32f5046784eaa45d9ed8cc0c1a to your computer and use it in GitHub Desktop.
#SpringBatch java.util.function components
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
package org.springframework.batch.item.function; | |
import java.util.List; | |
import java.util.function.Consumer; | |
import org.springframework.batch.item.ItemWriter; | |
import org.springframework.util.Assert; | |
public class ConsumerItemWriter<T> implements ItemWriter<T> { | |
private Consumer<T> consumer; | |
public ConsumerItemWriter(Consumer<T> consumer) { | |
Assert.notNull(consumer, "A consumer is required"); | |
this.consumer = consumer; | |
} | |
@Override | |
public void write(List<? extends T> items) throws Exception { | |
items.forEach(this.consumer); | |
} | |
} |
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
package org.springframework.batch.item.function; | |
import java.util.function.Predicate; | |
import org.springframework.batch.item.ItemProcessor; | |
import org.springframework.util.Assert; | |
public class PredicateFilteringItemProcessor<T> implements ItemProcessor<T, T> { | |
private Predicate<T> predicate; | |
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) ? item : null; | |
} | |
} |
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
package org.springframework.batch.item.function; | |
import java.util.function.Supplier; | |
import org.springframework.batch.item.ItemReader; | |
import org.springframework.util.Assert; | |
public class SupplierItemReader<T> implements ItemReader<T> { | |
private Supplier<T> supplier; | |
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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment