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 defined base.
    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 defined base.
    int inc()
    Increments the object by 1 modulo the defined base.
    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 defined base and returns the object value before the decrement.
    int postInc()
    Increments the object by 1 modulo the defined base and returns the object value before the increment.
    String postIncAsHex()
    Increments the object by 1 modulo the defined base 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 defined base.
    int toInt()
    Returns the object as an integer.
    String toString()
    Returns a string representation of the object.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • 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 defined base.
      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:

      1. The value of the object is subtracted from the parameter.
      2. Modulo calculation is applied on the result.
      3. If the absolute value of the result is less than half the modulo base then return the result as is.
      4. 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 defined base.
      Returns:
      the incremented value.
    • postInc

      public final int postInc()
      Increments the object by 1 modulo the defined base and returns the object value before the increment.
      Returns:
      the object value before the increment.
    • postIncAsHex

      public final String postIncAsHex()
      Increments the object by 1 modulo the defined base 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 defined base.
      Returns:
      the decremented value.
    • postDec

      public final int postDec()
      Decrements the object by 1 modulo the defined base 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:

      1. The parameter is subtracted from the value of the object.
      2. Modulo calculation is applied on the result.
      3. 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.
      4. 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

      public final int compareTo​(ZModInteger o)
      Compares this object to another modulo object.

      See compareTo(int) how to determine the relation between the values.

      Specified by:
      compareTo in interface Comparable<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

      public final int fromHex​(String hex)
      Assumes the parameter represents an hexadecimal integer value, converts it to an integer and returns the result modulo the object defined base.
      Parameters:
      hex - the value to convert.
      Returns:
      the parameter converted to an integer modulo the object defined base.
    • toString

      public final String toString()
      Returns a string representation of the object.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the object.