Package groovy.lang

Class GString

  • All Implemented Interfaces:
    Buildable, GroovyObject, Writable,, java.lang.CharSequence, java.lang.Comparable
    Direct Known Subclasses:

    public abstract class GString
    extends GroovyObjectSupport
    implements java.lang.Comparable, java.lang.CharSequence, Writable, Buildable,
    Represents a String which contains embedded values such as "hello there ${user} how are you?" which can be evaluated lazily. Advanced users can iterate over the text and values to perform special processing, such as for performing SQL operations, the values can be substituted for ? and the actual value objects can be bound to a JDBC statement.

    James Strachan: The lovely name of this class was suggested by Jules Gosnell and was such a good idea, I couldn't resist :)

    See Also:
    Serialized Form
    • Field Detail


        public static final java.lang.String[] EMPTY_STRING_ARRAY

        public static final java.lang.Object[] EMPTY_OBJECT_ARRAY
      • EMPTY

        public static final GString EMPTY
        A GString containing a single empty String and no values.
    • Constructor Detail

      • GString

        public GString​(java.lang.Object values)
      • GString

        public GString​(java.lang.Object[] values)
    • Method Detail

      • getStrings

        public abstract java.lang.String[] getStrings()
      • invokeMethod

        public java.lang.Object invokeMethod​(java.lang.String name,
                                             java.lang.Object args)
        Overloaded to implement duck typing for Strings so that any method that can't be evaluated on this object will be forwarded to the toString() object instead.
        Specified by:
        invokeMethod in interface GroovyObject
        invokeMethod in class GroovyObjectSupport
        name - the name of the method to call
        args - the arguments to use for the method call
        the result of invoking the method
      • getValues

        public java.lang.Object[] getValues()
      • plus

        public GString plus​(java.lang.String that)
      • getValueCount

        public int getValueCount()
      • getValue

        public java.lang.Object getValue​(int idx)
      • toString

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

        public writeTo​( out)
        Description copied from interface: Writable
        Writes this object to the given writer.

        This is used to defer content creation until the point when it is streamed to the output destination. Oftentimes, content will be defined but not necessarily created (as is may be the case with a Closure definition.) In that case, the output is then 'deferred' to the point when it is serialized to the writer. This class may be used whenever an object should be responsible for creating its own textual representation, but creating the entire output as a single String would be inefficient (such as outputting a multi-gigabyte XML document.)

        Specified by:
        writeTo in interface Writable
        out - the Writer to which this Writable should output its data.
        the Writer that was passed
        Throws: - if an error occurred while outputting data to the writer
      • hashCode

        public int hashCode()
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object that)
        equals in class java.lang.Object
      • equals

        public boolean equals​(GString that)
      • compareTo

        public int compareTo​(java.lang.Object that)
        Specified by:
        compareTo in interface java.lang.Comparable
      • charAt

        public char charAt​(int index)
        Specified by:
        charAt in interface java.lang.CharSequence
      • length

        public int length()
        Specified by:
        length in interface java.lang.CharSequence
      • subSequence

        public java.lang.CharSequence subSequence​(int start,
                                                  int end)
        Specified by:
        subSequence in interface java.lang.CharSequence
      • negate

        public java.util.regex.Pattern negate()
        Turns a String into a regular expression pattern
        the regular expression pattern
      • getBytes

        public byte[] getBytes()
      • getBytes

        public byte[] getBytes​(java.lang.String charset)