Package groovy.lang
Class GString
java.lang.Object
groovy.lang.GroovyObjectSupport
groovy.lang.GString
- All Implemented Interfaces:
Buildable,GroovyObject,Writable,Serializable,CharSequence,Comparable
- Direct Known Subclasses:
GStringImpl
public abstract class GString
extends GroovyObjectSupport
implements Comparable, CharSequence, Writable, Buildable, Serializable
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:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbuild(GroovyObject builder) Builds this object into the supplied builder.protected intCalculates the initial buffer capacity for rendering this GString.charcharAt(int index) intbooleanCompares this GString with another GString by rendered content.booleanbyte[]getBytes()Returns the rendered string as a byte array using the platform default charset.byte[]Returns the rendered string as a byte array using the supplied charset.abstract String[]Returns the string segments surrounding the interpolated values.getValue(int idx) Returns the interpolated value at the supplied index.intReturns the number of interpolated values.Object[]Returns the interpolated values.inthashCode()invokeMethod(String name, 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.intlength()negate()Turns a String into a regular expression patternConcatenates this GString with another GString.Concatenates this GString with a String.subSequence(int start, int end) toString()Writes this object to the given writer.Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClassMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.CharSequence
chars, codePoints, isEmptyMethods inherited from interface groovy.lang.GroovyObject
getProperty, setProperty
-
Field Details
-
EMPTY_STRING_ARRAY
Shared empty string array. -
EMPTY_OBJECT_ARRAY
Shared empty object array. -
EMPTY
A GString containing a single empty String and no values.
-
-
Constructor Details
-
GString
Creates a GString from the supplied values array reference.- Parameters:
values- the interpolated values
-
GString
Creates a GString from the supplied values.- Parameters:
values- the interpolated values
-
-
Method Details
-
getStrings
Returns the string segments surrounding the interpolated values.- Returns:
- the string segments
-
invokeMethod
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:
invokeMethodin interfaceGroovyObject- Parameters:
name- the name of the method to callargs- the arguments to use for the method call- Returns:
- the result of invoking the method
-
getValues
Returns the interpolated values.- Returns:
- the interpolated values
-
plus
Concatenates this GString with another GString.- Parameters:
that- the other GString- Returns:
- the concatenated GString
-
plus
Concatenates this GString with a String.- Parameters:
that- the string to append- Returns:
- the concatenated GString
-
getValueCount
public int getValueCount()Returns the number of interpolated values.- Returns:
- the value count
-
getValue
Returns the interpolated value at the supplied index.- Parameters:
idx- the value index- Returns:
- the interpolated value
-
toString
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-
calcInitialCapacity
protected int calcInitialCapacity()Calculates the initial buffer capacity for rendering this GString.- Returns:
- the initial capacity
-
writeTo
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 it 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:
writeToin interfaceWritable- Parameters:
out- the Writer to which this Writable should output its data.- Returns:
- the Writer that was passed
- Throws:
IOException- if an error occurred while outputting data to the writer
-
build
Builds this object into the supplied builder. -
hashCode
public int hashCode() -
equals
-
equals
Compares this GString with another GString by rendered content.- Parameters:
that- the other GString- Returns:
trueif the rendered strings are equal
-
compareTo
- Specified by:
compareToin interfaceComparable
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
length
public int length()- Specified by:
lengthin interfaceCharSequence
-
subSequence
- Specified by:
subSequencein interfaceCharSequence
-
negate
Turns a String into a regular expression pattern- Returns:
- the regular expression pattern
-
getBytes
public byte[] getBytes()Returns the rendered string as a byte array using the platform default charset.- Returns:
- the rendered bytes
-
getBytes
Returns the rendered string as a byte array using the supplied charset.- Parameters:
charset- the charset name- Returns:
- the rendered bytes
- Throws:
UnsupportedEncodingException- if the charset is unsupported
-