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

cz.mmsparams.api.utils.MessageBuilder Maven / Gradle / Ivy

package cz.mmsparams.api.utils;

import cz.mmsparams.api.interfaces.ITestIdProvider;
import cz.mmsparams.api.websocket.MessageIdGenerator;
import cz.mmsparams.api.websocket.WebSocketConstants;
import cz.mmsparams.api.websocket.WebSocketMessageBase;

public class MessageBuilder
{
    private String senderKey;
    private String recipientKey;
    private String messageID;
    private String testID;

    public MessageBuilder withSenderKey(String senderKey)
    {
        this.senderKey = senderKey;
        return this;
    }

    public MessageBuilder withRecipientKey(String recipientKey)
    {
        this.recipientKey = recipientKey;
        return this;
    }

    public MessageBuilder withRecipientKeyServer()
    {
        this.recipientKey = WebSocketConstants.Server_Recipient_Key;
        return this;
    }

    public MessageBuilder withSenderKeyServer()
    {
        this.senderKey = WebSocketConstants.Server_Recipient_Key;
        return this;
    }

    public MessageBuilder withRecipientKeyClientBroadcast()
    {
        this.recipientKey = WebSocketConstants.Server_To_Client_Broadcast;
        return this;
    }

    private MessageBuilder withTestId(String testId)
    {
        this.testID = testId;
        return this;
    }

    public MessageBuilder withTestId(ITestIdProvider iTestIdProvider)
    {
        if (iTestIdProvider != null)
            this.testID = iTestIdProvider.getTestId();
        return this;
    }

    public MessageBuilder withNoTestId()
    {
        this.testID = null;
        return this;
    }

    public MessageBuilder withMessageId(String messageId)
    {
        this.messageID = messageId;
        return this;
    }

    public MessageBuilder withMessageIdRandom()
    {
        this.messageID = MessageIdGenerator.getNext();
        return this;
    }


    public  T build(Class clazz)
    {
        T msg = MessageFactory.create(clazz);
        msg.setSenderKey(senderKey);
        msg.setRecipientKey(recipientKey);
        msg.setTestID(testID);
        msg.setMessageID(messageID);
        return msg;
    }


    public static  T build(Class clazz, ITestIdProvider iTestIdProvider, String senderKey, String recipientKey, String messageID)
    {
        return new MessageBuilder()
                .withTestId(iTestIdProvider)
                .withSenderKey(senderKey)
                .withRecipientKey(recipientKey)
                .withMessageId(messageID)
                .build(clazz);
    }


    public  MessageBuilder withPrepareResponse(R request)
    {
        return this
                .withMessageId(request.getMessageID())
                .withTestId(request.getTestID())
                .withRecipientKey(request.getSenderKey());
    }



}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy