Package org.djutils.primitives
Class Primitive
java.lang.Object
org.djutils.primitives.Primitive
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 Summary
Modifier and TypeMethodDescriptionstatic Object[]
casts a set of values to classes.static Object
casts an object to a instance of clazz.static Class<?>
returns the primitiveClass of the name given as defined by the Java VM class constants.static Class<?>
getPrimitive
(Class<?> wrapperClass) gets the primitive of the given wrapperClass.static Class<?>
getWrapper
(Class<?> primitiveClass) gets the wrapper of this primitive class.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.static Boolean
casts an object to Boolean.static Byte
casts an object to Byte.static Character
toCharacter
(Object object) casts an object to Character.static Double
casts an object to Double.static Float
casts an object to Float.static Integer
casts an object to Integer.static Long
casts an object to Long.static Short
casts an object to Short.
-
Method Details
-
cast
casts a set of values to classes.- Parameters:
classes
- Class<?>[]; the classes to cast tovalues
- Object[]; the values- Returns:
- the newly creates values
-
cast
casts an object to a instance of clazz.- Parameters:
clazz
- Class<?>; the class to cast toobject
- Object; the object to cast- Returns:
- Object; the casted object
-
forName
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
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
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
casts an object to Boolean.- Parameters:
object
- Object; the object- Returns:
- Boolean
-
toByte
casts an object to Byte.- Parameters:
object
- Object; the object- Returns:
- Byte
-
toCharacter
casts an object to Character.- Parameters:
object
- Object; the object to parse- Returns:
- Integer the result
-
toDouble
casts an object to Double.- Parameters:
object
- Object; the object to parse- Returns:
- Integer the result
-
toFloat
casts an object to Float.- Parameters:
object
- Object; the object to parse- Returns:
- Float the result
-
toLong
casts an object to Long.- Parameters:
object
- Object; the object to parse- Returns:
- Long the result
-
toShort
casts an object to Short.- Parameters:
object
- Object; the object to parse- Returns:
- Long the result
-
toInteger
casts an object to Integer.- Parameters:
object
- Object; the object to parse- Returns:
- Integer the result
-
isPrimitiveAssignableFrom
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 assignablesecondClass
- Class<?>; the class from which firstClass should be assignable- Returns:
- firstClass.isAssignableFrom(secondClass) taking into acount primitive types
-