Class Parser.ParsedComponent

java.lang.Object
io.github.hashadex.naturaldateinput.parsers.Parser.ParsedComponent
Enclosing class:
Parser

public static final class Parser.ParsedComponent extends Object
Immutable data class that represents one parsed regex match. Contains date and/or time, reference datetime, source string, as well as the start and end indexes of the regex match from which the date/time were parsed.

Note: A ParsedComponent will always contain a date or a time. A case when both date() and time() methods return empty Optionals is not possible.

Objects of this class can only be created by Parsers by using the protected Parser.ParsedComponentBuilder.

Since:
1.0.0
Author:
hashadex
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the parsed date if the parser was able to parse a date from the regex match.
    int
    Returns the end index of the regex match from which the date/time were parsed.
    boolean
    Compares this ParsedComponent to the specified object.
    int
    Returns the hash code for this ParsedComponent.
    int
    Returns the length of the regex match from which the date/time were parsed.
    Returns the reference datetime used by the parser, which is usually the datetime when the string was parsed.
    Returns the source string in which the parser had found and successfully parsed a certain date/time format.
    int
    Returns the start index of the regex match from which the date/time were parsed.
    Returns the text of the regex match from which the date/time were parsed.
    Returns the parsed time if the parser was able to parse a time from the regex match.
    Returns a string representation of this ParsedComponent.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Method Details

    • reference

      public LocalDateTime reference()
      Returns the reference datetime used by the parser, which is usually the datetime when the string was parsed.

      For example, ENRelativeWordParser uses the reference datetime to parse words like "today" and "tomorrow".

      Returns:
      Reference datetime
      Since:
      1.0.0
    • source

      public String source()
      Returns the source string in which the parser had found and successfully parsed a certain date/time format.
      Returns:
      Source string
      Since:
      1.0.0
    • startIndex

      public int startIndex()
      Returns the start index of the regex match from which the date/time were parsed.

      Use the text() method to get the substring of the source string containing the regex match text.

      Returns:
      Start index
      Since:
      1.0.0
      See Also:
    • endIndex

      public int endIndex()
      Returns the end index of the regex match from which the date/time were parsed.

      Use the text() method to get the substring of the source string containing the regex match text.

      Returns:
      End index
      Since:
      1.0.0
      See Also:
    • length

      public int length()
      Returns the length of the regex match from which the date/time were parsed.
      Returns:
      Length, calculated with endIndex - startIndex
      Since:
      1.0.0
      See Also:
    • text

      public String text()
      Returns the text of the regex match from which the date/time were parsed.

      Example:

       
       ENRelativeWordParser parser = new ENRelativeWordParser();
       
       ParsedComponent result = parser.parse("Meeting tomorrow").findAny().get();
       System.out.println(result.text()); // -> "tomorrow"
       
       
      Returns:
      Substring of the source string beginning at startIndex() and ending at endIndex()
      Since:
      1.0.0
      See Also:
    • date

      public Optional<LocalDate> date()
      Returns the parsed date if the parser was able to parse a date from the regex match.

      Note: a ParsedComponent will always contain a date and/or a time. Therefore, a case when both date() and time() return empty Optionals is not possible.

      Returns:
      Optional that may contain the parsed LocalDate
      Since:
      1.0.0
      See Also:
    • time

      public Optional<LocalTime> time()
      Returns the parsed time if the parser was able to parse a time from the regex match.

      Note: a ParsedComponent will always contain a date and/or a time. Therefore, a case when both date() and time() return empty Optionals is not possible.

      Returns:
      Optional that may contain the parsed LocalTime
      Since:
      1.0.0
      See Also:
    • equals

      public boolean equals(Object obj)
      Compares this ParsedComponent to the specified object.

      Returns true if the specified object is a ParsedComponent and all fields (reference, source string, start index, end index, date, time) are equal.

      Overrides:
      equals in class Object
      Parameters:
      obj - Object to compare, null returns false
      Returns:
      true if this ParsedComponent is equal to the specified object
      Since:
      1.0.0
    • hashCode

      public int hashCode()
      Returns the hash code for this ParsedComponent.
      Overrides:
      hashCode in class Object
      Returns:
      Hash code integer
      Since:
      1.0.0
    • toString

      public String toString()
      Returns a string representation of this ParsedComponent.
      Overrides:
      toString in class Object
      Returns:
      String in the format "ParsedComponent["<text>" -> <date>T<time>]"
      Since:
      1.0.0