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

hu.icellmobilsoft.coffee.module.redisstream.common.RedisStreamPublication Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
/*-
 * #%L
 * Coffee
 * %%
 * Copyright (C) 2020 i-Cell Mobilsoft Zrt.
 * %%
 * 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
 * 
 *      http://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.
 * #L%
 */
package hu.icellmobilsoft.coffee.module.redisstream.common;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;

import hu.icellmobilsoft.coffee.module.redisstream.config.StreamMessageParameter;

/**
 * Value class for redis stream publications
 *
 * @author martin.nagy
 * @since 1.3.0
 */
public class RedisStreamPublication {

    /**
     * Stream group to send the message (another than initialized)
     */
    private final String streamGroup;

    /**
     * Message in stream. Can be String or JSON
     */
    private final String streamMessage;

    /**
     * Message parameters. Map key value is standardized from {@link StreamMessageParameter} enum value
     */
    private Map parameters;

    /**
     * Creates the value class for redis stream publication
     *
     * @param streamGroup
     *            Stream group to send the message (another than initialized), nullable
     * @param streamMessage
     *            Message in stream. Can be String or JSON
     * @return the create value class
     */
    public static RedisStreamPublication of(String streamGroup, String streamMessage) {
        return of(streamGroup, streamMessage, null);
    }

    /**
     * Creates the value class for redis stream publication
     * 
     * @param streamMessage
     *            Message in stream. Can be String or JSON
     * @return the create value class
     */
    public static RedisStreamPublication of(String streamMessage) {
        return of(null, streamMessage, null);
    }

    /**
     * Creates the value class for redis stream publication
     * 
     * @param streamMessage
     *            Message in stream. Can be String or JSON
     * @param parameters
     *            Message parameters, nullable. Map key value is standardized in {@link StreamMessageParameter} enum value
     * @return the create value class
     */
    public static RedisStreamPublication of(String streamMessage, Map parameters) {
        return of(null, streamMessage, parameters);
    }

    /**
     * Set {@code StreamMessageParameter#TTL} parameter to message. If a parameter exists, it will be overwritten
     * 
     * @param expiryInSec
     *            expiry in seconds
     * @return Same object with setted {@code StreamMessageParameter#TTL} parameter
     */
    public RedisStreamPublication withTTL(long expiryInSec) {
        this.withParameter(StreamMessageParameter.TTL, Instant.now().plus(expiryInSec, ChronoUnit.SECONDS).toEpochMilli());
        return this;
    }

    /**
     * Set standard parameter to message. If a parameter exists, it will be overwritten
     * 
     * @param parameterKey
     *            parameter key
     * @param parameterValue
     *            parameter value
     * @return Same object with setted parameter
     */
    public RedisStreamPublication withParameter(StreamMessageParameter parameterKey, Object parameterValue) {
        this.getInitializedParameters().put(parameterKey.getMessageKey(), String.valueOf(parameterValue));
        return this;
    }

    /**
     * Set custom parameter to message. If a parameter exists, it will be overwritten
     * 
     * @param parameterKey
     *            Custom parameter key
     * @param parameterValue
     *            Custom parameter value
     * @return Same object with setted parameter
     */
    public RedisStreamPublication withParameter(String parameterKey, Object parameterValue) {
        this.getInitializedParameters().put(parameterKey, String.valueOf(parameterValue));
        return this;
    }

    private Map getInitializedParameters() {
        if (this.getParameters() == null) {
            this.parameters = new HashMap<>();
        }
        return this.getParameters();
    }

    /**
     * Creates the value class for redis stream publication
     *
     * @param streamGroup
     *            Stream group to send the message (another than initialized), nullable
     * @param streamMessage
     *            Message in stream. Can be String or JSON
     * @param parameters
     *            Message parameters, nullable. Map key value is standardized in {@link StreamMessageParameter} enum value
     * @return the create value class
     */
    public static RedisStreamPublication of(String streamGroup, String streamMessage, Map parameters) {
        return new RedisStreamPublication(streamGroup, streamMessage, parameters);
    }

    private RedisStreamPublication(String streamGroup, String streamMessage, Map parameters) {
        this.streamGroup = streamGroup;
        this.streamMessage = streamMessage;
        this.parameters = parameters;
    }

    public String getStreamGroup() {
        return streamGroup;
    }

    public String getStreamMessage() {
        return streamMessage;
    }

    public Map getParameters() {
        return parameters;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy