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

com.io7m.changelog.parser.api.CParseError Maven / Gradle / Ivy

package com.io7m.changelog.parser.api;

import com.io7m.jlexing.core.LexicalPosition;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * The type of parse errors.
 */
@SuppressWarnings({"all"})
public final class CParseError implements CParseErrorType {
  private final LexicalPosition lexical;
  private final CParseErrorType.Severity severity;
  private final String message;
  private final Exception exception;

  private CParseError(
      LexicalPosition lexical,
      CParseErrorType.Severity severity,
      String message,
      Optional exception) {
    this.lexical = Objects.requireNonNull(lexical, "lexical");
    this.severity = Objects.requireNonNull(severity, "severity");
    this.message = Objects.requireNonNull(message, "message");
    this.exception = exception.orElse(null);
  }

  private CParseError(
      CParseError original,
      LexicalPosition lexical,
      CParseErrorType.Severity severity,
      String message,
      Exception exception) {
    this.lexical = lexical;
    this.severity = severity;
    this.message = message;
    this.exception = exception;
  }

  /**
   * @return The lexical position of the error
   */
  @Override
  public LexicalPosition lexical() {
    return lexical;
  }

  /**
   * @return The severity of the error
   */
  @Override
  public CParseErrorType.Severity severity() {
    return severity;
  }

  /**
   * @return The error message
   */
  @Override
  public String message() {
    return message;
  }

  /**
   * @return The exception raised, if any
   */
  @Override
  public Optional exception() {
    return Optional.ofNullable(exception);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link CParseErrorType#lexical() lexical} 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 lexical
   * @return A modified copy of the {@code this} object
   */
  public final CParseError withLexical(LexicalPosition value) {
    if (this.lexical == value) return this;
    LexicalPosition newValue = Objects.requireNonNull(value, "lexical");
    return new CParseError(this, newValue, this.severity, this.message, this.exception);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link CParseErrorType#severity() severity} attribute.
   * A value equality check is used to prevent copying of the same value by returning {@code this}.
   * @param value A new value for severity
   * @return A modified copy of the {@code this} object
   */
  public final CParseError withSeverity(CParseErrorType.Severity value) {
    if (this.severity == value) return this;
    CParseErrorType.Severity newValue = Objects.requireNonNull(value, "severity");
    return new CParseError(this, this.lexical, newValue, this.message, this.exception);
  }

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

  /**
   * Copy the current immutable object by setting a present value for the optional {@link CParseErrorType#exception() exception} attribute.
   * @param value The value for exception
   * @return A modified copy of {@code this} object
   */
  public final CParseError withException(Exception value) {
    Exception newValue = Objects.requireNonNull(value, "exception");
    if (this.exception == newValue) return this;
    return new CParseError(this, this.lexical, this.severity, this.message, newValue);
  }

  /**
   * Copy the current immutable object by setting an optional value for the {@link CParseErrorType#exception() exception} attribute.
   * A shallow reference equality check is used on unboxed optional value to prevent copying of the same value by returning {@code this}.
   * @param optional A value for exception
   * @return A modified copy of {@code this} object
   */
  public final CParseError withException(Optional optional) {
    Exception value = optional.orElse(null);
    if (this.exception == value) return this;
    return new CParseError(this, this.lexical, this.severity, this.message, value);
  }

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

  private boolean equalTo(CParseError another) {
    return lexical.equals(another.lexical)
        && severity.equals(another.severity)
        && message.equals(another.message)
        && Objects.equals(exception, another.exception);
  }

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

  /**
   * Prints the immutable value {@code CParseError} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder("CParseError{");
    builder.append("lexical=").append(lexical);
    builder.append(", ");
    builder.append("severity=").append(severity);
    builder.append(", ");
    builder.append("message=").append(message);
    if (exception != null) {
      builder.append(", ");
      builder.append("exception=").append(exception);
    }
    return builder.append("}").toString();
  }

  /**
   * Construct a new immutable {@code CParseError} instance.
   * @param lexical The value for the {@code lexical} attribute
   * @param severity The value for the {@code severity} attribute
   * @param message The value for the {@code message} attribute
   * @param exception The value for the {@code exception} attribute
   * @return An immutable CParseError instance
   */
  public static CParseError of(LexicalPosition lexical, CParseErrorType.Severity severity, String message, Optional exception) {
    return new CParseError(lexical, severity, message, exception);
  }

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

  /**
   * Creates a builder for {@link CParseError CParseError}.
   * @return A new CParseError builder
   */
  public static CParseError.Builder builder() {
    return new CParseError.Builder();
  }

  /**
   * Builds instances of type {@link CParseError CParseError}.
   * 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_LEXICAL = 0x1L; private static final long INIT_BIT_SEVERITY = 0x2L; private static final long INIT_BIT_MESSAGE = 0x4L; private long initBits = 0x7L; private LexicalPosition lexical; private CParseErrorType.Severity severity; private String message; private Exception exception; private Builder() { } /** * Fill a builder with attribute values from the provided {@code CParseErrorType} 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(CParseErrorType instance) { Objects.requireNonNull(instance, "instance"); setLexical(instance.lexical()); setSeverity(instance.severity()); setMessage(instance.message()); Optional exceptionOptional = instance.exception(); if (exceptionOptional.isPresent()) { setException(exceptionOptional); } return this; } /** * Initializes the value for the {@link CParseErrorType#lexical() lexical} attribute. * @param lexical The value for lexical * @return {@code this} builder for use in a chained invocation */ public final Builder setLexical(LexicalPosition lexical) { this.lexical = Objects.requireNonNull(lexical, "lexical"); initBits &= ~INIT_BIT_LEXICAL; return this; } /** * Initializes the value for the {@link CParseErrorType#severity() severity} attribute. * @param severity The value for severity * @return {@code this} builder for use in a chained invocation */ public final Builder setSeverity(CParseErrorType.Severity severity) { this.severity = Objects.requireNonNull(severity, "severity"); initBits &= ~INIT_BIT_SEVERITY; return this; } /** * Initializes the value for the {@link CParseErrorType#message() message} attribute. * @param message The value for message * @return {@code this} builder for use in a chained invocation */ public final Builder setMessage(String message) { this.message = Objects.requireNonNull(message, "message"); initBits &= ~INIT_BIT_MESSAGE; return this; } /** * Initializes the optional value {@link CParseErrorType#exception() exception} to exception. * @param exception The value for exception * @return {@code this} builder for chained invocation */ public final Builder setException(Exception exception) { this.exception = Objects.requireNonNull(exception, "exception"); return this; } /** * Initializes the optional value {@link CParseErrorType#exception() exception} to exception. * @param exception The value for exception * @return {@code this} builder for use in a chained invocation */ public final Builder setException(Optional exception) { this.exception = exception.orElse(null); return this; } /** * Builds a new {@link CParseError CParseError}. * @return An immutable instance of CParseError * @throws java.lang.IllegalStateException if any required attributes are missing */ public CParseError build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new CParseError(null, lexical, severity, message, exception); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList(); if ((initBits & INIT_BIT_LEXICAL) != 0) attributes.add("lexical"); if ((initBits & INIT_BIT_SEVERITY) != 0) attributes.add("severity"); if ((initBits & INIT_BIT_MESSAGE) != 0) attributes.add("message"); return "Cannot build CParseError, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy