Class BoolSemiring
- All Implemented Interfaces:
Serializable
,Comparable<BoolSemiring>
,Semiring<BoolSemiring>
This class wraps a primitive boolean
value and provides operations consistent with a boolean semiring.
In this semiring:
- Addition is defined as logical OR (
a + b = a || b
). - Multiplication is defined as logical AND (
a * b = a && b
). - Additive Identity is
false
(denoted byZERO
orFALSE
). - Multiplicative Identity is
true
(denoted byONE
orTRUE
).
The class implements the Semiring
interface and adheres to its contract.
Instances of BoolSemiring
are immutable and thread-safe.
Usage Example:
BoolSemiring a = BoolSemiring.TRUE; // Equivalent to new BoolSemiring(true)
BoolSemiring b = BoolSemiring.FALSE; // Equivalent to new BoolSemiring(false)
BoolSemiring sum = a.add(b); // Logical OR: true || false => true
BoolSemiring product = a.mult(b); // Logical AND: true && false => false
BoolSemiring notA = a.not(); // Logical NOT: !true => false
Constants:
The class provides constants for common boolean values:
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final BoolSemiring
The boolean value false.static final BoolSemiring
The boolean value true.static final BoolSemiring
The boolean value true.static final BoolSemiring
The boolean value false. -
Constructor Summary
ConstructorsConstructorDescriptionBoolSemiring
(boolean value) Constructs aBoolSemiring
semiring element with the specified boolean value.BoolSemiring
(int value) Constructs aBoolSemiring
semiring element from an integer. -
Method Summary
Modifier and TypeMethodDescriptionadd
(BoolSemiring b) Performs the addition operation of this semiring, defined as logical OR.and
(BoolSemiring b) Computes the logical AND of thisBoolSemiring
with another.int
Compares this element of the semiring withb
.double
Converts this semiring value to an equivalent double value.boolean
Checks if an object is equal to this semiring element.getOne()
Gets the multiplicative identity for this semiring.boolean
getValue()
Gets the value of this semiring element.getZero()
Gets the additive identity for this semiring.int
hashCode()
boolean
isOne()
Checks if this value is a multiplicative identity for this semiring.boolean
isZero()
Checks if this value is an additive identity for this semiring.mult
(BoolSemiring b) Performs the multiplication operation of this semiring, defined as logical AND.not()
Computes the logical NOT (negation) of thisBoolSemiring
.or
(BoolSemiring b) Computes the logical OR of thisBoolSemiring
with another.xor
(BoolSemiring b) Computes the exclusive OR (XOR) of thisBoolSemiring
with another.
-
Field Details
-
ONE
The boolean value true. -
TRUE
The boolean value true. -
ZERO
The boolean value false. -
FALSE
The boolean value false.
-
-
Constructor Details
-
BoolSemiring
public BoolSemiring(boolean value) Constructs aBoolSemiring
semiring element with the specified boolean value.- Parameters:
value
- the boolean value to wrap.
-
BoolSemiring
public BoolSemiring(int value) Constructs aBoolSemiring
semiring element from an integer.- Parameters:
value
- the integer value (must be 0 or 1).- Throws:
IllegalArgumentException
- if the value is not 0 or 1.
-
-
Method Details
-
add
Performs the addition operation of this semiring, defined as logical OR.
This operation is associative and commutative.
- Specified by:
add
in interfaceSemiring<BoolSemiring>
- Parameters:
b
- the second semiring element to add.- Returns:
- A new
BoolSemiring
representing the logical OR of this value andb
.
-
or
Computes the logical OR of this
BoolSemiring
with another.This method is equivalent to
add(BoolSemiring)
.- Parameters:
b
- theBoolSemiring
to perform the logical OR with.- Returns:
- A new
BoolSemiring
representing the logical OR of this value andb
.
-
xor
Computes the exclusive OR (XOR) of thisBoolSemiring
with another.- Parameters:
b
- theBoolSemiring
to perform the XOR with.- Returns:
- A new
BoolSemiring
representing the XOR of this value andb
.
-
mult
Performs the multiplication operation of this semiring, defined as logical AND.
This operation is associative.
- Specified by:
mult
in interfaceSemiring<BoolSemiring>
- Parameters:
b
- the second semiring element to multiply.- Returns:
- A new
BoolSemiring
representing the logical AND of this value andb
.
-
and
Computes the logical AND of this
BoolSemiring
with another.This method is equivalent to
mult(BoolSemiring)
.- Parameters:
b
- theBoolSemiring
to perform the logical AND with.- Returns:
- A new
BoolSemiring
representing the logical AND of this value andb
.
-
not
Computes the logical NOT (negation) of thisBoolSemiring
.- Returns:
- a new
BoolSemiring
representing the logical NOT of this value.
-
isZero
public boolean isZero()Checks if this value is an additive identity for this semiring.
An element 0 is an additive identity if a + 0 = a for any a in the semiring.
- Specified by:
isZero
in interfaceSemiring<BoolSemiring>
- Returns:
true
if this value is an additive identity for this semiring;false
otherwise.
-
isOne
public boolean isOne()Checks if this value is a multiplicative identity for this semiring.
An element 1 is a multiplicative identity if a * 1 = a for any a in the semiring.
- Specified by:
isOne
in interfaceSemiring<BoolSemiring>
- Returns:
true
if this value is a multiplicative identity for this semiring;false
otherwise.
-
getValue
public boolean getValue()Gets the value of this semiring element.- Returns:
- The value of this semiring element.
-
getZero
Gets the additive identity for this semiring.
An element 0 is an additive identity if a + 0 = a for any a in the semiring.
- Specified by:
getZero
in interfaceSemiring<BoolSemiring>
- Returns:
- The additive identity for this semiring.
-
getOne
Gets the multiplicative identity for this semiring.
An element 1 is a multiplicative identity if a * 1 = a for any a in the semiring.
- Specified by:
getOne
in interfaceSemiring<BoolSemiring>
- Returns:
- The multiplicative identity for this semiring.
-
compareTo
Compares this element of the semiring withb
.- Specified by:
compareTo
in interfaceComparable<BoolSemiring>
- Specified by:
compareTo
in interfaceSemiring<BoolSemiring>
- Parameters:
b
- Second element of the semiring.- Returns:
- An int value:
- 0 if this semiring element is equal to
b
. - invalid input: '<' 0 if this semiring element is less than
b
. - > 0 if this semiring element is greater than
b
.
Hence, this method returns zero if and only if the two semiring elements are equal, a negative value if and only the semiring
element it was called on is less than
b
and positive if and only if the semiring element it was called on is greater thanb
. - 0 if this semiring element is equal to
-
doubleValue
public double doubleValue()Converts this semiring value to an equivalent double value.- Specified by:
doubleValue
in interfaceSemiring<BoolSemiring>
- Returns:
- A double value equivalent to this semiring element.
-
equals
Checks if an object is equal to this semiring element.- Overrides:
equals
in classObject
- Parameters:
b
- Object to compare to this semiring element.- Returns:
- True if the objects are the same or are both
BoolSemiring
's and have equal values.
-
hashCode
-