Class DefaultTypeTransformation
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final Object[]Deprecated, for removal: This API element is subject to removal in a future version.since 5.0.0protected static final BigIntegerDeprecated, for removal: This API element is subject to removal in a future version.since 5.0.0 -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CollectionarrayAsCollection(Object value) Converts an array (including primitive arrays) to a Collection.static <T> Collection<T>arrayAsCollection(T[] value) Converts an object array to a Collection backed byArrays.asList.static ObjectConverts an object to an array of the specified target array type.static CollectionasCollection(Object value) Converts an object to a Collection.static <T> Collection<T>asCollection(T[] value) Converts a generic typed array to a typed Collection.static booleanbooleanUnbox(Object value) Unboxes an object to a primitivebooleanvalue.static Objectbox(boolean value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(byte value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(char value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(double value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(float value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(int value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(long value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic Objectbox(short value) Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationstatic byteUnboxes an object to a primitivebytevalue.static booleancastToBoolean(Object object) Method used for coercing an object to a boolean value, thanks to anasBoolean()method added on types.static charcastToChar(Object object) Deprecated.since 2.3.0 - useShortTypeHandling.castToChar(Object)insteadstatic NumbercastToNumber(Object object) Attempts to coerce an object to aNumber.static NumbercastToNumber(Object object, Class type) Attempts to coerce an object to aNumberof a specific target type.static ObjectcastToType(Object object, Class type) Performs a comprehensive type cast/coercion of an object to a target class.static ObjectcastToVargsArray(Object[] origin, int firstVargsPos, Class<?> arrayType) Converts an array of arguments to a varargs array of the specified type.static charUnboxes an object to a primitivecharvalue.static booleancompareArrayEqual(Object left, Object right) Compares two arrays element-wise for equality.static booleancompareEqual(Object left, Object right) Compares two objects for equality, handling nulls and type coercion.static intCompares the two objects handling nulls gracefully and performing numeric type coercion if requiredstatic boolean[]Deprecated.static byte[]Deprecated.static char[]Deprecated.static double[]Deprecated.static float[]Deprecated.static int[]Deprecated.static long[]Deprecated.static ObjectconvertToPrimitiveArray(Object a, Class type) Deprecated.static short[]Deprecated.static doubledoubleUnbox(Object value) Unboxes an object to a primitivedoublevalue.static floatfloatUnbox(Object value) Unboxes an object to a primitivefloatvalue.static CharactergetCharFromSizeOneString(Object value) Deprecated.static intUnboxes an object to a primitiveintvalue.static booleanisEnumSubclass(Object value) Deprecated.static longUnboxes an object to a primitivelongvalue.static Object[]primitiveArrayBox(Object array) Boxes a primitive array to an object array.static ListprimitiveArrayToList(Object array) Allows conversion of arrays into a mutable Liststatic ListAllows conversion of arrays into an immutable List viewstatic shortshortUnbox(Object value) Unboxes an object to a primitiveshortvalue.
-
Field Details
-
EMPTY_ARGUMENTS
Deprecated, for removal: This API element is subject to removal in a future version.since 5.0.0Empty array constant for backwards compatibility. -
ONE_NEG
Deprecated, for removal: This API element is subject to removal in a future version.since 5.0.0BigInteger constant representing negative one, for backwards compatibility.
-
-
Constructor Details
-
DefaultTypeTransformation
public DefaultTypeTransformation()
-
-
Method Details
-
byteUnbox
Unboxes an object to a primitivebytevalue.Converts the object to a
Numberand then extracts its byte value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation)- Returns:
- the byte value
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
charUnbox
Unboxes an object to a primitivecharvalue.Returns the null character (
' ') if the value is null. Otherwise delegates toShortTypeHandling.castToChar(Object).- Parameters:
value- the object to unbox (may be null, Character, Number, or String)- Returns:
- the char value, or null character if value is null
- Throws:
GroovyCastException- if the object cannot be converted to char
-
shortUnbox
Unboxes an object to a primitiveshortvalue.Converts the object to a
Numberand then extracts its short value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation)- Returns:
- the short value
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
intUnbox
Unboxes an object to a primitiveintvalue.Converts the object to a
Numberand then extracts its int value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation)- Returns:
- the int value
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
booleanUnbox
Unboxes an object to a primitivebooleanvalue.Uses
castToBoolean(Object)to perform the coercion.- Parameters:
value- the object to unbox (may be null for false, Boolean, or any object with asBoolean())- Returns:
- the boolean value
-
longUnbox
Unboxes an object to a primitivelongvalue.Converts the object to a
Numberand then extracts its long value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation)- Returns:
- the long value
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
floatUnbox
Unboxes an object to a primitivefloatvalue.Returns
Float.NaNif the value is null. Otherwise converts the object to aNumberand extracts its float value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation, may be null)- Returns:
- the float value, or NaN if value is null
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
doubleUnbox
Unboxes an object to a primitivedoublevalue.Returns
Double.NaNif the value is null. Otherwise converts the object to aNumberand extracts its double value.- Parameters:
value- the object to unbox (typically a wrapper object or string representation, may be null)- Returns:
- the double value, or NaN if value is null
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivebooleanvalue into aBooleanwrapper object.- Parameters:
value- the boolean value to box- Returns:
Boolean.TRUEorBoolean.FALSE
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivebytevalue into aBytewrapper object.- Parameters:
value- the byte value to box- Returns:
- a Byte object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivecharvalue into aCharacterwrapper object.- Parameters:
value- the char value to box- Returns:
- a Character object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitiveshortvalue into aShortwrapper object.- Parameters:
value- the short value to box- Returns:
- a Short object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitiveintvalue into anIntegerwrapper object.- Parameters:
value- the int value to box- Returns:
- an Integer object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivelongvalue into aLongwrapper object.- Parameters:
value- the long value to box- Returns:
- a Long object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivefloatvalue into aFloatwrapper object.- Parameters:
value- the float value to box- Returns:
- a Float object with the specified value
-
box
Deprecated.since 2.3.0 - Java's auto-boxing is sufficient for this operationBoxes a primitivedoublevalue into aDoublewrapper object.- Parameters:
value- the double value to box- Returns:
- a Double object with the specified value
-
castToNumber
Attempts to coerce an object to aNumber.If the object is already a Number, it is returned as-is. Characters are converted to their numeric code point. Strings of length 1 are converted to their character code point. GStrings are converted to String first, then processed.
- Parameters:
object- the object to coerce (Number, Character, GString, or String)- Returns:
- a Number representation of the object
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
castToNumber
Attempts to coerce an object to aNumberof a specific target type.If the object is already a Number, it is returned as-is. Characters are converted to their numeric code point. Strings of length 1 are converted to their character code point. GStrings are converted to String first, then processed.
- Parameters:
object- the object to coerce (Number, Character, GString, or String)type- the target numeric type (used for error reporting)- Returns:
- a Number representation of the object
- Throws:
GroovyCastException- if the object cannot be converted to a number
-
castToBoolean
Method used for coercing an object to a boolean value, thanks to anasBoolean()method added on types.- Parameters:
object- to coerce to a boolean value- Returns:
- a boolean value
-
castToChar
Deprecated.since 2.3.0 - useShortTypeHandling.castToChar(Object)insteadAttempts to coerce an object to acharvalue (as a Character wrapper).Accepts Character objects, Numbers (converted via intValue), or Strings of length 1.
- Parameters:
object- the object to coerce (Character, Number, or String)- Returns:
- a Character with the coerced value
- Throws:
GroovyCastException- if the object cannot be converted to char
-
castToType
Performs a comprehensive type cast/coercion of an object to a target class.Handles the following conversions:
- Primitive types (delegates to castToPrimitive)
- null and Object.class (returns as-is)
- Already compatible types (returns as-is)
- Array types (delegates to asArray)
- Enum types (delegates to ShortTypeHandling)
- Collection types (delegates to continueCastOnCollection)
- String type (uses FormatHelper)
- Boolean type (uses castToBoolean)
- Character type (delegates to ShortTypeHandling)
- Class type (delegates to ShortTypeHandling)
- Number types (delegates to continueCastOnNumber)
- Parameters:
object- the object to cast (may be null)type- the target type to cast to- Returns:
- an object of the target type, or null if input is null and type is not primitive
- Throws:
ClassCastException- or GroovyCastException if casting is not possible
-
asArray
Converts an object to an array of the specified target array type.Handles direct assignment if already correct type, stream conversions (IntStream, LongStream, DoubleStream), and generic collection to array conversions with element type casting.
- Parameters:
object- the object to convert (Collection, Stream, or already an array)type- the target array type- Returns:
- an array of the target type with elements converted and cast as needed
- Throws:
ClassCastException- if array element conversion is not possible
-
asCollection
Converts a generic typed array to a typed Collection.- Type Parameters:
T- the element type- Parameters:
value- the typed array to convert- Returns:
- a Collection backed by the array elements
-
asCollection
Converts an object to a Collection.Handles the following conversions:
- null → empty list
- Collection → returned as-is
- Map → collection of entry set
- array → collection backed by array
- BaseStream (IntStream, etc.) → list via StreamGroovyMethods
- String/GString → list of characters
- Iterable → list via DefaultGroovyMethods
- Optional → singleton or empty set
- Enum Class → list of enum constants
- File → list of lines
- MethodClosure → list via adapter
- Other → singleton collection containing the object
- Parameters:
value- the object to convert (may be null)- Returns:
- a Collection representing the object's elements
- Throws:
GroovyRuntimeException- if File reading fails
-
arrayAsCollection
Converts an array (including primitive arrays) to a Collection.- Parameters:
value- an array (primitive or object array)- Returns:
- a Collection backed by the array elements
-
arrayAsCollection
Converts an object array to a Collection backed byArrays.asList.- Type Parameters:
T- the element type- Parameters:
value- an object array- Returns:
- a Collection (mutable list) backed by the array
-
isEnumSubclass
Deprecated.Determines whether the value object is a Class object representing a subclass of java.lang.Enum. Uses class name check to avoid breaking on pre-Java 5 JREs.- Parameters:
value- an object- Returns:
- true if the object is an Enum
-
primitiveArrayToList
Allows conversion of arrays into a mutable List- Parameters:
array- an array- Returns:
- the array as a List
-
primitiveArrayToUnmodifiableList
Allows conversion of arrays into an immutable List view- Parameters:
array- an array- Returns:
- a List view of the array
-
primitiveArrayBox
Boxes a primitive array to an object array.Converts primitive values in the array to their boxed equivalents. Each primitive element is converted to its corresponding wrapper class (e.g., int to Integer).
- Parameters:
array- the primitive array to box- Returns:
- an Object array containing the boxed values
-
compareTo
Compares the two objects handling nulls gracefully and performing numeric type coercion if required -
compareEqual
Compares two objects for equality, handling nulls and type coercion.Performs semantic equality comparison with proper handling of null values, null objects, Comparable types, and arrays. Applies numeric type coercion when appropriate.
- Parameters:
left- the first object to compareright- the second object to compare- Returns:
- true if the objects are equal, false otherwise
-
compareArrayEqual
Compares two arrays element-wise for equality.Performs element-by-element comparison of two arrays using
compareEqual(Object, Object). Returns true only if both arrays have the same length and all corresponding elements are equal. Null arrays are handled according to standard null comparison semantics.- Parameters:
left- the first array to compareright- the second array to compare- Returns:
- true if the arrays are equal (same length and equal elements), false otherwise
-
convertToIntArray
Deprecated. -
convertToBooleanArray
Deprecated. -
convertToByteArray
Deprecated. -
convertToShortArray
Deprecated. -
convertToCharArray
Deprecated. -
convertToLongArray
Deprecated. -
convertToFloatArray
Deprecated. -
convertToDoubleArray
Deprecated. -
convertToPrimitiveArray
Deprecated. -
getCharFromSizeOneString
Deprecated. -
castToVargsArray
Converts an array of arguments to a varargs array of the specified type.Handles conversion of remaining arguments starting from the specified position into a properly typed array. If the argument at the start position is already the target array type and it's the only remaining argument, returns it unchanged. Otherwise, creates a new array with type-converted elements.
- Parameters:
origin- the original array of argumentsfirstVargsPos- the index of the first varargs elementarrayType- the target array type- Returns:
- an array of the specified type containing the varargs elements
-