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

net.sf.eBus.client.sysmessages.McastKeyMessage Maven / Gradle / Ivy

The newest version!
//
// Copyright 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.eBus.client.sysmessages;

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;
import net.sf.eBus.client.EFeedState;
import net.sf.eBus.util.Validator;

/**
 * {@code EMulticastPublisher}s announce the mapping between a
 * message key and its publisher-assigned unique identifier. This
 * identifier is used an encode eBus message header rather than
 * the message class and subject, allowing the header to be of
 * fixed size. This message is also used to report a change in
 * an existing message key's feed status.
 *
 * @author Charles W. Rapp
 */

public final class McastKeyMessage
    extends AbstractKeyMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

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

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

    /**
     * Publisher's multicast identifier.
     */
    public final UUID multicastId;

    /**
     * Publisher's message key identifier. Note that multicast
     * messages are sent only from the multicast publisher to
     * multicast subscriber.
     */
    public final int keyId;

    /**
     * The notification feed's current state.
     */
    public final EFeedState feedState;

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

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

    /**
     * Creates a new multicast key message from the given
     * builder's configuration.
     * @param builder contains message configuration.
     */
    private McastKeyMessage(final Builder builder)
    {
        super (builder);

        this.multicastId = builder.mMulticastId;
        this.keyId = builder.mKeyId;
        this.feedState = builder.mFeedState;
    } // end of McastKeyMessage(Builder)

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

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

    @SuppressWarnings({"java:S2159"})
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof McastKeyMessage)
        {
            final McastKeyMessage keyMsg = (McastKeyMessage) o;

            retcode = (super.equals(o) &&
                       Objects.equals(multicastId, keyMsg.multicastId) &&
                       keyId == keyMsg.keyId);
        }

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

    @Override
    public int hashCode()
    {
        return (Objects.hash(super.hashCode(), multicastId, keyId));
    } // end of hashCode()

    @Override
    public String toString()
    {
        return (String.format("%s%n                 JVM ID: %s%n                 key ID: %d%n             feed state: %s",
                              super.toString(),
                              multicastId,
                              keyId,
                              feedState));
    } // end of toString()

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

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

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

    /**
     * Class used to create {@link McastKeyMessage} instances.
     * Used by eBus to de-serialize an encoded message.
     */
    public static final class Builder
        extends AbstractKeyMessage.Builder
    {
    //-----------------------------------------------------------
    // Member data.
    //

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

        private UUID mMulticastId;
        private int mKeyId;
        private EFeedState mFeedState;

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

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

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

            mKeyId = -1;
        } // end of Builder()

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

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

        /**
         * Returns the newly instantiated {@code McastKeyMessage}
         * based on {@code this} builder configuration.
         * @return target message instance.
         */
        @Override
        protected McastKeyMessage buildImpl()
        {
            return (new McastKeyMessage(this));
        } // end of buildImpl()

        /**
         * Checks if the message key identifier and feed state
         * are configured. If not then appends those problems to
         * the validator list.
         * @param problems contains problems found with the
         * message configuration.
         * @return {@code problems} allowing for method chaining.
         */
        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mMulticastId,
                                         Validator.NOT_SET)
                         .requireTrue((mKeyId >= 0),
                                      "keyId",
                                      Validator.NOT_SET)
                         .requireNotNull(mFeedState,
                                         Validator.NOT_SET));
        } // end of validate(Validator)

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

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

        /**
         * Sets multicast identifier to the given value.
         * @param id multicast identifier.
         * @return {@code this} key message builder.
         * @throws IllegalArgumentException
         * if {@code id} is {@code null}.
         */
        public Builder multicastId(final UUID id)
        {
            mMulticastId =
                Objects.requireNonNull(id, "id is null");

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

        /**
         * Sets the message key identifier to the given value.
         * @param id message key identifier.
         * @return {@code this} key message builder.
         * @throws IllegalArgumentException
         * if {@code id} < zero.
         */
        public Builder keyId(final int id)
        {
            if (id < 0)
            {
                throw (
                    new IllegalArgumentException("id < zero"));
            }

            mKeyId = id;

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

        /**
         * Sets the multicast feed state to the given value.
         * @param fs multicast feed state. May be
         * {@code null}.
         * @return {@code this} builder.
         * @throws NullPointerException
         * if {@code fs} is {@code null}.
         */
        public Builder feedState(final EFeedState fs)
        {
            mFeedState =
                Objects.requireNonNull(fs, "feedState is null");

            return (this);
        } // end of feedState(EFeedState)

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy