org.blinkenbyte.io
Class BlockRange

java.lang.Object
  extended by org.blinkenbyte.io.BlockRange
All Implemented Interfaces:
java.lang.Comparable<BlockRange>

public class BlockRange
extends java.lang.Object
implements java.lang.Comparable<BlockRange>

This class represents a mutable block range, specified by a starting block number, and the length of the run. It provides methods to combine and remove blocks individually and as a list, and to read and write lists from a Reader/Writer.


Constructor Summary
BlockRange(long _blockNumber, long _blockLength)
          Constructs a new BlockRange.
 
Method Summary
 void absorb(BlockRange bp)
          Extends the range of this BlockRange to include the specified block.
static void coalesce(java.util.List<BlockRange> list)
          Coalesces a list of BlockRange objects.
 int compareTo(BlockRange bp)
          Compares the starting block of this BlockRange to the specified BlockRange.
 int compareToBlockNumber(long n)
          Compares a block number to this range.
 boolean contains(BlockRange bp)
          Returns true if the specified BlockRange is fully contained within this BlockRange.
 long getBlockLength()
          Returns the number of blocks represented by this object.
 long getBlockNumber()
          Returns the starting block number represented by this object.
 long getEndBlockNumber()
          Returns the ending block number encompassed by this range.
 java.lang.String getLine()
          Returns a string representation of this BlockRange as it would be read or written from a Reader/Writer.
 boolean isBlockInRange(long n)
          Returns whether a given block number is in the range covered by this object.
 boolean isContiguousWith(BlockRange bp)
          Returns true if the union of this BlockRange with the specified BlockRange is a single continuous block.
 boolean isOverlapping(BlockRange bp)
          Returns true if the intersection of this BlockRange with the specified BlockRange is not empty.
static java.util.ArrayList<BlockRange> readList(java.io.Reader r)
          Reads a list of block ranges in a format that can be read by readList.
static void readList(java.io.Reader r, java.util.List<BlockRange> list)
          Reads a list of block ranges in a format that can be read by readList, and appends them to the specified list.
 BlockRange[] remove(BlockRange bp)
          Removes a range of blocks from this BlockRange, and returns the resulting BlockRange(s).
 java.lang.String toString()
          Returns a string representation of this BlockRange
 BlockRange union(BlockRange bp)
          Returns a new block that represents a union of this BlockRange and the specified BlockRange.
static void writeList(java.util.List<BlockRange> list, java.io.Writer w)
          Writes out a list of blocks in a format that can be read by readList.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BlockRange

public BlockRange(long _blockNumber,
                  long _blockLength)
Constructs a new BlockRange.

Parameters:
_blockNumber - The starting block number.
_blockLength - The number of blocks.
Method Detail

getBlockNumber

public long getBlockNumber()
Returns the starting block number represented by this object.


getBlockLength

public long getBlockLength()
Returns the number of blocks represented by this object.


getEndBlockNumber

public long getEndBlockNumber()
Returns the ending block number encompassed by this range. This is one block past the last block covered by this object.


isBlockInRange

public boolean isBlockInRange(long n)
Returns whether a given block number is in the range covered by this object.


compareToBlockNumber

public int compareToBlockNumber(long n)
Compares a block number to this range.

Returns:
Returns 1 if the block number is past the range of this object.
Returns 0 if the block number is within the range of this object.
Returns -1 if the block number is before the range of this object.

contains

public boolean contains(BlockRange bp)
Returns true if the specified BlockRange is fully contained within this BlockRange.


isContiguousWith

public boolean isContiguousWith(BlockRange bp)
Returns true if the union of this BlockRange with the specified BlockRange is a single continuous block.


isOverlapping

public boolean isOverlapping(BlockRange bp)
Returns true if the intersection of this BlockRange with the specified BlockRange is not empty.


absorb

public void absorb(BlockRange bp)
Extends the range of this BlockRange to include the specified block.


union

public BlockRange union(BlockRange bp)
Returns a new block that represents a union of this BlockRange and the specified BlockRange. If the blocks are not contiguous, all blocks between the two blocks are included in the union.


remove

public BlockRange[] remove(BlockRange bp)
Removes a range of blocks from this BlockRange, and returns the resulting BlockRange(s). If the ranges do not overlap, the result is undefined.

Returns:
Returns the result of the block removal. This may be a null if the specified BlockRange contains this BlockRange. Otherwise, an array of one or two BlockRange objects will be returned.

compareTo

public int compareTo(BlockRange bp)
Compares the starting block of this BlockRange to the specified BlockRange.

Specified by:
compareTo in interface java.lang.Comparable<BlockRange>

toString

public java.lang.String toString()
Returns a string representation of this BlockRange

Overrides:
toString in class java.lang.Object

getLine

public java.lang.String getLine()
Returns a string representation of this BlockRange as it would be read or written from a Reader/Writer. This is the block number, followed by a space, and followed by a block count.


coalesce

public static void coalesce(java.util.List<BlockRange> list)
Coalesces a list of BlockRange objects. Any contiguous or overlapping blocks will be combined. This method may modify existing ranges rather than create new ones.


writeList

public static void writeList(java.util.List<BlockRange> list,
                             java.io.Writer w)
                      throws java.io.IOException
Writes out a list of blocks in a format that can be read by readList. One BlockRange is written per line, using the result of getLine().

Throws:
java.io.IOException

readList

public static java.util.ArrayList<BlockRange> readList(java.io.Reader r)
                                                throws java.lang.Exception
Reads a list of block ranges in a format that can be read by readList. One BlockRange is written per line, using the result of getLine().

Returns:
A list of BlockRange objects
Throws:
java.lang.Exception

readList

public static void readList(java.io.Reader r,
                            java.util.List<BlockRange> list)
                     throws java.lang.Exception
Reads a list of block ranges in a format that can be read by readList, and appends them to the specified list. One BlockRange is written per line, using the result of getLine().

Throws:
java.lang.Exception