Package org.spiderwiz.zutils
Class ZModInteger
java.lang.Object
org.spiderwiz.zutils.ZModInteger
- All Implemented Interfaces:
Comparable<ZModInteger>
public class ZModInteger extends Object implements Comparable<ZModInteger>
Represents an integer number with a defined modulo base.
-
Constructor Summary
Constructors Constructor Description ZModInteger(int base)
Constructs an object with the given modulo base and the initial value of zero.ZModInteger(int base, int value)
Constructs an object with the given modulo base and the given initial value. -
Method Summary
Modifier and Type Method Description int
add(int value)
Returns the modulo sum of the object and the parameter.int
compareTo(int value)
Modulo compares the object with the parameter.int
compareTo(ZModInteger o)
Compares this object to another modulo object.int
dec()
Decrements the object by 1 modulo the definedbase
.int
diff(int value)
Returns the modulo result of the subtraction of the object from the parameter.int
divide(int divisor)
Returns the modulo whole division of the object by the parameter.int
fromHex(String hex)
Assumes the parameter represents an hexadecimal integer value, converts it to an integer and returns the result modulo the object definedbase
.int
inc()
Increments the object by 1 modulo the definedbase
.int
max(int value)
Returns the modulo maximum between the object and the parameter.int
min(int value)
Returns the modulo minimum between the object and the parameter.int
multiply(int factor)
Returns the modulo multiplication of the object by the parameter.int
postDec()
Decrements the object by 1 modulo the definedbase
and returns the object value before the decrement.int
postInc()
Increments the object by 1 modulo the definedbase
and returns the object value before the increment.String
postIncAsHex()
Increments the object by 1 modulo the definedbase
and returns the object value before the increment as a hexadecimal value.int
setValue(int value)
Sets the value of the object to the given value modulo the object definedbase
.int
toInt()
Returns the object as an integer.String
toString()
Returns a string representation of the object.
-
Constructor Details
-
ZModInteger
public ZModInteger(int base)Constructs an object with the given modulo base and the initial value of zero.- Parameters:
base
- the modulo base.
-
ZModInteger
public ZModInteger(int base, int value)Constructs an object with the given modulo base and the given initial value.- Parameters:
base
- the modulo base.value
- the initial value.
-
-
Method Details
-
toInt
public final int toInt()Returns the object as an integer.- Returns:
- the object value as an integer.
-
setValue
public final int setValue(int value)Sets the value of the object to the given value modulo the object definedbase
.- Parameters:
value
- the value to set in the object after applying modulo calculation.- Returns:
- the set value.
-
add
public final int add(int value)Returns the modulo sum of the object and the parameter.- Parameters:
value
- the value to add.- Returns:
- the modulo sum of the object and the parameter.
-
diff
public final int diff(int value)Returns the modulo result of the subtraction of the object from the parameter.The modulo result is calculated as follows:
- The value of the object is subtracted from the parameter.
- Modulo calculation is applied on the result.
- If the absolute value of the result is less than half the modulo base then return the result as is.
- Otherwise return the negation of the result.
- Parameters:
value
- the value to subtract the object value from.- Returns:
- the modulo result of the subtraction of the object from the parameter.
-
multiply
public final int multiply(int factor)Returns the modulo multiplication of the object by the parameter.- Parameters:
factor
- the value to multiply by.- Returns:
- the modulo multiplication of the object by the parameter.
-
divide
public final int divide(int divisor)Returns the modulo whole division of the object by the parameter.- Parameters:
divisor
- the value to divide by.- Returns:
- the modulo whole division of the object by the parameter.
-
inc
public final int inc()Increments the object by 1 modulo the definedbase
.- Returns:
- the incremented value.
-
postInc
public final int postInc()Increments the object by 1 modulo the definedbase
and returns the object value before the increment.- Returns:
- the object value before the increment.
-
postIncAsHex
Increments the object by 1 modulo the definedbase
and returns the object value before the increment as a hexadecimal value.- Returns:
- the object value before the increment as a hexadecimal value.
-
dec
public final int dec()Decrements the object by 1 modulo the definedbase
.- Returns:
- the decremented value.
-
postDec
public final int postDec()Decrements the object by 1 modulo the definedbase
and returns the object value before the decrement.- Returns:
- the object value before the decrement.
-
compareTo
public final int compareTo(int value)Modulo compares the object with the parameter.The relation between the two values is determined as follows:
- The parameter is subtracted from the value of the object.
- Modulo calculation is applied on the result.
- If the absolute value of the result is less than half the modulo base then the modulo relation of the two values is identical to their arithmetic relation.
- Otherwise the modulo relation of the two values is the opposite of their arithmetic relation.
- Parameters:
value
- the value to compare the object to.- Returns:
- a negative integer, zero, or a positive integer as this object is modulo less than, equal to, or greater than the parameter.
-
compareTo
Compares this object to another modulo object.See
compareTo(int)
how to determine the relation between the values.- Specified by:
compareTo
in interfaceComparable<ZModInteger>
- Parameters:
o
- the object to compare to.- Returns:
- a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the parameter.
- See Also:
compareTo(int)
-
max
public final int max(int value)Returns the modulo maximum between the object and the parameter.See
compareTo(int)
how to determine the relation between the values.- Parameters:
value
- the value to compare with- Returns:
- the modulo maximum between the object and the parameter.
- See Also:
compareTo(int)
-
min
public final int min(int value)Returns the modulo minimum between the object and the parameter.See
compareTo(int)
how to determine the relation between the values.- Parameters:
value
- the value to compare with- Returns:
- the modulo minimum between the object and the parameter.
- See Also:
compareTo(int)
-
fromHex
Assumes the parameter represents an hexadecimal integer value, converts it to an integer and returns the result modulo the object definedbase
.- Parameters:
hex
- the value to convert.- Returns:
- the parameter converted to an integer modulo the object defined base.
-
toString
Returns a string representation of the object.
-