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

net.sf.eBusx.monitor.TransientStatusMessage 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, 2016, 2019, 2020. Charles W. Rapp
// All Rights Reserved.
//

package net.sf.eBusx.monitor;

import com.google.common.base.Strings;
import java.io.Serializable;
import java.util.Formatter;
import net.sf.eBus.messages.ENotificationMessage;

/**
 * Reports a monitored object's transient event. This event is
 * immediate and not on-going. The notification subject is the
 * {@link MonitorId#typeName} and
 * {@link MonitorId#instanceName} in the format
 * "<type name>.<instance name>". This means that
 * in order to subscribe to this notification, you must first
 * request the registered monitored objects using the
 * {@link MonitoredObjectRequest} message.
 *
 * @author Charles Rapp
 */

public final class TransientStatusMessage
    extends ENotificationMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

    /**
     * The monitored object's 4-byte integer identifier.
     */
    public final int instance;

    /**
     * The instance on-going action level.
     */
    public final ActionLevel actionLevel;

    /**
     * The on-going action name.
     */
    public final String actionName;

    /**
     * Option message explaining the action.
     */
    public final String actionMessage;

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

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

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

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

    /**
     * Creates a new instance of TransientStatusMessage.
     * @param subject the
     * {@link MonitorId#toString() monitor identifier}.
     * @param inst the monitored instance integer identifier.
     * @param level the action level.
     * @param name the action name.
     * @param message the action message.
     */
    @Deprecated
    public TransientStatusMessage(final String subject,
                                  final int inst,
                                  final ActionLevel level,
                                  final String name,
                                  final String message)
    {
        this (subject,
              System.currentTimeMillis(),
              inst,
              level,
              name,
              message);
    } // end of TransientStatusMessage(...)

    /**
     * The deserialization constructor.
     * @param subject the
     * {@link MonitorId#toString() monitor identifier}.
     * @param timestamp the message timestamp.
     * @param inst the monitored instance integer identifier.
     * @param level the action level.
     * @param name the action name.
     * @param message the action message.
     */
    @Deprecated
    public TransientStatusMessage(final String subject,
                                  final long timestamp,
                                  final int inst,
                                  final ActionLevel level,
                                  final String name,
                                  final String message)
    {
        super (subject, timestamp);

        instance = inst;
        actionLevel = level;
        actionName = name;
        actionMessage = message;
    } // end of TransientStatusMessage(...)

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

        this.instance = builder.mInstanceId;
        this.actionLevel = builder.mActionLevel;
        this.actionName = builder.mActionName;
        this.actionMessage = builder.mActionMessage;
    } // end of TransientStatusMessage(Builder)

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

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

    /**
     * Returns the application information message as text.
     * @return textual representation of application information.
     */
    @Override
    public String toString()
    {
        final Formatter retval = new Formatter();

        retval.format("%s%n               instance: %s%n",
                      super.toString(),
                      instance);
        retval.format("           action level: %s%n",
                      actionLevel);
        retval.format("            action name: %s%n",
                      actionName);
        retval.format("         action message: %s",
                      actionMessage);

        return (retval.toString());
    } // end of toString()

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

    public static Builder builder()
    {
        return (new Builder());
    } // end of builder()

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

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

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

        private int mInstanceId;
        private ActionLevel mActionLevel;
        private String mActionName;
        private String mActionMessage;

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

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

        private Builder()
        {
            super (TransientStatusMessage.class);
        } // end of Builder()

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

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

        @Override
        public TransientStatusMessage buildImpl()
        {
            return (new TransientStatusMessage(this));
        } // end of buildImpl()

        @Override
        public Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mActionLevel,
                                         "actionLevel")
                         .requireNotNull(mActionName,
                                         "actionName"));
        } // end of validate(Validator)

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

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

        public Builder instance(final int id)
        {
            mInstanceId = id;

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

        public Builder actionLevel(final ActionLevel level)
        {
            if (level == null)
            {
                throw (new NullPointerException("level is null"));
            }

            mActionLevel = level;

            return (this);
        } // end of actionLevel(ActionLevel)

        public Builder actionName(final String name)
        {
            if (Strings.isNullOrEmpty(name))
            {
                throw (
                    new IllegalArgumentException(
                        "name is null or empty"));
            }

            mActionName = name;

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

        public Builder actionMessage(final String message)
        {
            mActionMessage = message;

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy