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

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

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

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 ParseError implements ParseErrorType {
  private final LexicalPosition lexical;
  private final ParseErrorType.Severity severity;
  private final String message;
  private final Exception exception;

  private ParseError(
      LexicalPosition lexical,
      ParseErrorType.Severity severity,
      String message,
      Exception exception) {
    this.lexical = lexical;
    this.severity = severity;
    this.message = message;
    this.exception = exception;
  }

  /**
   * @return The value of the {@code lexical} attribute
   */
  @Override
  public LexicalPosition lexical() {
    return lexical;
  }

  /**
   * @return The error severity
   */
  @Override
  public ParseErrorType.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 ParseErrorType#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 ParseError withLexical(LexicalPosition value) {
    if (this.lexical == value) return this;
    LexicalPosition newValue = Objects.requireNonNull(value, "lexical");
    return new ParseError(newValue, this.severity, this.message, this.exception);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link ParseErrorType#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 ParseError withSeverity(ParseErrorType.Severity value) {
    if (this.severity == value) return this;
    ParseErrorType.Severity newValue = Objects.requireNonNull(value, "severity");
    if (this.severity.equals(newValue)) return this;
    return new ParseError(this.lexical, newValue, this.message, this.exception);
  }

  /**
   * Copy the current immutable object by setting a value for the {@link ParseErrorType#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 ParseError withMessage(String value) {
    String newValue = Objects.requireNonNull(value, "message");
    if (this.message.equals(newValue)) return this;
    return new ParseError(this.lexical, this.severity, newValue, this.exception);
  }

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

  /**
   * Copy the current immutable object by setting an optional value for the {@link ParseErrorType#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
   */
  @SuppressWarnings("unchecked") // safe covariant cast
  public final ParseError withException(Optional optional) {
    Exception value = optional.orElse(null);
    if (this.exception == value) return this;
    return new ParseError(this.lexical, this.severity, this.message, value);
  }

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

  private boolean equalTo(ParseError 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 ParseError} with attribute values.
   * @return A string representation of the value
   */
  @Override
  public String toString() {
    StringBuilder builder = new StringBuilder("ParseError{");
    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();
  }

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

  /**
   * Creates a builder for {@link ParseError ParseError}.
   * 
   * ParseError.builder()
   *    .setLexical(com.io7m.jlexing.core.LexicalPosition&lt;java.net.URI&gt;) // required {@link ParseErrorType#lexical() lexical}
   *    .setSeverity(com.io7m.coffeepick.runtime.parser.spi.ParseErrorType.Severity) // required {@link ParseErrorType#severity() severity}
   *    .setMessage(String) // required {@link ParseErrorType#message() message}
   *    .setException(Exception) // optional {@link ParseErrorType#exception() exception}
   *    .build();
   * 
* @return A new ParseError builder */ public static ParseError.Builder builder() { return new ParseError.Builder(); } /** * Builds instances of type {@link ParseError ParseError}. * 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 ParseErrorType.Severity severity; private String message; private Exception exception; private Builder() { } /** * Fill a builder with attribute values from the provided {@code ParseErrorType} 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(ParseErrorType 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 ParseErrorType#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 ParseErrorType#severity() severity} attribute. * @param severity The value for severity * @return {@code this} builder for use in a chained invocation */ public final Builder setSeverity(ParseErrorType.Severity severity) { this.severity = Objects.requireNonNull(severity, "severity"); initBits &= ~INIT_BIT_SEVERITY; return this; } /** * Initializes the value for the {@link ParseErrorType#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 ParseErrorType#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 ParseErrorType#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 ParseError ParseError}. * @return An immutable instance of ParseError * @throws java.lang.IllegalStateException if any required attributes are missing */ public ParseError build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new ParseError(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 ParseError, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy