Interface Data


public interface Data
Interface of wrappers for semantic data.

There is no need for client code to deal with concrete subclasses. Wrapping and unwrapping are performed behind the scenes, by semantic parser actions and abstract accessors of type Data.View, respectively.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Functional interface for unwrapping interpretation of semantic data.
    static class 
    A placeholder for a view that can be initialized late.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Data
    add(Data elem)
    Adds an element to a list.
    default List<Data>
    Unwraps a list to its elements.
    static Data
    atomic(Object value)
    Wraps an atomic value.
    static <D, T> Data
    content(Token<D,T> token)
    Wraps a copy of a lexical token.
    static <D, T> Data
    content(@Opt Location<D> location, String text, T type)
    Wraps a lexical token.
    default <A> Data
    flatMapAs(Class<A> type, Function<? super A,Data> fun)
    Transforms a wrapped atomic value with a given function.
    default Data
    get()
    Returns the present content of an optional value.
    default @Opt Location<?>
    Returns the source code location of a wrapped token.
    default int
    Returns the tag index of a tagged value.
    default int
    Returns the total number of choices of a tagged value.
    default String
    Returns the text content of a wrapped token.
    default Object
    Returns the token type of a wrapped token.
    default Object
    Returns the atomic value wrapped by this data value.
    default <A> A
    getValueAs(Class<A> type)
    Returns the atomic value wrapped by this data value if of the expected type.
    default boolean
    Checks whether this data value wraps an atomic value.
    default boolean
    isAtomic(Class<?> type)
    Checks whether this data value wraps an atomic value of the given type.
    default boolean
    Checks whether this data value wraps a lexical token.
    default boolean
    Checks whether this data value is optional.
    default boolean
    Checks whether this data value is a pair.
    default boolean
    Checks whether an optional value is present.
    default boolean
    Checks whether this data value is a sequence.
    default boolean
    Checks whether this data value is tagged.
    default boolean
    isTagged(int expectedTotal)
    Checks whether this data value is tagged out of a given total number of choices.
    static Data
    just(Data value)
    Marks a value as optional and present.
    default <A, B> Data
    mapAs(Class<A> type, Function<? super A,? extends B> fun)
    Transforms a wrapped atomic value with a given function.
    static Data
    Returns an empty optional value.
    default Data
    orElse(Data defaultValue)
    Returns the present content of an optional value or a default.
    default Data
    orElseGet(Supplier<? extends Data> defaultValue)
    Returns the present content of an optional value or a default.
    static Data
    pair(Data left, Data right)
    Combines two data elements into a pair.
    static Data
    sequence(Data... elems)
    Combines zero or more data elements into a sequence.
    tag(int index, int total)
    Returns a function that marks values with a numerical tag.
    static Data
    tag(int index, int total, Data value)
    Marks a value with a numerical tag.
    default Data
    Returns the content of a tagged value.
    default Data
    untag(int expectedTotal)
    Returns the content of a tagged value out of the given number of choices.
  • Method Details

    • atomic

      static Data atomic(Object value)
      Wraps an atomic value.
      Parameters:
      value - the value
      Returns:
      a wrapper around value
    • content

      static <D, T> Data content(Token<D,T> token)
      Wraps a copy of a lexical token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      token - the token
      Returns:
      a wrapped copy of token
      Throws:
      NullPointerException - if token is null
    • content

      static <D, T> Data content(@Opt Location<D> location, String text, T type)
      Wraps a lexical token.
      Type Parameters:
      D - the type of document identifiers
      T - the type of token types
      Parameters:
      location - the source code location, or null if not available
      text - the text content
      type - the token type
      Returns:
      a wrapped token
      Throws:
      NullPointerException - if text or type is null
    • sequence

      static Data sequence(Data... elems)
      Combines zero or more data elements into a sequence.
      Parameters:
      elems - the elements
      Returns:
      a sequence containing all of elems
      Throws:
      NullPointerException - if elems is null
    • pair

      static Data pair(Data left, Data right)
      Combines two data elements into a pair.
      Parameters:
      left - the left element
      right - the right element
      Returns:
      a pair containing left and right
      Throws:
      NullPointerException - if left or right is null
    • just

      static Data just(Data value)
      Marks a value as optional and present.
      Parameters:
      value - the content value
      Returns:
      an optional and present wrapper
    • none

      static Data none()
      Returns an empty optional value.
      Returns:
      a value that does not contain data
    • tag

      static Data tag(int index, int total, Data value)
      Marks a value with a numerical tag.

      Tags are nonnegative indices out of a specified number of choices.

      Parameters:
      index - the tag index
      total - the total number of choices
      value - the content value
      Returns:
      a value tagged as index out of total
      Throws:
      IllegalArgumentException - if index < 0 or total < 0, or if index >= total.
    • tag

      static UnaryOperator<Data> tag(int index, int total)
      Returns a function that marks values with a numerical tag.

      Tags are nonnegative indices out of a specified number of choices.

      Parameters:
      index - the tag index
      total - the total number of choices
      Returns:
      a function that tags values as index out of total
      Throws:
      IllegalArgumentException - if index < 0 or total < 0, or if index >= total.
      See Also:
    • isSequence

      default boolean isSequence()
      Checks whether this data value is a sequence.
      Returns:
      true if this data value is a sequence; false otherwise
      See Also:
    • isAtomic

      default boolean isAtomic()
      Checks whether this data value wraps an atomic value.
      Returns:
      true if this data value wraps an atomic value; false otherwise
      See Also:
    • isAtomic

      default boolean isAtomic(Class<?> type)
      Checks whether this data value wraps an atomic value of the given type.
      Parameters:
      type - the expected type
      Returns:
      true if this data value wraps an atomic value of type type; false otherwise
      Throws:
      NullPointerException - if type is null
      See Also:
    • isContent

      default boolean isContent()
      Checks whether this data value wraps a lexical token.
      Returns:
      true if this data value wraps a lexical token; false otherwise
      See Also:
    • isPair

      default boolean isPair()
      Checks whether this data value is a pair.
      Returns:
      true if this data value is a pair; false otherwise
      See Also:
    • isTagged

      default boolean isTagged()
      Checks whether this data value is tagged.
      Returns:
      true if this data value is tagged; false otherwise
      See Also:
    • isTagged

      default boolean isTagged(int expectedTotal)
      Checks whether this data value is tagged out of a given total number of choices.
      Parameters:
      expectedTotal - the expected total number of choices
      Returns:
      true if this data value is tagged and its total number of choices is equal to expectedTotal; false otherwise
      See Also:
    • isOptional

      default boolean isOptional()
      Checks whether this data value is optional.
      Returns:
      true if this data value is optional; false otherwise
      See Also:
    • getValueAs

      default <A> A getValueAs(Class<A> type)
      Returns the atomic value wrapped by this data value if of the expected type.
      Type Parameters:
      A - the expected type
      Parameters:
      type - the expected type
      Returns:
      the unwrapped atomic value
      Throws:
      UnsupportedOperationException - if this data value does not wrap an atomic value
      ClassCastException - if the unwrapped value is not of the expected type
      NullPointerException - if type is null
    • getValue

      default Object getValue()
      Returns the atomic value wrapped by this data value.
      Returns:
      the unwrapped atomic value
      Throws:
      UnsupportedOperationException - if this data value does not wrap an atomic value
    • mapAs

      default <A, B> Data mapAs(Class<A> type, Function<? super A,? extends B> fun)
      Transforms a wrapped atomic value with a given function.
      Type Parameters:
      A - the expected type
      B - the result type
      Parameters:
      type - the expected type
      fun - a function that maps the wrapped atomic value to another atomic value
      Returns:
      the wrapped result of mapping the wrapped atomic value
      Throws:
      UnsupportedOperationException - if this data value does not wrap an atomic value
      ClassCastException - if the unwrapped value is not of the expected type
      NullPointerException - if type or fun is null
    • flatMapAs

      default <A> Data flatMapAs(Class<A> type, Function<? super A,Data> fun)
      Transforms a wrapped atomic value with a given function.
      Type Parameters:
      A - the expected type
      Parameters:
      type - the expected type
      fun - a function that maps the wrapped atomic value to some arbitrary value
      Returns:
      the result of mapping the wrapped atomic value
      Throws:
      UnsupportedOperationException - if this data value does not wrap an atomic value
      ClassCastException - if the unwrapped value is not of the expected type
      NullPointerException - if type or fun is null
    • add

      default Data add(Data elem)
      Adds an element to a list.
      Parameters:
      elem - the element to add
      Returns:
      a new list containing all elements of this list followed by elem
      Throws:
      UnsupportedOperationException - if this value is not a list
    • asList

      default List<Data> asList()
      Unwraps a list to its elements.
      Returns:
      the elements of this data value if it is a list
      Throws:
      UnsupportedOperationException - if this value is not a list
    • getTagIndex

      default int getTagIndex()
      Returns the tag index of a tagged value.
      Returns:
      the tag index of this data value if it is tagged
      Throws:
      UnsupportedOperationException - if this value is not tagged
    • getTagTotal

      default int getTagTotal()
      Returns the total number of choices of a tagged value.
      Returns:
      the total number of choices of this data value if it is tagged
      Throws:
      UnsupportedOperationException - if this value is not tagged
    • untag

      default Data untag()
      Returns the content of a tagged value.
      Returns:
      the content of this data value if it is tagged
      Throws:
      UnsupportedOperationException - if this value is not tagged
    • untag

      default Data untag(int expectedTotal)
      Returns the content of a tagged value out of the given number of choices.
      Parameters:
      expectedTotal - the expected total number of choices
      Returns:
      the content of this data value if it is tagged and its total number of choices is equal to expectedTotal
      Throws:
      UnsupportedOperationException - if this value is not tagged
      IllegalArgumentException - if this value is tagged but its total number of choices is not equal to expectedTotal
    • getLocation

      default @Opt Location<?> getLocation()
      Returns the source code location of a wrapped token.
      Returns:
      the source code location of the wrapped token, or null if not available
      Throws:
      UnsupportedOperationException - if this data value does not wrap a token
    • getText

      default String getText()
      Returns the text content of a wrapped token.
      Returns:
      the text content of the wrapped token
      Throws:
      UnsupportedOperationException - if this data value does not wrap a token
    • getType

      default Object getType()
      Returns the token type of a wrapped token.
      Returns:
      the token type of the wrapped token
      Throws:
      UnsupportedOperationException - if this data value does not wrap a token
    • isPresent

      default boolean isPresent()
      Checks whether an optional value is present.
      Returns:
      true if this value is optional and present; false if this value is optional and absent
      Throws:
      UnsupportedOperationException - if this value is not optional
    • get

      default Data get()
      Returns the present content of an optional value.
      Returns:
      the content if this value is optional and present
      Throws:
      UnsupportedOperationException - if this value is not optional
      NoSuchElementException - if this value is optional but absent
    • orElse

      default Data orElse(Data defaultValue)
      Returns the present content of an optional value or a default.
      Parameters:
      defaultValue - the default value to stand for absence
      Returns:
      the content if this value is optional and present; defaultValue if this value is optional and absent
      Throws:
      UnsupportedOperationException - if this value is not optional
    • orElseGet

      default Data orElseGet(Supplier<? extends Data> defaultValue)
      Returns the present content of an optional value or a default.
      Parameters:
      defaultValue - an operation that either supplies the default value to stand for absence or throws NoSuchElementException
      Returns:
      the content if this value is optional and present; the result of defaultValue.get() if this value is optional and absent
      Throws:
      UnsupportedOperationException - if this value is not optional
      NullPointerException - if defaultValue is null