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

dev.struchkov.godfather.main.domain.content.Message Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
package dev.struchkov.godfather.main.domain.content;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import dev.struchkov.godfather.main.domain.jackson.TelegramPayloadDeserializer;
import dev.struchkov.godfather.main.domain.jackson.TelegramPayloadSerializer;
import dev.struchkov.haiti.utils.container.ContextKey;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static dev.struchkov.haiti.utils.Checker.checkNotNull;

/**
 * Абстрактная сущность - Сообщение от пользователя.
 *
 * @author upagge [08/07/2019]
 */
@Getter
@Setter
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public abstract class Message {

    @EqualsAndHashCode.Include
    protected String id;

    /**
     * Тип сообщения.
     */
    protected ContentType contentType;

    /**
     * Дата создания.
     */
    protected LocalDateTime createDate;

    /**
     * Идентификатор пользователя, отправившего сообщение.
     */
    protected String fromPersonId;

    /**
     * Текстовое сообщение.
     */
    protected String text;

    @JsonSerialize(using = TelegramPayloadSerializer.class)
    @JsonDeserialize(using = TelegramPayloadDeserializer.class)
    protected Map payload = new HashMap<>();

    protected Message(Message source) {
        this.id = source.getId();
        this.fromPersonId = source.getFromPersonId();
        this.text = source.getText();
        this.createDate = source.getCreateDate();
        this.contentType = source.getContentType();
        this.payload = source.getPayload();
    }

    protected Message() {
    }

    public  void addPayload(ContextKey key, T value) {
        if (checkNotNull(value)) {
            payload.put(key, value);
        }
    }

    public  Optional getPayLoad(ContextKey contextKey) {
        return Optional.ofNullable(payload.get(contextKey))
                .map(value -> (T) value);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy