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

io.snice.protocol.RequestSupport Maven / Gradle / Ivy

package io.snice.protocol;

import java.util.Optional;

import static io.snice.preconditions.PreConditions.assertNotNull;

public class RequestSupport extends TransactionSupport implements Request {

    protected RequestSupport(final TransactionId transactionId, final O owner, final Optional payload) {
        super(transactionId, owner, payload);
    }

    protected RequestSupport(final TransactionId transactionId, final O owner) {
        super(transactionId, owner);
    }

    protected RequestSupport(final O owner) {
        super(TransactionId.generateDefault(), owner);
    }

    protected RequestSupport(final O owner, final T payload) {
        super(TransactionId.generateDefault(), owner, Optional.of(payload));
    }

    public static  RequestSupport create(final O owner) {
        assertNotNull(owner);
        return new RequestSupport<>(owner);
    }

    public static  RequestSupport create(final O owner, final T payload) {
        assertNotNull(owner);
        assertNotNull(payload);
        return new RequestSupport<>(owner, payload);
    }

    public static  BuilderSupport of(final O owner, final T payload) {
        assertNotNull(owner);
        assertNotNull(payload);
        return new BuilderSupport<>(owner, Optional.of(payload));
    }

    public static  BuilderSupport of(final O owner) {
        assertNotNull(owner);
        return new BuilderSupport<>(owner, Optional.empty());
    }

    @Override
    public Response.Builder buildResponse() {
        return new ResponseSupport.BuilderSupport<>(getTransactionId(), getOwner(), Optional.empty());
    }

    @Override
    public  Response.Builder buildResponse(final T1 payload) {
        assertNotNull(payload);
        return new ResponseSupport.BuilderSupport<>(getTransactionId(), getOwner(), Optional.of(payload));
    }

    @Override
    public Response createResponse() {
        return new ResponseSupport.BuilderSupport<>(getTransactionId(), getOwner(), Optional.empty()).build();
    }

    public static class BuilderSupport implements Request.Builder {

        private final O owner;
        private final Optional payload;
        private TransactionId transactionId;

        protected BuilderSupport(final O owner, final Optional payload) {
            this.owner = owner;
            this.payload = payload;
        }


        @Override
        public Builder withTransactionId(final TransactionId transactionId) {
            assertNotNull(transactionId);
            this.transactionId = transactionId;
            return this;
        }

        @Override
        public Request build() {
            final TransactionId id = transactionId == null ? TransactionId.generateDefault() : transactionId;
            return internalBuild(id, owner, payload);
        }

        protected Request internalBuild(final TransactionId id, final O owner, final Optional payload) {
            return new RequestSupport<>(id, owner, payload);
        }
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy