net.kano.joscar
Class SeqNum

java.lang.Object
  extended bynet.kano.joscar.SeqNum

public class SeqNum
extends java.lang.Object

Represents a sequence of numbers within a given range and allows for automatically-wrapping sequential traversal of such a sequence. This class has an internal value for the "current" value in the sequence, which is advanced with each call to next().

Example usage:

class CommandSender {
    // command ID's must be between 1 and 100 and each
    // must be greater than the last, unless wrapping
    // back to 1 from 100
    SeqNum cmdIdSeq = new SeqNum(1, 100);

    void sendCmd(Command cmd) {
        long cmdId = cmdIdSeq.next();
        System.out.println("Command #" + cmdId + ": "
                + cmd);
        reallySendCmd(cmd);
    }

    ...
}
 


Constructor Summary
SeqNum(long min, long max)
          Creates a new sequence with the given minimum and maximum values.
SeqNum(long min, long max, long current)
          Creates a new sequence with the given minimum and maximum values.
 
Method Summary
 long getLast()
          Returns the last value returned by next(), or the initial value if next() has not been called.
 long getMax()
          Returns the maximum value of an element of this sequence.
 long getMin()
          Returns the minimum value of an element of this sequence.
 long next()
          Returns the next element of this sequence.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SeqNum

public SeqNum(long min,
              long max)
Creates a new sequence with the given minimum and maximum values. Note that the given maximum value is an actual maximum value; that is, a value returned by next could be that value.

Parameters:
min - the smallest value allowed in this sequence
max - the largest value allowed in this sequence

SeqNum

public SeqNum(long min,
              long max,
              long current)
Creates a new sequence with the given minimum and maximum values. Note that the given maximum value is an actual maximum value; that is, a value returned by next could be that value.

Parameters:
min - the smallest value allowed in this sequence
max - the largest value allowed in this sequence
current - an initial value for the current value in this sequence
Throws:
java.lang.IllegalArgumentException - if the given initial value does not lie within the given range
Method Detail

getMin

public final long getMin()
Returns the minimum value of an element of this sequence.

Returns:
this sequence's inclusive lower bound

getMax

public final long getMax()
Returns the maximum value of an element of this sequence.

Returns:
this sequence's inclusive upper bound

getLast

public final long getLast()
Returns the last value returned by next(), or the initial value if next() has not been called.

Returns:
the last value returned by next()

next

public long next()
Returns the next element of this sequence. This method advances this class's "current" value of the sequence.

Returns:
the next element of this sequence, wrapping if necessary