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

com.io7m.waxmill.client.api.WXMApplicationVersion Maven / Gradle / Ivy

The newest version!
package com.io7m.waxmill.client.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * An application version.
 */
@SuppressWarnings({"all"})
public final class WXMApplicationVersion implements WXMApplicationVersionType {
  private final String applicationName;
  private final String applicationVersion;

  private WXMApplicationVersion(String applicationName, String applicationVersion) {
    this.applicationName = applicationName;
    this.applicationVersion = applicationVersion;
  }

  /**
   * @return The name of the application
   */
  @Override
  public String applicationName() {
    return applicationName;
  }

  /**
   * @return The version of the application
   */
  @Override
  public String applicationVersion() {
    return applicationVersion;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link WXMApplicationVersionType#applicationName() applicationName} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for applicationName
   * @return A modified copy of the {@code this} object
   */
  public final WXMApplicationVersion withApplicationName(String value) {
    String newValue = Objects.requireNonNull(value, "applicationName");
    if (this.applicationName.equals(newValue)) return this;
    return new WXMApplicationVersion(newValue, this.applicationVersion);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link WXMApplicationVersionType#applicationVersion() applicationVersion} attribute.
   * An equals check used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for applicationVersion
   * @return A modified copy of the {@code this} object
   */
  public final WXMApplicationVersion withApplicationVersion(String value) {
    String newValue = Objects.requireNonNull(value, "applicationVersion");
    if (this.applicationVersion.equals(newValue)) return this;
    return new WXMApplicationVersion(this.applicationName, newValue);
  }

  /**
   * This instance is equal to all instances of {@code WXMApplicationVersion} that have equal attribute values.
   * @return {@code true} if {@code this} is equal to {@code another} instance
   */
  @Override
  public boolean equals(Object another) {
    if (this == another) return true;
    return another instanceof WXMApplicationVersion
        && equalTo((WXMApplicationVersion) another);
  }

  private boolean equalTo(WXMApplicationVersion another) {
    return applicationName.equals(another.applicationName)
        && applicationVersion.equals(another.applicationVersion);
  }

  /**
   * Computes a hash code from attributes: {@code applicationName}, {@code applicationVersion}.
   * @return hashCode value
   */
  @Override
  public int hashCode() {
    int h = 5381;
    h += (h << 5) + applicationName.hashCode();
    h += (h << 5) + applicationVersion.hashCode();
    return h;
  }

  /**
   * Prints the immutable value {@code WXMApplicationVersion} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    return "WXMApplicationVersion{"
        + "applicationName=" + applicationName
        + ", applicationVersion=" + applicationVersion
        + "}";
  }

  /**
   * Creates an immutable copy of a {@link WXMApplicationVersionType} value.
   * Uses accessors to get values to initialize the new immutable instance.
   * If an instance is already immutable, it is returned as is.
   * @param instance The instance to copy
   * @return A copied immutable WXMApplicationVersion instance
   */
  public static WXMApplicationVersion copyOf(WXMApplicationVersionType instance) {
    if (instance instanceof WXMApplicationVersion) {
      return (WXMApplicationVersion) instance;
    }
    return WXMApplicationVersion.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link WXMApplicationVersion WXMApplicationVersion}.
   * 
   * WXMApplicationVersion.builder()
   *    .setApplicationName(String) // required {@link WXMApplicationVersionType#applicationName() applicationName}
   *    .setApplicationVersion(String) // required {@link WXMApplicationVersionType#applicationVersion() applicationVersion}
   *    .build();
   * 
* @return A new WXMApplicationVersion builder */ public static WXMApplicationVersion.Builder builder() { return new WXMApplicationVersion.Builder(); } /** * Builds instances of type {@link WXMApplicationVersion WXMApplicationVersion}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. *

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ public static final class Builder { private static final long INIT_BIT_APPLICATION_NAME = 0x1L; private static final long INIT_BIT_APPLICATION_VERSION = 0x2L; private long initBits = 0x3L; private String applicationName; private String applicationVersion; private Builder() { } /** * Fill a builder with attribute values from the provided {@code WXMApplicationVersionType} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(WXMApplicationVersionType instance) { Objects.requireNonNull(instance, "instance"); setApplicationName(instance.applicationName()); setApplicationVersion(instance.applicationVersion()); return this; } /** * Initializes the value for the {@link WXMApplicationVersionType#applicationName() applicationName} attribute. * @param applicationName The value for applicationName * @return {@code this} builder for use in a chained invocation */ public final Builder setApplicationName(String applicationName) { this.applicationName = Objects.requireNonNull(applicationName, "applicationName"); initBits &= ~INIT_BIT_APPLICATION_NAME; return this; } /** * Initializes the value for the {@link WXMApplicationVersionType#applicationVersion() applicationVersion} attribute. * @param applicationVersion The value for applicationVersion * @return {@code this} builder for use in a chained invocation */ public final Builder setApplicationVersion(String applicationVersion) { this.applicationVersion = Objects.requireNonNull(applicationVersion, "applicationVersion"); initBits &= ~INIT_BIT_APPLICATION_VERSION; return this; } /** * Builds a new {@link WXMApplicationVersion WXMApplicationVersion}. * @return An immutable instance of WXMApplicationVersion * @throws java.lang.IllegalStateException if any required attributes are missing */ public WXMApplicationVersion build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new WXMApplicationVersion(applicationName, applicationVersion); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_APPLICATION_NAME) != 0) attributes.add("applicationName"); if ((initBits & INIT_BIT_APPLICATION_VERSION) != 0) attributes.add("applicationVersion"); return "Cannot build WXMApplicationVersion, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy