Package org.dellroad.stuff.java
Class Primitive<T>
java.lang.Object
org.dellroad.stuff.java.Primitive<T>
- Type Parameters:
T
- Primitive wrapper type
- All Implemented Interfaces:
Serializable
,Comparator<T>
Simple utility enumeration for working Java
Class
instances representing primitive types (including void
).
Instances of this class represent the eight Java primitive types plus void
.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionBoolean type.Byte type.Character type.Double type.Float type.Integer type.Long type.Short type.Void type. -
Method Summary
Modifier and TypeMethodDescriptionabstract int
Compare two primitive values.static Primitive<?>
Get the instance with the given name (e.g.,"void"
,"int"
, etc.).static <T> Primitive<T>
Get the value corresponding to the given Java primitive or primitive wrapper type.abstract T
Get this primitive's default value.char
Get the single character descriptor for this primitive type, e.g., "I".Get the long name for this primitive type, e.g., "Integer".getName()
Get the short name for this primitive type, e.g., "int".Get a regular expression that matches all allowed inputs toparseValue()
.int
getSize()
Get the size of one value of this primitive type in bytes.getType()
Get theClass
object representing this primitive type, e.g.,Integer.TYPE
.Get the wrapper class' "unwrap" method for this primitive type, e.g.,Integer.intValue()
.Get the wrapperClass
object for this primitive type, e.g.,Integer.class
.parseValue
(String string) Parse a string-encoded value of this instance.toString()
static <T> Class<T>
Unwrap the given type into its primitive type if it's a primitive wrapper type.values()
Get all instances of this class.abstract <R> R
visit
(PrimitiveSwitch<R> pswitch) Apply visitor pattern.static <T> Class<T>
Wrap the given primitive type into its wrapper type if it's a primitive type.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Comparator
equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Field Details
-
VOID
-
BOOLEAN
Boolean type. String values must equal"true"
or"false"
. -
BYTE
Byte type. String values are parsed usingByte.decode()
. -
CHARACTER
Character type. String values must be exactly one character long. -
SHORT
Short type. String values are parsed usingShort.decode()
. -
INTEGER
Integer type. String values are parsed usingInteger.decode()
. -
FLOAT
Float type. String values are parsed usingFloat.parseFloat()
. -
LONG
Long type. String values are parsed usingLong.decode()
. -
DOUBLE
Double type. String values are parsed usingDouble.parseDouble()
.
-
-
Method Details
-
visit
Apply visitor pattern.- Type Parameters:
R
- return type- Parameters:
pswitch
- visit target- Returns:
- result of visit
-
getDefaultValue
Get this primitive's default value.- Returns:
- default value for this primitive type
-
getName
Get the short name for this primitive type, e.g., "int".- Returns:
- short form name of this primitive type
-
getLongName
Get the long name for this primitive type, e.g., "Integer". Also the simple name of the wrapper type.- Returns:
- long form name of this primitive type
-
getLetter
public char getLetter()Get the single character descriptor for this primitive type, e.g., "I".- Returns:
- letter associated with this primitive type
-
getSize
public int getSize()Get the size of one value of this primitive type in bytes.- Returns:
- primitive size in bytes
-
getType
Get theClass
object representing this primitive type, e.g.,Integer.TYPE
.- Returns:
- primitive type associated with this primitive type
-
getWrapperType
Get the wrapperClass
object for this primitive type, e.g.,Integer.class
.- Returns:
- wrapper type associated with this primitive type
-
getParsePattern
Get a regular expression that matches all allowed inputs toparseValue()
. The returned pattern may also accept some values thatparseValue()
rejects, such as32768
for ashort
.- Returns:
- pattern matching valid string encodings
-
getUnwrapMethod
Get the wrapper class' "unwrap" method for this primitive type, e.g.,Integer.intValue()
.- Returns:
- the unwrap method associated with this primitive type
-
compare
Compare two primitive values.- Specified by:
compare
in interfaceComparator<T>
- Throws:
NullPointerException
- if either value is null
-
parseValue
Parse a string-encoded value of this instance. This method is guaranteed to accept all possible return values from the primitive'stoString()
, and possibly other reasonable inputs (e.g., hex and octal values for integral types).- Parameters:
string
- string representation of a value- Returns:
- parsed value
- Throws:
IllegalArgumentException
- ifstring
is nullIllegalArgumentException
- ifstring
does not match the parse pattern
-
forName
Get the instance with the given name (e.g.,"void"
,"int"
, etc.).- Parameters:
name
- instance short name- Returns:
- corresponding instance, or null if name does not correspond to any
Primitive
instance - Throws:
IllegalArgumentException
- ifname
is null
-
values
Get all instances of this class.- Returns:
- a modifiable set containing all instances; modifications have no effect on this class
-
get
Get the value corresponding to the given Java primitive or primitive wrapper type.- Parameters:
c
- Java type class object- Returns:
- the
Primitive
corresponding toc
, ornull
ifc
is not a primitive or primitive wrapper type
-
unwrap
Unwrap the given type into its primitive type if it's a primitive wrapper type.- Parameters:
type
- given type- Returns:
- primitive type corresponding to
type
, ortype
iftype
is not a primitive wrapper type, or null iftype
is null
-
wrap
Wrap the given primitive type into its wrapper type if it's a primitive type.- Parameters:
type
- given type- Returns:
- primitive wrapper type corresponding to
type
, ortype
iftype
is not a primitive type, or null iftype
is null
-
toString
-