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

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

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

import java.io.Serializable;
import javax.annotation.Nullable;
import net.sf.eBus.messages.EReplyMessage.ReplyStatus;
import net.sf.eBus.util.Validator;

/**
 * This message is sent in reply to a logon message. The subject
 * contains the eBus identifier received in the
 * {@link LogonMessage}.
 *
 * @see AbstractLogonMessage
 * @see LogonMessage
 * @see LogoffMessage
 *
 * @author Charles Rapp
 */

public final class LogonReply
    extends AbstractLogonMessage
    implements Serializable
{
//---------------------------------------------------------------
// Member data.
//

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

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

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

    /**
     * Either the logon attempt succeeded or failed.
     */
    public final ReplyStatus logonStatus;

    /**
     * Explanation for a rejected logon request. May be
     * {@code null} or empty.
     */
    @Nullable
    public final String reason;

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

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

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

        this.logonStatus = builder.mLogonStatus;
        this.reason = builder.mReason;
    } // end of LogonReply(Builder)

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

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

    /**
     * Returns {@code true} if {@code o} is a
     * non-{@code null LogonReply} instance with reply status and
     * reason equal to {@code this LogonReply} instance and
     * {@code false} otherwise.
     * @param o comparison object.
     * @return {@code true} if the message fields are equal
     * and {@code false} otherwise.
     */
    @Override
    public boolean equals(final Object o)
    {
        boolean retcode = (this == o);

        if (!retcode && o instanceof LogonReply)
        {
            final LogonReply lr = (LogonReply) o;

            retcode = (super.equals(o) &&
                       logonStatus == lr.logonStatus &&
                       (reason == null ?
                            lr.reason == null :
                            reason.equals(lr.reason)));
        }

        return (retcode);
    } // endof equals(Object)

    /**
     * Returns the feed status message hash code.
     * @return the feed status message hash code.
     */
    @Override
    public int hashCode()
    {
        return (
            (((super.hashCode() * 37) +
              logonStatus.ordinal()) * 37) +
            (reason == null ? 0 : reason.hashCode()));
    } // end of hashCode()

    /**
     * Returns a human-readable text version of this message.
     * @return text version of this message.
     */
    @Override
    public String toString()
    {
        return (
            String.format(
                "%s%n           logon status: %s%n                 reason: %s",
                super.toString(),
                logonStatus,
                (reason == null ? "(none)" : reason)));
    } // end of toString()

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

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

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

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

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

        /**
         * Required reply status field specifying whether the
         * logon request was accepted or rejected.
         */
        private ReplyStatus mLogonStatus;

        /**
         * Optional field explaining why logon request is
         * rejected.
         */
        private String mReason;

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

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

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

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

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

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

        /**
         * Checks if logon status is set. If not then failure is
         * added to the problems list.
         * @param problems used to check field validity and
         * collect discovered invalid fields.
         * @return {@code problems} to allow for method chaining.
         */
        @Override
        protected Validator validate(final Validator problems)
        {
            return (super.validate(problems)
                         .requireNotNull(mLogonStatus,
                                         "logon status"));
        } // end of validate(Validator)

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

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

        /**
         * Sets the logon status to the given value.
         * @param status logon status.
         * @return {@code this} logon reply builder.
         * @throws NullPointerException
         * if {@code status} is {@code null}.
         */
        public Builder logonStatus(final ReplyStatus status)
        {
            if (status == null)
            {
                throw (
                    new NullPointerException("status is null"));
            }

            mLogonStatus = status;

            return (this);
        } // end of logonStatus(final ReplyStatus status)

        /**
         * Sets the logon status reason.
         * @param t reason for logon status. May be a
         * {@code null} or empty string.
         * @return {@code this} logon reply builder.
         */
        public Builder reason(final String t)
        {
            mReason = t;

            return (this);
        } // end of reason(final String t)

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy