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

net.sf.eBusx.monitor.MonitorId 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 2011, 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 net.sf.eBus.messages.EField;

/**
 * A passive, immutable class for storing a monitored object's
 * type name, instance name and its assigned unique, 4-byte
 * integer identifier. The {@link #toString()} return value
 * serves as the subject for the {@link Monitorable} instance
 * {@link PersistentStatusMessage} and
 * {@link TransientStatusMessage} notifications.
 *
 * @author Charles Rapp
 */

public final class MonitorId
    extends EField
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

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

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

    /**
     * The monitored object's type.
     */
    public final String typeName;

    /**
     * The monitored object's name. Should be unique within the
     * type and process.
     */
    public final String instanceName;

    /**
     * The object's assigned integer identifier.
     */
    public final int id;

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

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

    /**
     * Creates a new {@code MonitorId} instance.
     * @param typeName the object type name.
     * @param instanceName the object name. Should be unique
     * within the process for the given type name.
     * @param id the assigned unique identifier. Unique within
     * the process.
     *
     * @deprecated use {@link Builder} to create a monitor ID
     * field instance.
     * @see #builder()
     */
    @Deprecated
    public MonitorId(final String typeName,
                     final String instanceName,
                     final int id)
    {
        this.typeName = typeName;
        this.instanceName = instanceName;
        this.id = id;
    } // end of MonitorId(String, String, int)

    /**
     * Creates a new monitor identifier instance based on the
     * builder settings.
     * @param builder monitor identifier builder.
     */
    private MonitorId(final Builder builder)
    {
        super (builder);

        this.typeName = builder.mTypeName;
        this.instanceName = builder.mInstanceName;
        this.id = builder.mId;
    } // end of MonitorId(Builder)

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

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

    /**
     * Returns {@code true} if {@code o} is a
     * non-{@code null MonitorId} instance with the same
     * integer identifier; otherwise, returns {@code false}.
     * @param o comparison object.
     * @return {@code true} if {@code o} equals
     * {@code this MonitorId} instance and {@code false}
     * otherwise.
     */
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (retcode == false && o instanceof MonitorId)
        {
            retcode = (id == ((MonitorId) o).id);
        }

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

    /**
     * Returns the unique, 4-byte, signed monitor identifier.
     * @return the unique, 4-byte, signed monitor identifier.
     */
    @Override
    public int hashCode()
    {
        return (id);
    } // end of hashCode()

    /**
     * Returns "<type name>.<instance name>". This
     * return values is the subject for a registered
     * {@link Monitorable} instance
     * {@link PersistentStatusMessage} and
     * {@link TransientStatusMessage} notifications.
     * @return the textual representation of this monitor
     * identifier.
     */
    @Override
    public String toString()
    {
        return (String.format("%s.%s", typeName, instanceName));
    } // end of toString()

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

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

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

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

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

        private String mTypeName;
        private String mInstanceName;
        private int mId;

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

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

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

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

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

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

        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mTypeName, "typeName")
                         .requireNotNull(mInstanceName,
                                         "instanceName"));
        } // end of validate(Validator)

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

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

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

            mTypeName = name;

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

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

            mInstanceName = name;

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

        public Builder id(final int id)
        {
            mId = id;

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy