Package fj.data

Class LazyString

  • All Implemented Interfaces:
    java.lang.CharSequence

    public final class LazyString
    extends java.lang.Object
    implements java.lang.CharSequence
    A lazy (non-evaluated) immutable character string.
    • Field Detail

      • s

        private final Stream<java.lang.Character> s
      • empty

        public static final LazyString empty
        The empty string.
      • toStream

        public static final F<LazyString,​Stream<java.lang.Character>> toStream
        First-class conversion from lazy strings to streams.
      • toString

        public static final F<LazyString,​java.lang.String> toString
        First-class conversion from lazy strings to String.
      • fromStream

        public static final F<Stream<java.lang.Character>,​LazyString> fromStream
        First-class conversion from character streams to lazy strings.
      • eqS

        private static final Equal<Stream<java.lang.Character>> eqS
    • Constructor Detail

      • LazyString

        private LazyString​(Stream<java.lang.Character> s)
    • Method Detail

      • str

        public static LazyString str​(java.lang.String s)
        Constructs a lazy string from a String.
        Parameters:
        s - A string from which to construct a lazy string.
        Returns:
        A lazy string with the characters from the given string.
      • fromStream

        public static LazyString fromStream​(Stream<java.lang.Character> s)
        Constructs a lazy string from a stream of characters.
        Parameters:
        s - A stream of characters.
        Returns:
        A lazy string with the characters from the given stream.
      • toStream

        public Stream<java.lang.Character> toStream()
        Gives a stream representation of this lazy string.
        Returns:
        A stream representation of this lazy string.
      • length

        public int length()
        The length of the lazy string. Note that this operation is O(n).
        Specified by:
        length in interface java.lang.CharSequence
        Returns:
        The length of this lazy string.
      • charAt

        public char charAt​(int index)
        Returns the caracter at the specified index.
        Specified by:
        charAt in interface java.lang.CharSequence
        Parameters:
        index - The index for the character to be returned.
        Returns:
        The character at the specified index.
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Gets the specified subsequence of this lazy string. This operation does not fail for indexes that are out of bounds. If the start index is past the end of this lazy string, then the resulting character sequence will be empty. If the end index is past the end of this lazy string, then the resulting character sequence will be truncated.
        Specified by:
        subSequence in interface java.lang.CharSequence
        Parameters:
        start - The character index of this lazy string at which to start the subsequence.
        end - The character index of this lazy string at which to end the subsequence.
        Returns:
        A character sequence containing the specified character subsequence.
      • toStringEager

        public java.lang.String toStringEager()
        Returns the String representation of this lazy string.
        Returns:
        The String representation of this lazy string.
      • toStringLazy

        public java.lang.String toStringLazy()
      • toString

        public java.lang.String toString()
        Specified by:
        toString in interface java.lang.CharSequence
        Overrides:
        toString in class java.lang.Object
      • eval

        public java.lang.String eval()
      • append

        public LazyString append​(LazyString cs)
        Appends the given lazy string to the end of this lazy string.
        Parameters:
        cs - A lazy string to append to this one.
        Returns:
        A new lazy string that is the concatenation of this string and the given string.
      • append

        public LazyString append​(java.lang.String s)
        Appends the given String to the end of this lazy string.
        Parameters:
        s - A String to append to this lazy string.
        Returns:
        A new lazy string that is the concatenation of this lazy string and the given string.
      • contains

        public boolean contains​(LazyString cs)
        Returns true if the given lazy string is a substring of this lazy string.
        Parameters:
        cs - A substring to find in this lazy string.
        Returns:
        True if the given string is a substring of this string, otherwise False.
      • endsWith

        public boolean endsWith​(LazyString cs)
        Returns true if the given lazy string is a suffix of this lazy string.
        Parameters:
        cs - A string to find at the end of this lazy string.
        Returns:
        True if the given string is a suffix of this lazy string, otherwise False.
      • startsWith

        public boolean startsWith​(LazyString cs)
        Returns true if the given lazy string is a prefix of this lazy string.
        Parameters:
        cs - A string to find at the start of this lazy string.
        Returns:
        True if the given string is a prefix of this lazy string, otherwise False.
      • startsWith

        public static F<LazyString,​F<LazyString,​java.lang.Boolean>> startsWith()
        First-class prefix check.
        Returns:
        A function that yields true if the first argument is a prefix of the second.
      • head

        public char head()
        Returns the first character of this string.
        Returns:
        The first character of this string, or error if the string is empty.
      • tail

        public LazyString tail()
        Returns all but the first character of this string.
        Returns:
        All but the first character of this string, or error if the string is empty.
      • isEmpty

        public boolean isEmpty()
        Checks if this string is empty.
        Returns:
        True if there are no characters in this string, otherwise False.
      • reverse

        public LazyString reverse()
        Returns the reverse of this string.
        Returns:
        the reverse of this string.
      • indexOf

        public Option<java.lang.Integer> indexOf​(char c)
        Returns the first index of the given character in this lazy string, if present.
        Parameters:
        c - A character to find in this lazy string.
        Returns:
        The first index of the given character in this lazy string, or None if the character is not present.
      • indexOf

        public Option<java.lang.Integer> indexOf​(LazyString cs)
        Returns the first index of the given substring in this lazy string, if present.
        Parameters:
        cs - A substring to find in this lazy string.
        Returns:
        The first index of the given substring in this lazy string, or None if there is no such substring.
      • matches

        public boolean matches​(java.lang.String regex)
        Regular expression pattern matching.
        Parameters:
        regex - A regular expression to match this lazy string.
        Returns:
        True if this string mathches the given regular expression, otherwise False.
      • split

        public Stream<LazyString> split​(F<java.lang.Character,​java.lang.Boolean> p)
        Splits this lazy string by characters matching the given predicate.
        Parameters:
        p - A predicate that matches characters to be considered delimiters.
        Returns:
        A stream of the substrings in this lazy string, when separated by the given predicate.
      • map

        public LazyString map​(F<java.lang.Character,​java.lang.Character> f)
      • split

        public Stream<LazyString> split​(char c)
        Splits this lazy string by the given delimiter character.
        Parameters:
        c - A delimiter character at which to split.
        Returns:
        A stream of substrings of this lazy string, when separated by the given delimiter.
      • words

        public Stream<LazyString> words()
        Splits this lazy string into words by spaces.
        Returns:
        A stream of the words in this lazy string, when split by spaces.
      • lines

        public Stream<LazyString> lines()
        Splits this lazy string into lines.
        Returns:
        A stream of the lines in this lazy string, when split by newlines.
      • unlines

        public static LazyString unlines​(Stream<LazyString> str)
        Joins the given stream of lazy strings into one, separated by newlines.
        Parameters:
        str - A stream of lazy strings to join by newlines.
        Returns:
        A new lazy string, consisting of the given strings separated by newlines.
      • unwords

        public static LazyString unwords​(Stream<LazyString> str)
        Joins the given stream of lazy strings into one, separated by spaces.
        Parameters:
        str - A stream of lazy strings to join by spaces.
        Returns:
        A new lazy string, consisting of the given strings with spaces in between.