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

com.io7m.minisite.core.MinSourcesConfiguration Maven / Gradle / Ivy

package com.io7m.minisite.core;

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

/**
 * Configurations for source code repositories.
 */
@SuppressWarnings({"all"})
public final class MinSourcesConfiguration implements MinSourcesConfigurationType {
  private final String system;
  private final URI uri;

  private MinSourcesConfiguration(String system, URI uri) {
    this.system = Objects.requireNonNull(system, "system");
    this.uri = Objects.requireNonNull(uri, "uri");
  }

  private MinSourcesConfiguration(MinSourcesConfiguration original, String system, URI uri) {
    this.system = system;
    this.uri = uri;
  }

  /**
   * @return The SCM system (such as "Git")
   */
  @Override
  public String system() {
    return system;
  }

  /**
   * @return The repository URL
   */
  @Override
  public URI uri() {
    return uri;
  }

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

  /**
   * Copy the current immutable object by setting a value for the {@link MinSourcesConfigurationType#uri() uri} attribute.
   * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for uri
   * @return A modified copy of the {@code this} object
   */
  public final MinSourcesConfiguration withUri(URI value) {
    if (this.uri == value) return this;
    URI newValue = Objects.requireNonNull(value, "uri");
    return new MinSourcesConfiguration(this, this.system, newValue);
  }

  /**
   * This instance is equal to all instances of {@code MinSourcesConfiguration} 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 MinSourcesConfiguration
        && equalTo(0, (MinSourcesConfiguration) another);
  }

  private boolean equalTo(int synthetic, MinSourcesConfiguration another) {
    return system.equals(another.system)
        && uri.equals(another.uri);
  }

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

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

  /**
   * Construct a new immutable {@code MinSourcesConfiguration} instance.
   * @param system The value for the {@code system} attribute
   * @param uri The value for the {@code uri} attribute
   * @return An immutable MinSourcesConfiguration instance
   */
  public static MinSourcesConfiguration of(String system, URI uri) {
    return new MinSourcesConfiguration(system, uri);
  }

  /**
   * Creates an immutable copy of a {@link MinSourcesConfigurationType} 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 MinSourcesConfiguration instance
   */
  public static MinSourcesConfiguration copyOf(MinSourcesConfigurationType instance) {
    if (instance instanceof MinSourcesConfiguration) {
      return (MinSourcesConfiguration) instance;
    }
    return MinSourcesConfiguration.builder()
        .from(instance)
        .build();
  }

  /**
   * Creates a builder for {@link MinSourcesConfiguration MinSourcesConfiguration}.
   * 
   * MinSourcesConfiguration.builder()
   *    .setSystem(String) // required {@link MinSourcesConfigurationType#system() system}
   *    .setUri(java.net.URI) // required {@link MinSourcesConfigurationType#uri() uri}
   *    .build();
   * 
* @return A new MinSourcesConfiguration builder */ public static MinSourcesConfiguration.Builder builder() { return new MinSourcesConfiguration.Builder(); } /** * Builds instances of type {@link MinSourcesConfiguration MinSourcesConfiguration}. * 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_SYSTEM = 0x1L; private static final long INIT_BIT_URI = 0x2L; private long initBits = 0x3L; private String system; private URI uri; private Builder() { } /** * Fill a builder with attribute values from the provided {@code MinSourcesConfigurationType} 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(MinSourcesConfigurationType instance) { Objects.requireNonNull(instance, "instance"); this.setSystem(instance.system()); this.setUri(instance.uri()); return this; } /** * Initializes the value for the {@link MinSourcesConfigurationType#system() system} attribute. * @param system The value for system * @return {@code this} builder for use in a chained invocation */ public final Builder setSystem(String system) { this.system = Objects.requireNonNull(system, "system"); initBits &= ~INIT_BIT_SYSTEM; return this; } /** * Initializes the value for the {@link MinSourcesConfigurationType#uri() uri} attribute. * @param uri The value for uri * @return {@code this} builder for use in a chained invocation */ public final Builder setUri(URI uri) { this.uri = Objects.requireNonNull(uri, "uri"); initBits &= ~INIT_BIT_URI; return this; } /** * Builds a new {@link MinSourcesConfiguration MinSourcesConfiguration}. * @return An immutable instance of MinSourcesConfiguration * @throws java.lang.IllegalStateException if any required attributes are missing */ public MinSourcesConfiguration build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new MinSourcesConfiguration(null, system, uri); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_SYSTEM) != 0) attributes.add("system"); if ((initBits & INIT_BIT_URI) != 0) attributes.add("uri"); return "Cannot build MinSourcesConfiguration, some of required attributes are not set " + attributes; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy