org.occurrent.application.composition.command.CommandConversion Maven / Gradle / Ivy
package org.occurrent.application.composition.command;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Utility functions to convert functions expecting a list of domain events into a functions expecting a stream of domain events,
* and vice versa.
*/
public class CommandConversion {
/**
* Convert a command taking Stream<T>
into one that takes a List<T>
.
*
* @param command The command to convert
* @param The type of the domain event
* @return A function that does the conversion described above.
*/
public static Function, List> toListCommand(Function, Stream> command) {
return events -> command.apply(events.stream()).collect(Collectors.toList());
}
/**
* Convert a command taking List<T>
into one that takes a Stream<T>
.
*
* @param command The command to convert
* @param The type of the domain event
* @return A function that does the conversion described above.
*/
public static Function, Stream> toStreamCommand(Function, List> command) {
return events -> command.apply(events.collect(Collectors.toList())).stream();
}
}