net.sf.eBus.client.AbstractEBusMessage Maven / Gradle / Ivy
//
// Copyright 2014 - 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;
import java.io.Serializable;
import java.net.InetSocketAddress;
import net.sf.eBus.config.EConfigure.ConnectionType;
import net.sf.eBus.messages.EMessageObject;
import net.sf.eBus.messages.ENotificationMessage;
import net.sf.eBus.messages.ESystemMessage;
import net.sf.eBus.util.Validator;
/**
* Base class for eBus {@link ConnectionMessage} and
* {@link ServerMessage} classes. Defines the common notification
* subject used for all eBus client messages.
*
Contains the remote TCP address and service port used by
subclasses.
*
* @see ConnectionMessage
* @see ServerMessage
*
* @author Charles Rapp
*/
public abstract class AbstractEBusMessage
extends ENotificationMessage
implements Serializable
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x050200L;
//-----------------------------------------------------------
// Locals.
//
/**
* Connection type.
*/
public final ConnectionType connectionType;
/**
* The client address referenced in this message.
*/
public final InetSocketAddress remoteAddress;
/**
* IP port number.
*/
public final InetSocketAddress serverAddress;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
/**
* Creates an eBus message based on the given builder
* configuration.
* @param builder contains message field settings.
*/
protected AbstractEBusMessage(final Builder, ?> builder)
{
super (builder);
this.connectionType = builder.mConnectionType;
this.remoteAddress = builder.mRemoteAddress;
this.serverAddress = builder.mServerAddress;
} // end of AbstractEBusMessage(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns the message as text.
* @return textual representation of the message.
*/
@Override
public String toString()
{
return (
String.format("%s%n connection type: %s%n address: %s%n port: %d%n server address: %s",
super.toString(),
connectionType,
remoteAddress.getHostName(),
remoteAddress.getPort(),
serverAddress));
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
//---------------------------------------------------------------
// Inner classes.
//
/**
* Base class builder for {@code AbstractEBusMessage}
* builders.
*
* @param target message class.
* @param Builder subclass.
*/
public abstract static class Builder>
extends ENotificationMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
private ConnectionType mConnectionType;
private InetSocketAddress mRemoteAddress;
private InetSocketAddress mServerAddress;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
protected Builder(final Class extends EMessageObject> targetClass)
{
super (targetClass, ESystemMessage.SYSTEM_SUBJECT);
} // end of Builder(Class)
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Checks if remote address and server port are set.
* @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(mConnectionType,
"connection type")
.requireNotNull(mRemoteAddress,
"remote address"));
} // end of validate(List<>)
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
@SuppressWarnings ("unchecked")
public final B connectionType(final ConnectionType connType)
{
mConnectionType = connType;
return ((B) this);
} // end of connectionType(ConnectionType)
@SuppressWarnings ("unchecked")
public final B remoteAddress(final InetSocketAddress address)
{
mRemoteAddress = address;
return ((B) this);
} // end of remoteAddress(InetSocketAddress)
@SuppressWarnings ("unchecked")
public final B serverAddress(final InetSocketAddress serverAddress)
{
mServerAddress = serverAddress;
return ((B) this);
} // end of serverAddress(InetSocketAddress)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class AbstractEBusMessage