Class DefaultTypeTransformation

java.lang.Object
org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation

public class DefaultTypeTransformation extends Object
Class providing various type conversions, coercions and boxing/unboxing operations.
  • Field Details

    • EMPTY_ARGUMENTS

      @Deprecated(forRemoval=true, since="5.0.0") protected static final Object[] EMPTY_ARGUMENTS
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 5.0.0
      Empty array constant for backwards compatibility.
    • ONE_NEG

      @Deprecated(forRemoval=true, since="5.0.0") protected static final BigInteger ONE_NEG
      Deprecated, for removal: This API element is subject to removal in a future version.
      since 5.0.0
      BigInteger constant representing negative one, for backwards compatibility.
  • Constructor Details

    • DefaultTypeTransformation

      public DefaultTypeTransformation()
  • Method Details

    • byteUnbox

      public static byte byteUnbox(Object value)
      Unboxes an object to a primitive byte value.

      Converts the object to a Number and 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

      public static char charUnbox(Object value)
      Unboxes an object to a primitive char value.

      Returns the null character ('') if the value is null. Otherwise delegates to ShortTypeHandling.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

      public static short shortUnbox(Object value)
      Unboxes an object to a primitive short value.

      Converts the object to a Number and 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

      public static int intUnbox(Object value)
      Unboxes an object to a primitive int value.

      Converts the object to a Number and 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

      public static boolean booleanUnbox(Object value)
      Unboxes an object to a primitive boolean value.

      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

      public static long longUnbox(Object value)
      Unboxes an object to a primitive long value.

      Converts the object to a Number and 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

      public static float floatUnbox(Object value)
      Unboxes an object to a primitive float value.

      Returns Float.NaN if the value is null. Otherwise converts the object to a Number and 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

      public static double doubleUnbox(Object value)
      Unboxes an object to a primitive double value.

      Returns Double.NaN if the value is null. Otherwise converts the object to a Number and 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") public static Object box(boolean value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive boolean value into a Boolean wrapper object.
      Parameters:
      value - the boolean value to box
      Returns:
      Boolean.TRUE or Boolean.FALSE
    • box

      @Deprecated(since="2.3.0") public static Object box(byte value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive byte value into a Byte wrapper object.
      Parameters:
      value - the byte value to box
      Returns:
      a Byte object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(char value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive char value into a Character wrapper object.
      Parameters:
      value - the char value to box
      Returns:
      a Character object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(short value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive short value into a Short wrapper object.
      Parameters:
      value - the short value to box
      Returns:
      a Short object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(int value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive int value into an Integer wrapper object.
      Parameters:
      value - the int value to box
      Returns:
      an Integer object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(long value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive long value into a Long wrapper object.
      Parameters:
      value - the long value to box
      Returns:
      a Long object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(float value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive float value into a Float wrapper object.
      Parameters:
      value - the float value to box
      Returns:
      a Float object with the specified value
    • box

      @Deprecated(since="2.3.0") public static Object box(double value)
      Deprecated.
      since 2.3.0 - Java's auto-boxing is sufficient for this operation
      Boxes a primitive double value into a Double wrapper object.
      Parameters:
      value - the double value to box
      Returns:
      a Double object with the specified value
    • castToNumber

      public static Number castToNumber(Object object)
      Attempts to coerce an object to a Number.

      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

      public static Number castToNumber(Object object, Class type)
      Attempts to coerce an object to a Number of 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

      public static boolean castToBoolean(Object object)
      Method used for coercing an object to a boolean value, thanks to an asBoolean() method added on types.
      Parameters:
      object - to coerce to a boolean value
      Returns:
      a boolean value
    • castToChar

      @Deprecated(since="2.3.0") public static char castToChar(Object object)
      Deprecated.
      since 2.3.0 - use ShortTypeHandling.castToChar(Object) instead
      Attempts to coerce an object to a char value (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

      public static Object castToType(Object object, Class type)
      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

      public static Object asArray(Object object, Class type)
      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

      public static <T> Collection<T> asCollection(T[] value)
      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

      public static Collection asCollection(Object value)
      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

      public static Collection arrayAsCollection(Object value)
      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

      public static <T> Collection<T> arrayAsCollection(T[] value)
      Converts an object array to a Collection backed by Arrays.asList.
      Type Parameters:
      T - the element type
      Parameters:
      value - an object array
      Returns:
      a Collection (mutable list) backed by the array
    • isEnumSubclass

      @Deprecated(since="2.3.0") public static boolean isEnumSubclass(Object value)
      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

      public static List primitiveArrayToList(Object array)
      Allows conversion of arrays into a mutable List
      Parameters:
      array - an array
      Returns:
      the array as a List
    • primitiveArrayToUnmodifiableList

      public static List primitiveArrayToUnmodifiableList(Object array)
      Allows conversion of arrays into an immutable List view
      Parameters:
      array - an array
      Returns:
      a List view of the array
    • primitiveArrayBox

      public static Object[] primitiveArrayBox(Object array)
      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

      public static int compareTo(Object left, Object right)
      Compares the two objects handling nulls gracefully and performing numeric type coercion if required
    • compareEqual

      public static boolean compareEqual(Object left, Object right)
      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 compare
      right - the second object to compare
      Returns:
      true if the objects are equal, false otherwise
    • compareArrayEqual

      public static boolean compareArrayEqual(Object left, Object right)
      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 compare
      right - the second array to compare
      Returns:
      true if the arrays are equal (same length and equal elements), false otherwise
    • convertToIntArray

      @Deprecated(since="2.3.0") public static int[] convertToIntArray(Object a)
      Deprecated.
    • convertToBooleanArray

      @Deprecated(since="2.3.0") public static boolean[] convertToBooleanArray(Object a)
      Deprecated.
    • convertToByteArray

      @Deprecated(since="2.3.0") public static byte[] convertToByteArray(Object a)
      Deprecated.
    • convertToShortArray

      @Deprecated(since="2.3.0") public static short[] convertToShortArray(Object a)
      Deprecated.
    • convertToCharArray

      @Deprecated(since="2.3.0") public static char[] convertToCharArray(Object a)
      Deprecated.
    • convertToLongArray

      @Deprecated(since="2.3.0") public static long[] convertToLongArray(Object a)
      Deprecated.
    • convertToFloatArray

      @Deprecated(since="2.3.0") public static float[] convertToFloatArray(Object a)
      Deprecated.
    • convertToDoubleArray

      @Deprecated(since="2.3.0") public static double[] convertToDoubleArray(Object a)
      Deprecated.
    • convertToPrimitiveArray

      @Deprecated(since="2.3.0") public static Object convertToPrimitiveArray(Object a, Class type)
      Deprecated.
    • getCharFromSizeOneString

      @Deprecated(since="2.3.0") public static Character getCharFromSizeOneString(Object value)
      Deprecated.
    • castToVargsArray

      public static Object castToVargsArray(Object[] origin, int firstVargsPos, Class<?> arrayType)
      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 arguments
      firstVargsPos - the index of the first varargs element
      arrayType - the target array type
      Returns:
      an array of the specified type containing the varargs elements