All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.opencredo.concursus.domain.json.events.channels.JsonEventOutChannel Maven / Gradle / Ivy

The newest version!
package com.opencredo.concursus.domain.json.events.channels;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.opencredo.concursus.domain.events.Event;
import com.opencredo.concursus.domain.events.channels.EventOutChannel;
import com.opencredo.concursus.domain.json.events.EventJson;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 * A channel through which {@link Event}s can be sent out of the system in JSON serialised form.
 */
public final class JsonEventOutChannel implements EventOutChannel {

    /**
     * Construct an {@link EventOutChannel} which serialises outgoing {@link Event}s to JSON.
     * @param objectMapper The {@link ObjectMapper} to use for serialisation.
     * @param eventAndJsonConsumer The {@link Consumer} that will receive the {@link Event} and its JSON serialisation.
     * @return The constructed {@link EventOutChannel}.
     */
    public static JsonEventOutChannel using(ObjectMapper objectMapper, BiConsumer eventAndJsonConsumer) {
        return new JsonEventOutChannel(objectMapper, eventAndJsonConsumer);
    }

    /**
     * Construct an {@link EventOutChannel} which serialises outgoing {@link Event}s to JSON.
     * @param objectMapper The {@link ObjectMapper} to use for serialisation.
     * @param jsonConsumer The {@link Consumer} that will receive the serialised {@link Event}s.
     * @return The constructed {@link EventOutChannel}.
     */
    public static JsonEventOutChannel using(ObjectMapper objectMapper, Consumer jsonConsumer) {
        return new JsonEventOutChannel(objectMapper, (e, j) -> jsonConsumer.accept(j));
    }

    private final ObjectMapper objectMapper;
    private final BiConsumer eventAndJsonConsumer;

    private JsonEventOutChannel(ObjectMapper objectMapper, BiConsumer eventAndJsonConsumer) {
        this.objectMapper = objectMapper;
        this.eventAndJsonConsumer = eventAndJsonConsumer;
    }

    @Override
    public void accept(Event event) {
        eventAndJsonConsumer.accept(event, EventJson.toString(event, objectMapper));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy