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

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

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

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

/**
 * A parser request.
 */
@SuppressWarnings({"all"})
public final class SPIParserRequest implements SPIParserRequestType {
  private final URI file;
  private final InputStream stream;

  private SPIParserRequest(URI file, InputStream stream) {
    this.file = file;
    this.stream = stream;
  }

  /**
   * @return The URI of the file, for diagnostic purposes
   */
  @Override
  public URI file() {
    return file;
  }

  /**
   * @return An open input stream of the file to be parsed
   */
  @Override
  public InputStream stream() {
    return stream;
  }

  /**
   * Copy the current immutable object by setting a value for the {@link SPIParserRequestType#file() file} 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 file
   * @return A modified copy of the {@code this} object
   */
  public final SPIParserRequest withFile(URI value) {
    if (this.file == value) return this;
    URI newValue = Objects.requireNonNull(value, "file");
    return new SPIParserRequest(newValue, this.stream);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link SPIParserRequestType#stream() stream} 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 stream
   * @return A modified copy of the {@code this} object
   */
  public final SPIParserRequest withStream(InputStream value) {
    if (this.stream == value) return this;
    InputStream newValue = Objects.requireNonNull(value, "stream");
    return new SPIParserRequest(this.file, newValue);
  }

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

  private boolean equalTo(SPIParserRequest another) {
    return file.equals(another.file)
        && stream.equals(another.stream);
  }

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

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

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

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy