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

net.sf.eBusx.monitor.MonitorId Maven / Gradle / Ivy

//
// Copyright 2011, 2013, 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.monitor;

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

/**
 * 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 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 && 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