net.sf.eBus.client.sysmessages.SubscribeMessage Maven / Gradle / Ivy
The newest version!
//
// Copyright 2012, 2013, 2015, 2016, 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 net.sf.eBus.client.EFeedState;
import net.sf.eBus.util.Validator;
/**
* This message is sent to a remote publisher to have a
* notification feed either started or stopped.
*
* @author Charles Rapp
*/
public final class SubscribeMessage
extends AbstractKeyMessage
implements Serializable
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x050200L;
//-----------------------------------------------------------
// Locals.
//
/**
* The subscription is either {@code EFeedState.UP} or
* {@code EFeedState.DOWN}.
*/
public final EFeedState feedState;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
private SubscribeMessage(final Builder builder)
{
super (builder);
this.feedState = builder.mFeedState;
} // end of SubscribeMessage(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns {@code true} if {@code o} is a
* non-{@code null SubscribeMessage} instance with the
* same up flag as {@code this SubscribeMessage} 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 SubscribeMessage)
{
final SubscribeMessage psm =
(SubscribeMessage) o;
retcode = (super.equals(o) &&
feedState == psm.feedState);
}
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 ((super.hashCode() * 37) + feedState.ordinal());
} // 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 feed state: %s",
super.toString(),
feedState));
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
/**
* Returns a new instance of the {@code SubscribeMessage}
* builder.
* @return message builder instance.
*/
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Class used to create {@link SubscribeMessage} instances.
* Used by eBus to de-serialize an encoded message.
*/
public static final class Builder
extends AbstractKeyMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
private EFeedState mFeedState;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (SubscribeMessage.class);
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Returns the newly instantiated
* {@code SubscribeMessage} based on this builder
* configuration.
* @return target message instance.
*/
@Override
protected SubscribeMessage buildImpl()
{
return (new SubscribeMessage(this));
} // end of buildImpl()
/**
* Checks if the feed state is configured. If not, then
* appends this problem to the validator.
* @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(mFeedState,
"feed state"));
} // end of validate(Validator)
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
/**
* Sets the subscription feed state to the given value.
* @param fs subscription feed state.
* @return {@code this} builder.
* @throws NullPointerException
* if {@code fs} is {@code null}.
*/
public Builder feedState(final EFeedState fs)
{
if (fs == null)
{
throw (new NullPointerException("fs is null"));
}
mFeedState = fs;
return (this);
} // end of feedState(EFeedState)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class SubscribeMessage