Class SList

All Implemented Interfaces:
Sexpr<SList>, Comparable<Sexpr<?>>, Iterable<Sexpr<?>>, Collection<Sexpr<?>>, List<Sexpr<?>>

public final class SList extends AbstractList<Sexpr<?>> implements Sexpr<SList>
Symbolic List.

Textually, a SList consists of an opening parenthesis, a the series of elements separated by spaces, followed by a closing parenthesis.

All instances of this interface are immutable.

  • Method Details

    • of

      public static SList of(Sexpr<?>... elements)
      Factory Method.
      Parameters:
      elements - will be the elements in this list.
      Returns:
      the new symbolic-list.
    • of

      public static SList of(SourceLocation location, Sexpr<?>... elements)
      Factory Method.
      Parameters:
      location - will be the location() of this list.
      elements - will be the elements in this list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(Iterable<? extends Sexpr<?>> list)
      Factory Method.
      Parameters:
      list - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(SourceLocation location, Iterable<? extends Sexpr<?>> list)
      Factory Method.
      Parameters:
      location - will be the location() of this list.
      list - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(Stream<? extends Sexpr<?>> stream)
      Factory Method.
      Parameters:
      stream - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(SourceLocation location, Stream<? extends Sexpr<?>> stream)
      Factory Method.
      Parameters:
      location - will be the location() of this list.
      stream - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(Iterator<? extends Sexpr<?>> stream)
      Factory Method.
      Parameters:
      stream - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • copyOf

      public static SList copyOf(SourceLocation location, Iterator<? extends Sexpr<?>> stream)
      Factory Method.
      Parameters:
      location - will be the location() of this list.
      stream - contains the elements for the new list.
      Returns:
      the new symbolic-list.
    • fromMap

      public static SList fromMap(SourceLocation location, Map<? extends Sexpr<?>,? extends Sexpr<?>> map)
      This method creates a new two-dimensional list from the given map.

      The result will be a list of lists. Each inner list will correspond to a single entry in the map. Each inner list will have exactly two elements. The first element is the key from the map entry. The second element is the value from the map entry.

      Parameters:
      location - will be the location() of this list.
      map - contains the entries to add to the list.
      Returns:
      the new symbolic-list.
    • fromMap

      public static SList fromMap(SourceLocation location, Map<? extends Sexpr<?>,? extends Sexpr<?>> map, Sexpr<?> separator)
      This method creates a new two-dimensional list from the given map.

      The result will be a list of lists. Each inner list will correspond to a single entry in the map. Each inner list will have exactly three elements. The first element will be the key from the map entry. the second element will be the given separator. The third element will be the value from the map entry.

      Parameters:
      location - will be the location() of this list.
      map - contains the entries to add to the list.
      separator - will be used as the key-value separator.
      Returns:
      the new symbolic-list.
    • get

      public Sexpr get(int i)
      Specified by:
      get in interface List<Sexpr<?>>
      Specified by:
      get in class AbstractList<Sexpr<?>>
    • isAtom

      public boolean isAtom()
      This method determines whether this object is a SAtom.
      Specified by:
      isAtom in interface Sexpr<SList>
      Returns:
      true, iff this object is a SAtom.
    • isList

      public boolean isList()
      This method determines whether this object is a SList.
      Specified by:
      isList in interface Sexpr<SList>
      Returns:
      true, iff this object is a SAtom.
    • first

      public Sexpr first()
      This method retrieves the first element of this list.
      Returns:
      the first element, or null, if the list is empty.
    • last

      public Sexpr last()
      This method retrieves the last element of this list.
      Returns:
      the last element, or null, if the list is empty.
    • location

      public SourceLocation location()
      This method retrieves an object describing the source from which this object we obtained.
      Specified by:
      location in interface Sexpr<SList>
      Returns:
      the source-code location of this node.
    • mutator

      public SList.Mutator mutator()
      This method obtains a mutator that can be used to non-destructively modify the tree rooted at this node.
      Returns:
      the mutator.
    • size

      public int size()
      Specified by:
      size in interface Collection<Sexpr<?>>
      Specified by:
      size in interface List<Sexpr<?>>
      Specified by:
      size in class AbstractCollection<Sexpr<?>>
    • tail

      public SList tail()
      This method retrieves the sub-list containing all of the elements of this list, except for the first element in linear-time.
      Returns:
      the tail sub-list.
    • bfs

      public boolean bfs(Predicate<Sexpr<?>> condition)
      This method performs a breadth-first-search of the tree rooted at this node.
      Specified by:
      bfs in interface Sexpr<SList>
      Parameters:
      condition - will return true, when the sought after node is found.
      Returns:
      true, if the node was found.
    • dfs

      public boolean dfs(Predicate<Sexpr<?>> condition)
      This method performs a depth-first-search of the tree rooted at this node.
      Specified by:
      dfs in interface Sexpr<SList>
      Parameters:
      condition - will return true, when the sought after node is found.
      Returns:
      true, if the node was found.
    • preorder

      public boolean preorder(Predicate<Sexpr<?>> condition)
      This method performs a pre-order-search of the tree rooted at this node.
      Specified by:
      preorder in interface Sexpr<SList>
      Parameters:
      condition - will return true, when the sought after node is found.
      Returns:
      true, if the node was found.
    • postorder

      public boolean postorder(Predicate<Sexpr<?>> condition)
      This method performs a post-order-search of the tree rooted at this node.
      Specified by:
      postorder in interface Sexpr<SList>
      Parameters:
      condition - will return true, when the sought after node is found.
      Returns:
      true, if the node was found.
    • traverse

      public void traverse(Consumer<Sexpr<?>> before, Consumer<Sexpr<?>> after)
      This method performs a traversal of the tree rooted at this node.
      Specified by:
      traverse in interface Sexpr<SList>
      Parameters:
      before - will be invoked upon entering each sub-tree.
      after - will be invoked upon exiting each sub-tree.
    • asMap

      public Optional<Map<Sexpr<?>,Sexpr<?>>> asMap()
      This method retrieves this value, as a map.

      This list must be a list of sub-lists. Each sub-list will correspond to an entry in the new map. The first element in the sub-list will be used as a map-key. The last element in the sub-list will be used as a map-value. If the same map-key occurs multiple times, the last entry will prevail.

      Returns:
      the immutable value, if possible.
    • treeHeight

      public int treeHeight()
      This method determines the height of the tree rooted at this node.

      This is a constant-time operation.

      Specified by:
      treeHeight in interface Sexpr<SList>
      Returns:
      the height of this tree.
    • treeLeafCount

      public int treeLeafCount()
      This method counts the leaf nodes in the tree rooted at this node.

      This is a constant-time operation.

      Specified by:
      treeLeafCount in interface Sexpr<SList>
      Returns:
      the number of leaf nodes.
    • treeSize

      public int treeSize()
      This method counts the nodes in the tree rooted at this node.

      This is a constant-time operation.

      Specified by:
      treeSize in interface Sexpr<SList>
      Returns:
      the number of nodes, including this node.
    • equals

      public final boolean equals(Object other)
      Specified by:
      equals in interface Collection<Sexpr<?>>
      Specified by:
      equals in interface List<Sexpr<?>>
      Overrides:
      equals in class AbstractList<Sexpr<?>>
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<Sexpr<?>>
      Specified by:
      hashCode in interface List<Sexpr<?>>
      Overrides:
      hashCode in class AbstractList<Sexpr<?>>
    • toString

      public String toString()
      This method retrieves the textual representation of this symbolic expression.
      Specified by:
      toString in interface Sexpr<SList>
      Overrides:
      toString in class AbstractCollection<Sexpr<?>>
      Returns:
      this object as a string.
    • parse

      public static SList parse(String location, String input)
      This method converts the textual representation of a SList to an actual corresponding SList object.

      This method inserts an implicit symbolic-list into the input. For example, the input "(1 2) (3 4)" will produce a SList equivalent to "((1 2) (3 4))".

      Parameters:
      location - is a human-readable string indicating where the input came form.
      input - is the input to parse.
      Returns:
      the resulting symbolic-list.
    • parse

      public static SList parse(String input)
      This method converts the textual representation of a SList to an actual corresponding SList object.

      This method inserts an implicit symbolic-list into the input. For example, the input "(1 2) (3 4)" will produce a SList equivalent to "((1 2) (3 4))".

      Parameters:
      input - is the input to parse.
      Returns:
      the resulting symbolic-list.
    • parseResource

      public static SList parseResource(String path) throws IOException
      This method converts the textual representation of a resource file to an actual corresponding SList object.

      See method parse(*) for more parsing details.

      Parameters:
      path - is the path to the resource file.
      Returns:
      the new symbolic-list.
      Throws:
      IOException - if the resource cannot be read.
    • parseFile

      public static SList parseFile(File file) throws IOException
      This method converts the textual representation of a text file to an actual corresponding SList object.

      See method parse(*) for more parsing details.

      Parameters:
      file - is the path to the file.
      Returns:
      the new symbolic-list.
      Throws:
      IOException - if the resource cannot be read.