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

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

//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later
// version.
//
// This library is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General
// Public License along with this library; if not, write to the
//
// Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330,
// Boston, MA
// 02111-1307 USA
//
// The Initial Developer of the Original Code is Charles W. Rapp.
// Portions created by Charles W. Rapp are
// Copyright 2012. 2013, 2015, 2016, 2019, 2020. Charles W. Rapp
// All Rights Reserved.
//

package net.sf.eBusx.util;

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

/**
 * 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 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 instance for the given timer
     * name and sequence number.
     * @param timerName the expired timer name.
     * @param status either {@link ReplyStatus#OK_FINAL} if this
     * is a one-time timer or {@link ReplyStatus#OK_CONTINUING}
     * if this is an on-going timer.
     * @param seqNum the timer sequence number.
     * @throws IllegalArgumentException
     * if {@code timerName} is either {@code null} or empty.
     *
     * @deprecated use {@link Builder} to create timer reply
     * messages.
     */
    @Deprecated
    public TimerReply(final String timerName,
                      final ReplyStatus status,
                      final int seqNum)
        throws IllegalArgumentException
    {
        this (ETimer.TIMER_SUBJECT,
              System.currentTimeMillis(),
              status,
              null,
              timerName,
              seqNum);
    } // end of TimerReply(String, ReplyStatus, int)

    /**
     * The de-serialization constructor.
     * @param subject the message subject.
     * @param timestamp the message timestamp.
     * @param status the reply status.
     * @param reason the reply reason.
     * @param timerName the expired timer name.
     * @param seqNum the timer sequence number.
     * @throws IllegalArgumentException
     * if {@code timerName} is either {@code null} or empty.
     *
     * @deprecated use {@link Builder} to create timer reply
     * messages.
     */
    @Deprecated
    public TimerReply(final String subject,
                      final long timestamp,
                      final ReplyStatus status,
                      final String reason,
                      final String timerName,
                      final int seqNum)
        throws IllegalArgumentException
    {
        super (subject,
               timestamp,
               status,
               reason);

        if (timerName == null || timerName.isEmpty() == true)
        {
            throw (
                new IllegalArgumentException(
                    "timerName is null or empty"));
        }

        this.timerName = timerName;
        this.sequenceNumber = seqNum;
    } // end of TimerReply(...)

    /**
     * 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.
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (retcode == false &&
            o instanceof TimerReply)
        {
            final TimerReply reply = (TimerReply) o;

            retcode =
                (super.equals(o) == true &&
                 timerName.equals(reply.timerName) == true &&
                 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