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

net.sf.eBus.client.sysmessages.KeyMessage 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 2016. Charles W. Rapp
// All Rights Reserved.
//

package net.sf.eBus.client.sysmessages;

import java.io.Serializable;
import net.sf.eBus.messages.EFieldInfo;
import net.sf.eBus.messages.EMessageKey;
import net.sf.eBus.messages.ESystemMessage;

/**
 * This system message is used to exchange
 * message key-to-identifier mappings.
 *
 * @author Charles W. Rapp
 */

@EFieldInfo (fields = {"keyId", "keyClass", "keySubject"})
public final class KeyMessage
    extends ESystemMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member methods.
//

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

    /**
     * Creates a new outbound key update message for the given
     * message key and identifier. {@code msgKey} must have its
     * identifier set.
     * @param keyId unique message key identifier.
     * @param msgKey message key.
     * @throws IllegalArgumentException
     * if {@code msgKey} is {@code null}.
     *
     * @see #KeyMessage(String, long, int, String, String)
     */
    public KeyMessage(final int keyId,
                      final EMessageKey msgKey)
        throws IllegalArgumentException
    {
        super ();

        if (msgKey == null)
        {
            throw (
                new IllegalArgumentException("msgKey is null"));
        }

        this.keyId = keyId;
        this.keyClass = msgKey.name();
        this.keySubject = msgKey.subject();
    } // end of KeyMessage(int, EMessageKey)

    /**
     * Creates a new inbound key message for the given
     * de-serialized parameters.
     * @param subject {@link ESystemMessage#SYSTEM_SUBJECT}.
     * @param timestamp message timestamp.
     * @param keyId unique message key identifier.
     * @param keyClass message key class name.
     * @param keySubject message key subject.
     * @throws IllegalArgumentException
     * if {@code subject} or {@code keyClass} is either
     * {@code null} or empty.
     *
     * @see #KeyMessage(int, EMessageKey)
     */
    public KeyMessage(final String subject,
                      final long timestamp,
                      final int keyId,
                      final String keyClass,
                      final String keySubject)
        throws IllegalArgumentException
    {
        super (subject, timestamp);

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

        this.keyId = keyId;
        this.keyClass = keyClass;
        this.keySubject = keySubject;
    } // end of KeyMessage(String, long, int, String, String)

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

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

    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (retcode == false &&
            o instanceof KeyMessage)
        {
            retcode = (super.equals(o) == true &&
                       keyId == ((KeyMessage) o).keyId);
        }

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

    @Override
    public int hashCode()
    {
        return ((super.hashCode() * 37) + keyId);
    } // end of hashCode()

    @Override
    public String toString()
    {
        return (
            String.format("%s%n                key ID: %d%n             class name: %s%n                subject: %s",
                super.toString(),
                keyId,
                keyClass,
                keySubject));
    } // end of toString()

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

//---------------------------------------------------------------
// Member data.
//

    /**
     * The sender's message key identifier. The receiver may
     * assign its own unique identifier for the key.
     */
    public final int keyId;

    /**
     * The message key class name.
     */
    public final String keyClass;

    /**
     * The message key subject.
     */
    public final String keySubject;

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

    /**
     * Serialization version identifier.
     */
    private static final long serialVersionUID = 0x060100L;
} // end of class KeyMessage




© 2015 - 2025 Weber Informatics LLC | Privacy Policy