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

net.sf.eBusx.util.TimerReply Maven / Gradle / Ivy

//
// Copyright 2012. 2013, 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.eBusx.util;

import com.google.common.base.Strings;
import java.io.Serializable;
import net.sf.eBus.messages.ELocalOnly;
import net.sf.eBus.messages.EReplyMessage;
import net.sf.eBus.messages.EReplyMessage.ReplyStatus;
import net.sf.eBus.util.Validator;

/**
 * This reply message is sent when the requested timer expires.
 * The reply contains the requestor-defined timer name and
 * sequence number. If this is a one-shot timer, then
 * {@link #replyStatus} is set to {@link ReplyStatus#OK_FINAL} and
 * {@link #sequenceNumber} the final reply flag is set zero.
 * Repeating timers always have a {@link #replyStatus} of
 * {@link ReplyStatus#OK_CONTINUING} since timer replies keep
 * coming until either the requestor
 * {@link net.sf.eBus.client.ERequestFeed.ERequest#close() closes}
 * the timer or {@link ETimer} is
 * {@link ETimer#stopETimer() shut down}.
 *
 * @author Charles W. Rapp
 */

@ELocalOnly
public final class TimerReply
    extends EReplyMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

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

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

    /**
     * The requestor-assigned name for this timer request.
     */
    public final String timerName;

    /**
     * The time message sequence number. Values begin at zero.
     * This means that a one-time timer has a zero sequence
     * number.
     */
    public final int sequenceNumber;

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

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

    /**
     * Creates a new timer reply based on the message builder
     * contents.
     * @param builder message builders.
     */
    private TimerReply(final Builder builder)
    {
        super (builder);

        this.timerName = builder.mTimerName;
        this.sequenceNumber = builder.mSequenceNumber;
    } // end of TimerReply(Builder)

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

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

    // Use default javadoc.
    @SuppressWarnings({"java:S2159"})
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = super.equals(o);

        if (!retcode)
        {
            // no-op: super class equals failed so there is no
            // point in checking the sub-class
        }
        else if (!(o instanceof TimerReply))
        {
            retcode = false;
        }
        else
        {
            final TimerReply reply = (TimerReply) o;

            retcode =
                (timerName.equals(reply.timerName) &&
                 sequenceNumber == reply.sequenceNumber);
        }

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

    // Use default javadoc.
    @Override
    public int hashCode()
    {
        return ((((super.hashCode() * 37) +
                  timerName.hashCode()) * 37) +
                sequenceNumber);
    } // end of hashCode()

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

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

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

    public static final class Builder
        extends EReplyMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

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

        private String mTimerName;
        private int mSequenceNumber;

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

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

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

            mSequenceNumber = Integer.MIN_VALUE;

            this.subject(ETimer.TIMER_SUBJECT);
        } // end of Builder()

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

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

        @Override
        protected TimerReply buildImpl()
        {
            return (new TimerReply(this));
        } // end of buildImpl()

        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mTimerName,
                                         "timerName")
                         .requireNotNull(mSequenceNumber,
                                         "sequenceNumber"));
        } // end of validate(Validator)

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

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

        /**
         * Sets the timer name.
         * @param name timer name.
         * @return {@code this TimerReply} builder.
         * @throws IllegalArgumentException
         * if {@code name} is {@code null} or empty.
         */
        public Builder timerName(final String name)
        {
            if (Strings.isNullOrEmpty(name))
            {
                throw (
                    new IllegalArgumentException(
                        "name is null or empty"));
            }

            mTimerName = name;

            return (this);
        } // end of timerName(String)

        /**
         * Sets the sequence number.
         * @param n sequence number.
         * @return {@code this TimerReply} builder.
         * @throws IllegalArgumentException
         * if {@code n} is < zero.
         */
        public Builder sequenceNumber(final int n)
        {
            if (n < 0)
            {
                throw (new IllegalArgumentException("n < zero"));
            }

            mSequenceNumber = n;

            return (this);
        } // end of sequenceNumber(int)

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy