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

net.sf.eBus.client.sysmessages.RemoteAck Maven / Gradle / Ivy

//
// Copyright 2015, 2016, 2019, 2020 Charles W. Rapp
//
// 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.
//

package net.sf.eBus.client.sysmessages;

import java.io.Serializable;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.util.Validator;

/**
 * Acknowledges receipt of a request message from a remote eBus
 * application. This message is used to link the two feed
 * identifiers associated with the request. This allows a request
 * to be canceled before a reply is sent. This message has no
 * fields since no information beyond the header is required.
 *
 * @author Charles W. Rapp
 */

public final class RemoteAck
    extends ESystemMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

    //-----------------------------------------------------------
    // Constants.
    //

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x050200L;

    //-----------------------------------------------------------
    // Locals.
    //

    /**
     * The number of remaining remote repliers. When this value
     * reaches zero, the request is completed.
     */
    public final int remaining;

//---------------------------------------------------------------
// Member methods.
//

    //-----------------------------------------------------------
    // Constructors.
    //

    private RemoteAck(final Builder builder)
    {
        super (builder);

        this.remaining = builder.mRemaining;
    } // end of RemoteAck(Builder)

    //
    // end of Constructors.
    //-----------------------------------------------------------

    //-----------------------------------------------------------
    // Object Method Overrides.
    //

    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof RemoteAck)
        {
            retcode = (super.equals(o) &&
                       remaining == ((RemoteAck) o).remaining);
        }

        return (retcode);
    } // end of equals(Object)

    @Override
    public int hashCode()
    {
        return ((super.hashCode() * 37) + remaining);
    } // end of hashCode()

    /**
     * Returns a human-readable text version of this message.
     * @return text version of this message.
     */
    @Override
    public String toString()
    {
        return (String.format("%s%n              remaining: %d",
                              super.toString(),
                              remaining));
    } // end of toString()

    //
    // end of Object Method Overrides.
    //-----------------------------------------------------------

    /**
     * Returns a new instance of the {@code RemoteAck} message
     * builder.
     * @return message builder instance.
     */
    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

//---------------------------------------------------------------
// Inner classes.
//

    /**
     * Class used to create {@link RemoteAck} messages. Used
     * by eBus to de-serialize an encoded message.
     */
    public static final class Builder
        extends ESystemMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

        //-------------------------------------------------------
        // Locals.
        //

        private int mRemaining;

    //-----------------------------------------------------------
    // Member methods.
    //

        //-------------------------------------------------------
        // Constructors.
        //

        private Builder()
        {
            super (RemoteAck.class);

            mRemaining = -1;
        } // end of Builder()

        //
        // end of Constructors.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Builder Method Overrides.
        //

        /**
         * Returns the newly instantiated {@code RemoteAck}
         * based on this builder configuration.
         * @return target message instance.
         */
        @Override
        protected RemoteAck buildImpl()
        {
            return (new RemoteAck(this));
        } // end of buildImpl()

        /**
         * Adds any and all configuration problems to the list.
         * @param problems used to check field validity and
         * collect discovered invalid fields.
         * @return {@code problems} to allow for method chaining.
         */
        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireTrue(v -> (mRemaining >= 0),
                                      mRemaining,
                                      "remaining",
                                      Validator.NOT_SET));
        } // end of validate(Validator)

        //
        // end of Builder Method Overrides.
        //-------------------------------------------------------

        //-------------------------------------------------------
        // Set Methods.
        //

        /**
         * Sets the number of remaining repliers to the given
         * value.
         * @param n remaining repliers count.
         * @return {@code this RemoteAck} message builder.
         * @throws IllegalArgumentException
         * if {@code n} < zero.
         */
        public Builder remaining(final int n)
        {
            if (n < 0)
            {
                throw (new IllegalArgumentException("n < zero"));
            }

            mRemaining = n;

            return (this);
        } // end of remaining(final int n)

        //
        // end of Set Methods.
        //-------------------------------------------------------
    } // end of class Builder
} // end of class RemoteAck




© 2015 - 2025 Weber Informatics LLC | Privacy Policy