Class Primitive

java.lang.Object
org.djutils.primitives.Primitive

public final class Primitive extends Object
The Primitive class is a utility class to deal with primitives. Besides widening and unwidening this class casts and parses UTF8 strings into appropriate primitive classes.

Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See for project information https://djutils.org. The DJUTILS project is distributed under a three-clause BSD-style license, which can be found at https://djutils.org/docs/license.html.

Author:
Peter Jacobs
  • Method Details

    • cast

      public static Object[] cast(Class<?>[] classes, Object[] values)
      casts a set of values to classes.
      Parameters:
      classes - Class<?>[]; the classes to cast to
      values - Object[]; the values
      Returns:
      the newly creates values
    • cast

      public static Object cast(Class<?> clazz, Object object)
      casts an object to a instance of clazz.
      Parameters:
      clazz - Class<?>; the class to cast to
      object - Object; the object to cast
      Returns:
      Object; the casted object
    • forName

      public static Class<?> forName(String className)
      returns the primitiveClass of the name given as defined by the Java VM class constants. (i.e. both "int" and "I" return int.class). Both void and "V" return void.class. null is returned whenever an unknown className is given.
      Parameters:
      className - String; the className
      Returns:
      Class the primitiveClass
    • getPrimitive

      public static Class<?> getPrimitive(Class<?> wrapperClass)
      gets the primitive of the given wrapperClass.
      Parameters:
      wrapperClass - Class<?>; the wrapper class
      Returns:
      the primitive Class. null is returned whenever wrapperClass is not a wrapperclass.
    • getWrapper

      public static Class<?> getWrapper(Class<?> primitiveClass)
      gets the wrapper of this primitive class.
      Parameters:
      primitiveClass - Class<?>; the primitive class
      Returns:
      the Class. null is returned whenever wrapperClass is not a wrapperclass.
    • toBoolean

      public static Boolean toBoolean(Object object)
      casts an object to Boolean.
      Parameters:
      object - Object; the object
      Returns:
      Boolean
    • toByte

      public static Byte toByte(Object object)
      casts an object to Byte.
      Parameters:
      object - Object; the object
      Returns:
      Byte
    • toCharacter

      public static Character toCharacter(Object object)
      casts an object to Character.
      Parameters:
      object - Object; the object to parse
      Returns:
      Integer the result
    • toDouble

      public static Double toDouble(Object object)
      casts an object to Double.
      Parameters:
      object - Object; the object to parse
      Returns:
      Integer the result
    • toFloat

      public static Float toFloat(Object object)
      casts an object to Float.
      Parameters:
      object - Object; the object to parse
      Returns:
      Float the result
    • toLong

      public static Long toLong(Object object)
      casts an object to Long.
      Parameters:
      object - Object; the object to parse
      Returns:
      Long the result
    • toShort

      public static Short toShort(Object object)
      casts an object to Short.
      Parameters:
      object - Object; the object to parse
      Returns:
      Long the result
    • toInteger

      public static Integer toInteger(Object object)
      casts an object to Integer.
      Parameters:
      object - Object; the object to parse
      Returns:
      Integer the result
    • isPrimitiveAssignableFrom

      public static boolean isPrimitiveAssignableFrom(Class<?> firstClass, Class<?> secondClass)
      Returns true when the first class is assignable from the second class, e.g. when firstClass is Number and secondClass is Integer, the result is true (a number is assignable from an integer, i.e. Number n = Integer.valueOf(4) will work, but Integer i = new Number(4) will not work). This method also takes into account primitive types, so when firstClass is Number, and secondClass is int, the result is true, or when the firstClass is int, and the second class is Integer / firstClass is Integer and secondClass is int, the result will also be true.
      Parameters:
      firstClass - Class<?>; the class to which secondClass should be assignable
      secondClass - Class<?>; the class from which firstClass should be assignable
      Returns:
      firstClass.isAssignableFrom(secondClass) taking into acount primitive types