Class StringNode

java.lang.Object
uk.blankaspect.common.basictree.AbstractNode
uk.blankaspect.common.basictree.StringNode
All Implemented Interfaces:
Cloneable, ITreeNode<AbstractNode>

public class StringNode extends AbstractNode
This class implements a node that contains a string.

The default string representation of a string node begins and ends with quotation marks, between which are zero or more literal characters or escape sequences. Within the quotation marks, the following characters must be escaped:

  • U+0022 : quotation mark ("),
  • U+005C : reverse solidus (\),
  • a control character in the range U+0000 to U+001F inclusive.

An escape sequence consists of a prefix (a reverse solidus, '\', U+005C) followed by either a single character or, for a Unicode escape, a 'u' and four hexadecimal-digit characters; see escape(CharSequence).

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    The character that denotes the end of the string representation of a string node.
    protected static final char[][]
    Mappings from literal characters to their corresponding characters in an escape sequence.
    static final String
    The string with which an escape sequence begins.
    static final char
    The character with which an escape sequence begins.
    static final char
    The character that denotes the start of the string representation of a string node.
    static final NodeType
    The type of a string node.
    static final char
    The character that denotes a Unicode escape sequence when it follows the escape prefix.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance of a string node that has no parent and has the specified value.
    StringNode(AbstractNode parent, String value)
    Creates a new instance of a string node that has the specified parent and value.
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    Returns a representation of the Unicode value of the specified character as a string of four hexadecimal-digit characters.
    Creates and returns a copy of this string node that has no parent.
    boolean
    Returns true if the specified object is an instance of StringNode and it has the same value as this string node.
    static String
    Transforms the specified character sequence to a string, replacing each occurrence of one of the following characters with an escape sequence, and returns the resulting string: a quotation mark ("), a reverse solidus (\), a character outside the range U+0020 to U+007E inclusive.
    static String
    Calls escape(CharSequence) on the specified character sequence, encloses the returned string in quotation marks and returns the resulting string.
    Returns the type of this node.
    Returns the value of this string node.
    int
    Returns the hash code of this string node.
    boolean
    Returns true if this node can contain other nodes.
    static String[]
    Returns an array whose elements are the values of the specified string nodes, with the order of the elements preserved.
    static List<String>
    nodesToList(Iterable<? extends StringNode> nodes)
    Returns a list whose elements are the values of the specified string nodes, with the order of the elements preserved.
    Returns a string representation of this string node.
    static String
    unicodeEscape(char ch)
    Returns the Unicode escape sequence of the specified character.
    Creates a list of string nodes for the specified values, preserving the order of the elements, and returns the list, which may be used to construct a list node.
    valuesToNodes(String... values)
    Creates a list of string nodes for the specified values, preserving the order of the elements, and returns the list, which may be used to construct a list node.

    Methods inherited from class uk.blankaspect.common.basictree.AbstractNode

    getChildren, getListIndex, getMapKey, getParent, isRoot, setParent

    Methods inherited from class java.lang.Object

    finalize, getClass, notify, notifyAll, wait, wait, wait

    Methods inherited from interface uk.blankaspect.common.tree.ITreeNode

    isLeaf
  • Field Details

    • START_CHAR

      public static final char START_CHAR
      The character that denotes the start of the string representation of a string node.
      See Also:
    • END_CHAR

      public static final char END_CHAR
      The character that denotes the end of the string representation of a string node.
      See Also:
    • ESCAPE_PREFIX_CHAR

      public static final char ESCAPE_PREFIX_CHAR
      The character with which an escape sequence begins.
      See Also:
    • ESCAPE_PREFIX

      public static final String ESCAPE_PREFIX
      The string with which an escape sequence begins.
    • UNICODE_ESCAPE_CHAR

      public static final char UNICODE_ESCAPE_CHAR
      The character that denotes a Unicode escape sequence when it follows the escape prefix.
      See Also:
    • TYPE

      public static final NodeType TYPE
      The type of a string node.
    • ESCAPE_MAPPINGS

      protected static final char[][] ESCAPE_MAPPINGS
      Mappings from literal characters to their corresponding characters in an escape sequence.
  • Constructor Details

    • StringNode

      public StringNode(String value)
      Creates a new instance of a string node that has no parent and has the specified value.
      Parameters:
      value - the value of the string node.
      Throws:
      IllegalArgumentException - if value is null.
    • StringNode

      public StringNode(AbstractNode parent, String value)
      Creates a new instance of a string node that has the specified parent and value.
      Parameters:
      parent - the parent of the string node.
      value - the value of the string node.
      Throws:
      IllegalArgumentException - if value is null.
  • Method Details

    • charToUnicodeHex

      public static String charToUnicodeHex(char ch)
      Returns a representation of the Unicode value of the specified character as a string of four hexadecimal-digit characters.
      Parameters:
      ch - the character whose string representation of its Unicode value is required.
      Returns:
      the representation of the Unicode value of ch as a string of four hexadecimal-digit characters.
    • unicodeEscape

      public static String unicodeEscape(char ch)
      Returns the Unicode escape sequence of the specified character.
      Parameters:
      ch - the character whose Unicode escape sequence is required.
      Returns:
      the Unicode escape sequence of ch.
    • escape

      public static String escape(CharSequence seq)
      Transforms the specified character sequence to a string, replacing each occurrence of one of the following characters with an escape sequence, and returns the resulting string:
      • a quotation mark ("),
      • a reverse solidus (\),
      • a character outside the range U+0020 to U+007E inclusive.

      Each occurrence of the following characters in the input sequence is replaced by the corresponding two-character escape sequence in the rightmost column of the table:

      Two-character escape sequences
      U+0008   backspace     \b
      U+0009   tab     \t
      U+000A   line feed     \n
      U+000C   form feed     \f
      U+000D   carriage return     \r
      U+0022   quotation mark (")     \"
      U+005C   reverse solidus (\)     \\

      In addition, each character in the input sequence that lies outside the range U+0020 to U+007E inclusive and is not in the table above is replaced by its six-character Unicode escape sequence, \u nnnn, where n is a hexadecimal-digit character.

      Characters in the input sequence that are not escaped appear in the output string unchanged.

      The returned string contains only printable characters from the US-ASCII character encoding (ie, characters in the range U+0020 to U+007E inclusive).

      Parameters:
      seq - the character sequence that will be escaped.
      Returns:
      a string in which each character in seq that must be escaped or is greater than U+007E is replaced by an escape sequence.
    • escapeAndQuote

      public static String escapeAndQuote(CharSequence seq)
      Calls escape(CharSequence) on the specified character sequence, encloses the returned string in quotation marks and returns the resulting string.
      Parameters:
      seq - the character sequence that will be escaped and quoted.
      Returns:
      a string consisting of seq escaped and enclosed in quotation marks.
    • valuesToNodes

      public static List<StringNode> valuesToNodes(String... values)
      Creates a list of string nodes for the specified values, preserving the order of the elements, and returns the list, which may be used to construct a list node.
      Parameters:
      values - the values for which string nodes will be created.
      Returns:
      a list of string nodes whose underlying values are values.
    • valuesToNodes

      public static List<StringNode> valuesToNodes(Iterable<String> values)
      Creates a list of string nodes for the specified values, preserving the order of the elements, and returns the list, which may be used to construct a list node.
      Parameters:
      values - the values for which string nodes will be created.
      Returns:
      a list of string nodes whose underlying values are values.
    • nodesToArray

      public static String[] nodesToArray(Collection<? extends StringNode> nodes)
      Returns an array whose elements are the values of the specified string nodes, with the order of the elements preserved.
      Parameters:
      nodes - the string nodes whose values will be extracted into an array.
      Returns:
      an array whose elements are the values of nodes.
    • nodesToList

      public static List<String> nodesToList(Iterable<? extends StringNode> nodes)
      Returns a list whose elements are the values of the specified string nodes, with the order of the elements preserved.
      Parameters:
      nodes - the string nodes whose values will be extracted into a list.
      Returns:
      a list whose elements are the values of nodes.
    • getType

      public NodeType getType()
      Description copied from class: AbstractNode
      Returns the type of this node.
      Specified by:
      getType in class AbstractNode
      Returns:
      TYPE.
    • isContainer

      public boolean isContainer()
      Returns true if this node can contain other nodes. For a string node, this method always returns false.
      Specified by:
      isContainer in class AbstractNode
      Returns:
      false.
    • equals

      public boolean equals(Object obj)
      Returns true if the specified object is an instance of StringNode and it has the same value as this string node.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object with which this string node will be compared.
      Returns:
      true if obj is an instance of StringNode and it has the same value as this string node; false otherwise.
    • hashCode

      public int hashCode()
      Returns the hash code of this string node.
      Overrides:
      hashCode in class Object
      Returns:
      the hash code of this string node.
    • clone

      public StringNode clone()
      Creates and returns a copy of this string node that has no parent.
      Overrides:
      clone in class AbstractNode
      Returns:
      a copy of this string node that has no parent.
    • toString

      public String toString()
      Returns a string representation of this string node.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this string node.
    • getValue

      public String getValue()
      Returns the value of this string node.
      Returns:
      the value of this string node.