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

com.io7m.coffeepick.runtime.parser.spi.ParsedRepository Maven / Gradle / Ivy

The newest version!
package com.io7m.coffeepick.runtime.parser.spi;

import com.io7m.coffeepick.runtime.RuntimeRepositoryDescription;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * A parsed repository
 */
@SuppressWarnings({"all"})
public final class ParsedRepository
    implements ParserResultType.ParsedRepositoryType {
  private final RuntimeRepositoryDescription repository;

  private ParsedRepository(RuntimeRepositoryDescription repository) {
    this.repository = Objects.requireNonNull(repository, "repository");
  }

  private ParsedRepository(ParsedRepository original, RuntimeRepositoryDescription repository) {
    this.repository = repository;
  }

  /**
   * @return The actual repository value
   */
  @Override
  public RuntimeRepositoryDescription repository() {
    return repository;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link ParserResultType.ParsedRepositoryType#repository() repository} 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 repository
   * @return A modified copy of the {@code this} object
   */
  public final ParsedRepository withRepository(RuntimeRepositoryDescription value) {
    if (this.repository == value) return this;
    RuntimeRepositoryDescription newValue = Objects.requireNonNull(value, "repository");
    return new ParsedRepository(this, newValue);
  }

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

  private boolean equalTo(ParsedRepository another) {
    return repository.equals(another.repository);
  }

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

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

  /**
   * Construct a new immutable {@code ParsedRepository} instance.
   * @param repository The value for the {@code repository} attribute
   * @return An immutable ParsedRepository instance
   */
  public static ParsedRepository of(RuntimeRepositoryDescription repository) {
    return new ParsedRepository(repository);
  }

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

  /**
   * Creates a builder for {@link ParsedRepository ParsedRepository}.
   * 
   * ParsedRepository.builder()
   *    .setRepository(com.io7m.coffeepick.runtime.RuntimeRepositoryDescription) // required {@link ParserResultType.ParsedRepositoryType#repository() repository}
   *    .build();
   * 
* @return A new ParsedRepository builder */ public static ParsedRepository.Builder builder() { return new ParsedRepository.Builder(); } /** * Builds instances of type {@link ParsedRepository ParsedRepository}. * 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_REPOSITORY = 0x1L; private long initBits = 0x1L; private RuntimeRepositoryDescription repository; private Builder() { } /** * Fill a builder with attribute values from the provided {@code ParsedRepositoryType} 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(ParserResultType.ParsedRepositoryType instance) { Objects.requireNonNull(instance, "instance"); setRepository(instance.repository()); return this; } /** * Initializes the value for the {@link ParserResultType.ParsedRepositoryType#repository() repository} attribute. * @param repository The value for repository * @return {@code this} builder for use in a chained invocation */ public final Builder setRepository(RuntimeRepositoryDescription repository) { this.repository = Objects.requireNonNull(repository, "repository"); initBits &= ~INIT_BIT_REPOSITORY; return this; } /** * Builds a new {@link ParsedRepository ParsedRepository}. * @return An immutable instance of ParsedRepository * @throws java.lang.IllegalStateException if any required attributes are missing */ public ParsedRepository build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ParsedRepository(null, repository); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList<>(); if ((initBits & INIT_BIT_REPOSITORY) != 0) attributes.add("repository"); return "Cannot build ParsedRepository, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy