Java rotable integer


/**
 * A rotable integer is and infinite and comparable integer, which is
 * represented by a int primitive type. A rotable integer is:
 * - smaller than each integer between itself and
 * (itself + Integer.MAX_VALUE - window) mod Integer.MAX_VALUE;
 * - equals to itself;
 * - greater than each integer in the window, except itself.
 *
 * Rotable integers are useful for instance in protocol such as TCP (sequence
 * number).
 *
 * 0          window     value          Integer.MAX_VALUE
 * |----------------------[-----------]-----------------------------|
 * <<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>=<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 * (We should read value <op> <index>)
 *
 * @author Thibault Leruitte <thibault@leruitte.name>
 *
 */

public class RotableInteger implements Comparable<RotableInteger> {

  private final int window;
  private int value;

  /**
   * @param value The initial value. Must be greater or equal than 0.
   * @param window The size of the window. Must be greater than 0.
   */

  public RotableInteger(int value, int window) {
    setValue(value);
    if(window <= 0)
      throw new IllegalArgumentException("The window must be strictly " +
          "positive.");
    this.window = window;
  }

  /**
   * @return The size of the window.
   */

  public int getWindow() {
    return window;
  }

  /**
   * Set a value.
   * @param value Must be greather or equal than 0.
   */

  public void setValue(int value) {
    if(value < 0)
      throw new IllegalArgumentException("The value must be greater " +
          "than zero.");
    this.value = value;
  }
 
  /**
   * @return The value of this rotable integer.
   */

  public int getValue() {
    return value;
  }

  /**
   * Add one of this value.
   *
   * @return The new value.
   */

  public int increment() {
    value = (value != Integer.MAX_VALUE) ? ++value : 0;
    return value;
  }

  /**
   * Remove one of this value.
   *
   * @return The new value.
   */

  public int decrement() {
    value = (value != 0) ? --value : Integer.MAX_VALUE;
    return value;
  }

  @Override
  public boolean equals(Object obj) {
    if(obj instanceof RotableInteger) {
      RotableInteger rotable = (RotableInteger) obj;
      return value == rotable.value & window == rotable.value;
    }
    return false;
  }
 
  @Override
  public int compareTo(RotableInteger o) {
    if (o.window != this.window)
      throw new ClassCastException("The rotable integers must have the " +
          "same window.");
   
    if(value == o.value) {
      return 0;
    }
    else if(value - window >= 0) {
      // window is consecutive
      if(value - o.value > 0 & value - o.value < window) {
        // o is inside the window.
        return 1;
      }
      else {
        // o is outside the window.
        return -1;
      }
    }
    else {
      // window is split
      if(o.value < value
          | o.value > Integer.MAX_VALUE - window + 1 + value) {
        // o is inside the window.
        return 1;
      }
      else {
        // o is outside the window.
        return -1;
      }
    }
  }
}

Flux des posts et flux des commentaires.
Crédits