net.sf.eBusx.monitor.ApplicationInfo Maven / Gradle / Ivy
//
// 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.eBusx.monitor;
import com.google.common.base.Strings;
import java.io.Serializable;
import java.util.Formatter;
import net.sf.eBus.messages.EField;
import net.sf.eBus.messages.ENotificationMessage;
import net.sf.eBus.util.Validator;
/**
* Reports the application information. This includes the
* application name, version, copyright, description and
* user-defined attributes. These attributes are defined by
* creating an {@link EField} sub-class. See the
* {@code net.sf.eBus.messages} package documentation for more
* information about sub-classing {@code EField}.
*
* In order to receive this message, subscribe to:
* {@code net.sf.eBusx.monitor.ApplicationInfo}:{@link Monitor#MONITOR_UPDATE_SUBJECT}.
*
* @see MonitorUpdate
* @see Monitor
*
* @author Charles Rapp
*/
public final class ApplicationInfo
extends ENotificationMessage
implements Serializable
{
//---------------------------------------------------------------
// Member data.
//
//-----------------------------------------------------------
// Constants.
//
/**
* Serialization version identifier.
*/
private static final long serialVersionUID = 0x050200L;
//-----------------------------------------------------------
// Locals.
//
/**
* The application name.
*/
public final String appName;
/**
* The application version.
*/
public final String appVersion;
/**
* The application copyright information.
*/
public final String copyright;
/**
* Optional human-readable description of this application.
*/
public final String description;
/**
* The application-specific attributes.
*/
public final EField attributes;
//---------------------------------------------------------------
// Member methods.
//
//-----------------------------------------------------------
// Constructors.
//
private ApplicationInfo(final Builder builder)
{
super (builder);
this.appName = builder.mAppName;
this.appVersion = builder.mAppVersion;
this.copyright = builder.mCopyright;
this.description = builder.mDescription;
this.attributes = builder.mAttributes;
} // end of ApplicationInfo(Builder)
//
// end of Constructors.
//-----------------------------------------------------------
//-----------------------------------------------------------
// Object Method Overrides.
//
/**
* Returns the application information message as text.
* @return textual representation of application information.
*/
@SuppressWarnings({"java:S3457"})
@Override
public String toString()
{
final String retval;
try (Formatter output = new Formatter())
{
output.format("%s%n app name: %s%n",
super.toString(),
appName);
output.format(" app version: %s%n",
appVersion);
output.format(" copyright: %s%n",
copyright);
output.format(" description: %s%n",
description);
output.format(" attributes: ");
if (attributes == null)
{
output.format(" (none)");
}
else
{
output.format("%n%s", attributes);
}
retval = output.toString();
}
return (retval);
} // end of toString()
//
// end of Object Method Overrides.
//-----------------------------------------------------------
/**
* Returns the {@code ApplicationInfo} message builder.
* @return message builder.
*/
public static Builder builder()
{
return (new Builder());
} // end of builder()
//---------------------------------------------------------------
// Inner classes.
//
/**
* Builder class used to construct an {@link ApplicationInfo}
* instance. A {@code Builder} instance is accessed via the
* {@link ApplicationInfo#builder()} method.
*/
public static final class Builder
extends ENotificationMessage.Builder
{
//-----------------------------------------------------------
// Member data.
//
//-------------------------------------------------------
// Locals.
//
private String mAppName;
private String mAppVersion;
private String mCopyright;
private String mDescription;
private EField mAttributes;
//-----------------------------------------------------------
// Member methods.
//
//-------------------------------------------------------
// Constructors.
//
private Builder()
{
super (ApplicationInfo.class);
this.subject(Monitor.MONITOR_UPDATE_SUBJECT);
} // end of Builder()
//
// end of Constructors.
//-------------------------------------------------------
//-------------------------------------------------------
// Builder Method Overrides.
//
/**
* Returns an {@code ApplicationInfo} instance based on
* this builder's configuration. This method is called
* only after the builder configuration is successfully
* validated.
* @return new {@code ApplicationInfo} instance.
*/
@Override
protected ApplicationInfo buildImpl()
{
return (new ApplicationInfo(this));
} // end of buildImpl()
/**
* Validates the builder configuration. A valid
* configuration requires that the application name and
* version be set.
* @param problems add configuration problems to
* @return {@code problems} to allow for
* {@code Validator} method chaining.
*/
@Override
protected Validator validate(final Validator problems)
{
return (super.validate(problems)
.requireNotNull(mAppName, "appName")
.requireNotNull(mAppVersion, "appVersion"));
} // end of validate(Validator)
//
// end of Builder Method Overrides.
//-------------------------------------------------------
//-------------------------------------------------------
// Set Methods.
//
public Builder appName(final String name)
{
if (Strings.isNullOrEmpty(name))
{
throw (
new IllegalArgumentException(
"name is null or empty"));
}
mAppName = name;
return (this);
} // end of appName(String)
public Builder appVersion(final String version)
{
if (Strings.isNullOrEmpty(version))
{
throw (
new IllegalArgumentException(
"version is null or empty"));
}
mAppVersion = version;
return (this);
} // end of appVersion(String)
public Builder copyright(final String copyright)
{
mCopyright = copyright;
return (this);
} // end of copyright(String)
public Builder description(final String description)
{
mDescription = description;
return (this);
} // end of description(String)
public Builder attributes(final EField attributes)
{
mAttributes = attributes;
return (this);
} // end of attributes(EFoield)
//
// end of Set Methods.
//-------------------------------------------------------
} // end of class Builder
} // end of class ApplicationInfo