net.sf.eBus.client.sysmessages.AbstractLogonMessage Maven / Gradle / Ivy
The newest version!
//
// Copyright 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 com.google.common.base.Strings;
import java.io.Serializable;
import java.util.Objects;
import net.sf.eBus.messages.EMessageObject;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.util.Validator;
/**
* Base class for the eBus logon messages {@link LogonMessage},
* {@link LogonReply}, and {@link LogoffMessage}. Contains the
* unique eBus identifier. For a Java application, this is the
* JVM identifier and for a .Net application, the process
* identifier.
*
* @author Charles Rapp
*/
public abstract class AbstractLogonMessage
extends ESystemMessage
implements Serializable
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x050200L;
//-----------------------------------------------------------
// Locals.
//
/**
* The eBus identifier.
*/
public final String eid;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates a new abstract logon message from the configured
* message builder.
* @param builder message builder.
*/
protected AbstractLogonMessage(final Builder, ?> builder)
{
super (builder);
eid = builder.mEid;
} // end of AbstractLogonMessage(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns {@code true} if {@code o} is a
* non-{@code null AbstractLogonMessage} instance with an
* eBus identifier equal to
* {@code this AbstractLogonMessage} 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 AbstractLogonMessage)
{
final AbstractLogonMessage alm =
(AbstractLogonMessage) o;
retcode = (super.equals(o) && eid.equals(alm.eid));
}
return (retcode);
} // end of equals(Object)
/**
* Returns the feed status message hash code.
* @return the feed status message hash code.
*/
@Override
public int hashCode()
{
return (Objects.hash(super.hashCode(), eid));
} // 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 eBus ID: %s",
super.toString(),
eid));
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
//---------------------------------------------------------------
// Inner classes.
//
/**
* Base class for all {@link AbstractLogonMessage} builders.
* Provides eBus identifier validation.
*
* @param builds this target message class.
* @param message builder subclass. Needed to return the
* correct builder type when setting fields. If this were not
* the case, field chaining would not work.
*/
public abstract static class Builder>
extends ESystemMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
/**
* eBus identifier.
*/
private String mEid;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
/**
* Creates a logon message builder for the given target
* message class.
* @param targetClass builder for this target message
* class.
*/
protected Builder(final Class extends EMessageObject> targetClass)
{
super (targetClass);
} // end of Builder(Class)
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Checks if the eBus identifier is configured. If not,
* then appends those problems to the 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(mEid, "eid"));
} // end of validate(Validator)
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
/**
* Sets the eBus identifier to the given value.
* @param eid eBus identifier.
* @return {@code this} message builder.
* @throws IllegalArgumentException
* if {@code eid} is {@code null} or empty.
*/
@SuppressWarnings ("unchecked")
public B eid(final String eid)
{
if (Strings.isNullOrEmpty(eid))
{
throw (
new IllegalArgumentException(
"eid is null or empty"));
}
mEid = eid;
return ((B) this);
} // end of eid(String)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class AbstractLogonMessage