public final class Nybble
extends Number
implements Comparable
Nybble
represent primitive
byte
values because this is the smallest primitive available
that can accomodate a nybble.
A nybble is a 4-bit value that can be represented entirely with any single hexadecimal digit (there are 16 unique digits in the hexadecimal numbering system). Hexadecimal number are typically represented in pairs (or multiples of pairs) of hexadecimal digits.
Note: The hexadecimal digits are: 0123456789ABCDEF
Additionally, this class provides various helper functions and variables useful in dealing with nybbles.
Modifier and Type | Field and Description |
---|---|
static byte |
MAX_VALUE
A constant holding the maximum value a
nybble can represent;
7 (or 23 -1). |
static byte |
MIN_VALUE
A constant holding the minimum value a
nybble can represent;
-8 (or -23). |
static int |
SIZE
The number of bits needed to represent a
nybble . |
static Class |
TYPE
The primitive type
byte is represented by this
Class object because Java doesn't include a primitive type for
nybble . |
static String |
VERSION
Version number of this Package (read-only).
|
Constructor and Description |
---|
Nybble(byte value)
Create a
Nybble object representing the value of the
byte argument. |
Nybble(int value)
Create a
Nybble object representing the value of the
int argument. |
Nybble(String s)
Create a
Nybble object representing the parsed value of the
String argument. |
Modifier and Type | Method and Description |
---|---|
byte |
byteValue()
Return the value of this
Nybble as a byte . |
static byte |
coerce(byte value)
Strips off excess bits (the sign will be preserved), if any, and returns
the remaining nybble presented as a byte.
|
static int |
coerce(int value)
Strips off excess bits (the sign will be preserved), if any, and returns
the remaining nybble presented as an integer.
|
int |
compareTo(Nybble n)
Compare two Nybbles numerically by comparing their
nybble
values. |
int |
compareTo(Object o)
Behaves like
compareTo(Nybble) unless the Object is not a
Nybble . |
static Nybble |
decode(String s)
Convert the specified
String into a Byte . |
double |
doubleValue()
Return the value of this
Nybble as a double . |
boolean |
equals(Object obj)
Returns
true if obj is an instance of
Nybble and represents the same nybble value. |
float |
floatValue()
Return the value of this
Nybble as a float . |
int |
hashCode()
Return a hashcode representing this Object.
|
int |
intValue()
Return the value of this
Nybble as an int eger. |
long |
longValue()
Return the value of this
Nybble as a long
integer. |
static byte |
parseNybble(String s)
Converts the specified
String into a nybble ,
presented as a byte because Java doesn't have a primitive nybble type. |
static byte |
parseNybble(String s,
int radix)
Converts the specified
String into a nybble ,
presented as a byte because Java doesn't have a primitive nybble type,
using the specified radix (base). |
short |
shortValue()
Return the value of this
Nybble as a short . |
String |
toString()
Converts the
Nybble value to a String and
assumes a radix of 10. |
static String |
toString(byte nybble)
Converts the
nybble to a String and assumes a
radix of 10. |
static Nybble |
valueOf(String s)
Creates a new
Nybble object using the String
and assuming a radix of 10. |
static Nybble |
valueOf(String s,
int radix)
Creates a new
Nybble object using the String
and specified radix (base). |
public static final byte MAX_VALUE
nybble
can represent;
7 (or 23 -1).public static final byte MIN_VALUE
nybble
can represent;
-8 (or -23).public static final int SIZE
nybble
.public static final Class TYPE
byte
is represented by this
Class
object because Java doesn't include a primitive type for
nybble
.public static final String VERSION
public Nybble(byte value)
Nybble
object representing the value of the
byte
argument.value
- the value to useNumberFormatException
- if value
is out of rangepublic Nybble(int value)
Nybble
object representing the value of the
int
argument.value
- the value to useNumberFormatException
- if value
is out of rangepublic Nybble(String s)
Nybble
object representing the parsed value of the
String
argument. If the value is out of range, or can't be
parsed, then an exception will be thrown.s
- the string to convertNumberFormatException
- if the string does not contain a NybblevalueOf(String)
public byte byteValue()
Nybble
as a byte
.byteValue
in class Number
public static byte coerce(byte value)
value
- The byte valuepublic static int coerce(int value)
value
- The integer valuepublic int compareTo(Nybble n)
nybble
values. The result is positive if the first is greater, negative if the
second is greater, and 0 if the two are equal.n
- the Nybble to comparepublic int compareTo(Object o)
compareTo(Nybble)
unless the Object is not a
Nybble
. The result is positive if the first is greater,
negative if the second is greater, and 0 if the two are equal.compareTo
in interface Comparable
o
- the Object to compareClassCastException
- if the argument is not a
Nybble
public static Nybble decode(String s)
String
into a Byte
. The
String
may represent decimal, hexadecimal, or octal numbers.
The extended BNF grammar is as follows:
DecodableString: ( [Finally, the value must be in the range-
] DecimalNumber ) | ( [-
] (0x
|0X
|#
) { HexDigit }+ ) | ( [-
]0
{ OctalDigit } ) DecimalNumber: DecimalDigit except '0' { DecimalDigit } DecimalDigit: Character.digit(d, 10) has value 0 to 9 OctalDigit: Character.digit(d, 8) has value 0 to 7 DecimalDigit: Character.digit(d, 16) has value 0 to 15
MIN_VALUE
to
MAX_VALUE
, or an exception is thrown.s
- the String
to interpretNybble
represented as a
Byte
because Java doesn't have a primitive nybble typeNumberFormatException
- if s
cannot be parsed as a
nybble
NullPointerException
- if s
is nullInteger.decode(String)
public double doubleValue()
Nybble
as a double
.doubleValue
in class Number
public boolean equals(Object obj)
true
if obj
is an instance of
Nybble
and represents the same nybble value.equals
in class Object
obj
- the object to comparepublic float floatValue()
Nybble
as a float
.floatValue
in class Number
public int hashCode()
Nybble
's hash
code is simply its value.hashCode
in class Object
public int intValue()
Nybble
as an int
eger.intValue
in class Number
public long longValue()
Nybble
as a long
integer.longValue
in class Number
public static byte parseNybble(String s)
String
into a nybble
,
presented as a byte because Java doesn't have a primitive nybble type.
This function assumes a radix of 10.s
- the String
to convertString
argument converted to a nybble
,
represented as a byte
NumberFormatException
- if s
cannot be parsed as a
nybble
public static byte parseNybble(String s, int radix)
String
into a nybble
,
presented as a byte because Java doesn't have a primitive nybble type,
using the specified radix (base). The string must not be null
or empty. It may begin with an optional minus sign ("-"), which will
negate the answer, provided that there are also valid digits. Each digit
is parsed as if by Character.digit(d, radix)
, and must be in
the range 0
to radix - 1
. Finally, the result
must be within MIN_VALUE
to MAX_VALUE
, inclusive.
Unlike Double.parseDouble, you may not have a leading plus sign ("+").s
- the String
to convertradix
- the radix (base) to use in the conversionString
argument converted to a nybble
,
represented as a byte
NumberFormatException
- if s
cannot be parsed as a
nybble
public short shortValue()
Nybble
as a short
.shortValue
in class Number
public String toString()
Nybble
value to a String
and
assumes a radix of 10.toString
in class Object
String
representation of this Nybble
.Integer.toString()
public static String toString(byte nybble)
nybble
to a String
and assumes a
radix of 10.nybble
- the byte
representing a nybble to convert to
String
String
representation of the argumentNumberFormatException
- if value
is out of rangepublic static Nybble valueOf(String s)
Nybble
object using the String
and assuming a radix of 10.s
- the String
to convertNybble
NumberFormatException
- if s
cannot be parsed as a
nybble
Nybble(String)
,
parseNybble(String, int)
public static Nybble valueOf(String s, int radix)
Nybble
object using the String
and specified radix (base).s
- the String
to convertradix
- the radix (base) to convert withNybble
NumberFormatException
- if s
cannot be parsed as a
nybble
parseNybble(String, int)