Class StringList

  • All Implemented Interfaces:
    java.io.Serializable, IStringSequence

    public final class StringList
    extends AbstractStringList
    A typesafe, mutable list of strings supporting a variety of convenient operations as well as expected operations from List such as add(), size(), remove(), iterator(), get(int index) and toArray(). Instances of the class are not threadsafe.

    StringList objects can be constructed empty or they can be created using any of several static factory methods:

    • valueOf(String[])
    • valueOf(String)
    • valueOf(Collection)
    • valueOf(Object[])
    In the case of the Collection and Object[] factory methods, each Object in the collection or array is converted to a String via toString() before being added to the StringList.

    The tokenize() factory methods allow easy creation of StringLists via StringTokenizer. The repeat() static factory method creates a StringList that repeats a given String a given number of times.

    The prepend() method adds a String to the beginning of the StringList. The removeLast() method pops a String off the end of the list. The sort() method sorts strings in the List using Collections.sort(). The class also inherits useful methods from AbstractStringList that include join() methods ala Perl and a toString() method which joins the list of strings with comma separators for easy viewing.

    Author:
    Jonathan Locke
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      StringList()
      Constructor.
      StringList​(int size)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int pos, java.lang.String string)
      Adds the string to the stringlist at position pos.
      void add​(java.lang.String string)
      Adds a string to the back of this list.
      void add​(StringValue value)
      Adds a string value to this list as a string.
      boolean contains​(java.lang.String string)  
      java.lang.String get​(int index)
      Gets the string at the given index.
      java.util.List<java.lang.String> getList()  
      IStringIterator iterator()
      Returns a typesafe iterator over this collection of strings.
      void prepend​(java.lang.String string)
      Adds the given string to the front of the list.
      void remove​(int index)
      Removes the string at the given index.
      void removeLast()
      Removes the last string in this list.
      static StringList repeat​(int count, java.lang.String string)
      Returns a list of a string repeated a given number of times.
      int size()  
      void sort()
      Sorts this string list alphabetically.
      java.lang.String[] toArray()
      Converts this string list to a string array.
      static StringList tokenize​(java.lang.String string)
      Extracts tokens from a comma and space delimited string.
      static StringList tokenize​(java.lang.String string, java.lang.String delimiters)
      Extracts tokens from a delimited string.
      int totalLength()  
      static StringList valueOf​(java.lang.Object[] objects)
      Converts an array of objects into a list of strings by using the object to string conversion method of the StringValue class.
      static StringList valueOf​(java.lang.String string)
      Returns a string list with just one string in it.
      static StringList valueOf​(java.lang.String[] array)
      Converts a string array to a string list.
      static StringList valueOf​(java.util.Collection<?> collection)
      Converts a collection of objects into a list of string values by using the conversion methods of the StringValue class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • StringList

        public StringList()
        Constructor.
      • StringList

        public StringList​(int size)
        Constructor.
        Parameters:
        size - Number of elements to preallocate
    • Method Detail

      • repeat

        public static StringList repeat​(int count,
                                        java.lang.String string)
        Returns a list of a string repeated a given number of times.
        Parameters:
        count - The number of times to repeat the string
        string - The string to repeat
        Returns:
        The list of strings
      • tokenize

        public static StringList tokenize​(java.lang.String string)
        Extracts tokens from a comma and space delimited string.
        Parameters:
        string - The string
        Returns:
        The string tokens as a list
      • tokenize

        public static StringList tokenize​(java.lang.String string,
                                          java.lang.String delimiters)
        Extracts tokens from a delimited string.
        Parameters:
        string - The string
        delimiters - The delimiters
        Returns:
        The string tokens as a list
      • valueOf

        public static StringList valueOf​(java.util.Collection<?> collection)
        Converts a collection of objects into a list of string values by using the conversion methods of the StringValue class.
        Parameters:
        collection - The collection to add as strings
        Returns:
        The list
      • valueOf

        public static StringList valueOf​(java.lang.Object[] objects)
        Converts an array of objects into a list of strings by using the object to string conversion method of the StringValue class.
        Parameters:
        objects - The objects to convert
        Returns:
        The list of strings
      • valueOf

        public static StringList valueOf​(java.lang.String string)
        Returns a string list with just one string in it.
        Parameters:
        string - The string
        Returns:
        The list of one string
      • valueOf

        public static StringList valueOf​(java.lang.String[] array)
        Converts a string array to a string list.
        Parameters:
        array - The array
        Returns:
        The list
      • add

        public void add​(java.lang.String string)
        Adds a string to the back of this list.
        Parameters:
        string - String to add
      • add

        public void add​(int pos,
                        java.lang.String string)
        Adds the string to the stringlist at position pos.
        Parameters:
        pos - the position to add the string at
        string - the string to add.
      • add

        public void add​(StringValue value)
        Adds a string value to this list as a string.
        Parameters:
        value - The value to add
      • contains

        public boolean contains​(java.lang.String string)
        Parameters:
        string - The string to look for
        Returns:
        True if the list contains the string
      • get

        public java.lang.String get​(int index)
        Gets the string at the given index.
        Specified by:
        get in interface IStringSequence
        Specified by:
        get in class AbstractStringList
        Parameters:
        index - The index
        Returns:
        The string at the index
        Throws:
        java.lang.IndexOutOfBoundsException
        See Also:
        IStringSequence.get(int)
      • getList

        public java.util.List<java.lang.String> getList()
        Returns:
        List value (not a copy of this list)
      • prepend

        public void prepend​(java.lang.String string)
        Adds the given string to the front of the list.
        Parameters:
        string - The string to add
      • remove

        public void remove​(int index)
        Removes the string at the given index.
        Parameters:
        index - The index
      • removeLast

        public void removeLast()
        Removes the last string in this list.
      • sort

        public void sort()
        Sorts this string list alphabetically.
      • toArray

        public java.lang.String[] toArray()
        Converts this string list to a string array.
        Overrides:
        toArray in class AbstractStringList
        Returns:
        The string array
      • totalLength

        public int totalLength()
        Overrides:
        totalLength in class AbstractStringList
        Returns:
        The total length of all strings in this list.