public class Tristate extends Object
Tristate
is a class that is essentially an extension of the
Boolean
class that incorporates a third state. This third
state is called undefined and can be used in any case where the truthfulness
of something is undefined. This class is immutable and does not have any
public constructors. To get a Tristate
, use one of the
constants, or the forState()
function.
Copyright: Copyright 2003-2014, Knowledge Science Group, University of Calgary. Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation. The Knowledge Science Group makes no representations about the suitability of this software for any purpose. It is provided "as is" without express or implied warranty.
Modifier and Type | Field and Description |
---|---|
static Tristate |
FALSE
A static
Tristate that is in the false state. |
private static int |
FALSE_INT
The
int representing the false state. |
private int |
state
The
int that represents the state of this object. |
static Tristate |
TRUE
A static
Tristate that is in the truthful state. |
private static int |
TRUE_INT
The
int representing the truthful state. |
static Tristate |
UNDEFINED
A static
Tristate that is in the undefined state. |
private static int |
UNDEFINED_INT
The
int representing the undefined state. |
Modifier | Constructor and Description |
---|---|
private |
Tristate(int state)
Creates a new
Tristate object with the given
int state. |
|
Tristate(String val)
Creates a Tristate based on the String parameter
|
Modifier and Type | Method and Description |
---|---|
boolean |
booleanValue()
Determines the objects boolean value.
|
boolean |
booleanValue(boolean defaultValue)
Determines the objects boolean value, returning a default value if it is
undefined.
|
boolean |
equals(Object object)
Determines if the current
Tristate object is equal to the
given object. |
static Tristate |
forState(boolean state)
Returns the
Tristate object with the given boolean state. |
private int |
getState()
Retrieves the current state of the object as an
int . |
int |
hashCode()
Returns the hash code for this
Tristate object. |
boolean |
isDefined()
Determines whether the object has a defined state.
|
Tristate |
negation()
Returns the
Tristate object with the negation of this
object's boolean state. |
private void |
setState(int state)
Sets the state of the object to the given state.
|
String |
toString()
Retrieves the current state of the object as an
String . |
private int state
int
that represents the state of this object. It must
always be one of FALSE_INT
, TRUE_INT
, or
UNDEFINED_INT
.private static final int FALSE_INT
int
representing the false state.public static final Tristate FALSE
Tristate
that is in the false state.private static final int TRUE_INT
int
representing the truthful state.public static final Tristate TRUE
Tristate
that is in the truthful state.private static final int UNDEFINED_INT
int
representing the undefined state.public static final Tristate UNDEFINED
Tristate
that is in the undefined state.private Tristate(int state)
Tristate
object with the given
int
state. If state does not equal FALSE_INT
or TRUE_INT
, the state is set to
UNDEFINED_INT
.state
- The initial state of the new Tristate
object
as an int
.public Tristate(String val)
val
- "true" for a true state, "false" for a false state, otherwise an undefined state.public static Tristate forState(boolean state)
Tristate
object with the given boolean state.
This method will never return a Tristate
object that is
undefined.state
- The state of the Tristate
object that should
be returned as a boolean
.Tristate
object with the given truth value.public Tristate negation()
Tristate
object with the negation of this
object's boolean state. This method will return a Tristate
object that is undefined if this object is undefined.Tristate
object with the negation of this
object's truth value.private void setState(int state)
FALSE
or TRUE
, the state is set to
UNDEFINED
.state
- The state that the object should be set to.private int getState()
int
. This
will return FALSE_INT
if the state is false,
TRUE_INT
if the state is true, or
UNDEFINED_INT
if the state is undefined.FALSE_INT
if the state is false,
TRUE_INT
if the state is true, or
UNDEFINED_INT
if the state is undefined.public boolean isDefined()
true
if the state is not undefined; false
otherwise.true
if the state is not undefined;
false
otherwise.public boolean booleanValue() throws RuntimeException
true
if the
state is true; false
if the state is false. It will throw
an exception if the state is undefined.true
if the state is true; false
if
it is false.RuntimeException
- if the state is undefined.public boolean booleanValue(boolean defaultValue)
true
if the state is true,
false
if the state is false, or the given default value if
the state is undefined.defaultValue
- The boolean
value that should be
returned if the object's state is undefined.true
if the state is true, false
if
it is false, or the defaultValue if it is undefined.public boolean equals(Object object)
Tristate
object is equal to the
given object. This returns true
if the object is a
Tristate
or a Boolean
object in the same
state; false
otherwise.public String toString()
String
.
This will return "false"
if the state is false,
"true"
if the state is true, or "undefined"
if the state is undefined.public int hashCode()
Tristate
object. Returns
the hash code for Boolean.FALSE
if this object's state is
false, the hash code for Boolean.TRUE
if this object's
state is true, or the int
1225 if this object's state is
undefined.