LimeWire Collection Component API

org.limewire.collection
Class IntervalSet

java.lang.Object
  extended by org.limewire.collection.IntervalSet
All Implemented Interfaces:
Serializable, Iterable<Range>

public class IntervalSet
extends Object
implements Iterable<Range>, Serializable

Provides an interval of ranges (a "range" version of IntSet).

    IntervalSet is = new IntervalSet();

    is.add(Range.createRange(1,4));
    is.add(Range.createRange(11,14));
    is.add(Range.createRange(21,24));
    System.out.println("Set is " + is + " intervals: " + 
                is.getNumberOfIntervals());

    is.add(Range.createRange(5,10));
    System.out.println("Set is " + is + " intervals: " + 
                is.getNumberOfIntervals());
    IntervalSet is2 = is.invert(50);
    System.out.println("Set is " + is2 + " intervals: " + 
                is2.getNumberOfIntervals());

    Output:
        Set is [1-4, 11-14, 21-24] intervals: 3
        Set is [1-14, 21-24] intervals: 2
        Set is [0, 15-20, 25-49] intervals: 3

See Also:
Serialized Form

Nested Class Summary
static class IntervalSet.ByteIntervals
          Allows you to keep int and long intervals in the same location.
 
Constructor Summary
IntervalSet()
           
IntervalSet(Range interval)
          Creates an interval set with the given base range.
 
Method Summary
 void add(IntervalSet set)
          Adds a whole IntervalSet into this IntervalSet.
 void add(Range addInterval)
           
 void clear()
           
 IntervalSet clone()
          Clones the IntervalSet.
 boolean contains(Range i)
           
 boolean containsAny(Range i)
           
static IntervalSet createSingletonSet(long lowBound, long highBound)
          Creates an interval set representing a single Interval.
 void decode(long maxSize, Integer... id)
          decodes an interval set encoded with: http://www.limewire.org/wiki/index.php?title=HashTreeRangeEncoding
 void delete(IntervalSet set)
          Deletes all intervals in the specified set from this set.
 void delete(Range deleteMe)
          Deletes any overlap of existing intervals with the Interval to delete.
 Collection<Integer> encode(long maxSize)
          encodes the current interval set as defined in http://www.limewire.org/wiki/index.php?title=HashTreeRangeEncoding
 boolean equals(Object o)
          Compares two IntervalSets
 Iterator<Range> getAllIntervals()
           
 List<Range> getAllIntervalsAsList()
           
 Range getFirst()
          Returns the first element without modifying this IntervalSet.
 Range getLast()
          Returns the last element without modifying this IntervalSet.
 Iterator<Range> getNeededIntervals(long maxSize)
           
 int getNumberOfIntervals()
           
 List<Range> getOverlapIntervals(Range checkInterval)
           
 long getSize()
           
 IntervalSet invert(long maxSize)
          Creates an IntervalSet that is the negative to this IntervalSet.
 boolean isEmpty()
           
 Iterator<Range> iterator()
           
static IntervalSet parseBytes(byte[] ranges, byte[] ranges5)
          Parses an IntervalSet from a byte array.
 IntervalSet.ByteIntervals toBytes()
           
 String toString()
          Lists the contained intervals.
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntervalSet

public IntervalSet()

IntervalSet

public IntervalSet(Range interval)
Creates an interval set with the given base range.

Parameters:
interval - - range to create the interval set with.
Method Detail

createSingletonSet

public static IntervalSet createSingletonSet(long lowBound,
                                             long highBound)
Creates an interval set representing a single Interval.

Parameters:
lowBound - the lower bound of the represented Interval
highBound - the upper bound of the represented Interval
Returns:
an IntervalSet representing the range lowBound to highBound, inclusive.

add

public void add(Range addInterval)

add

public void add(IntervalSet set)
Adds a whole IntervalSet into this IntervalSet.


delete

public void delete(Range deleteMe)
Deletes any overlap of existing intervals with the Interval to delete.

Parameters:
deleteMe - the Interval that should be deleted.

delete

public void delete(IntervalSet set)
Deletes all intervals in the specified set from this set.


getFirst

public Range getFirst()
               throws NoSuchElementException
Returns the first element without modifying this IntervalSet.

Throws:
NoSuchElementException - if no intervals exist.
NoSuchElementException

getLast

public Range getLast()
              throws NoSuchElementException
Returns the last element without modifying this IntervalSet.

Throws:
NoSuchElementException - if no intervals exist.
NoSuchElementException

getNumberOfIntervals

public int getNumberOfIntervals()
Returns:
The number of Intervals in this IntervalSet.

contains

public boolean contains(Range i)
Returns:
Whether this interval set contains fully the given interval.

containsAny

public boolean containsAny(Range i)
Returns:
whether this interval set contains any part of the given interval.

getOverlapIntervals

public List<Range> getOverlapIntervals(Range checkInterval)
Returns:
a List of intervals that overlap checkInterval. For example if Intervals contains{[1-4],[6-10]} and checkInterval is [3-8], this method returns a list of 2 intervals {[3-4],[6-8]}. If there are no overlaps, this method returns an empty List.

getAllIntervals

public Iterator<Range> getAllIntervals()

iterator

public Iterator<Range> iterator()
Specified by:
iterator in interface Iterable<Range>

getAllIntervalsAsList

public List<Range> getAllIntervalsAsList()

getSize

public long getSize()

isEmpty

public boolean isEmpty()

clear

public void clear()

encode

public Collection<Integer> encode(long maxSize)
encodes the current interval set as defined in http://www.limewire.org/wiki/index.php?title=HashTreeRangeEncoding


decode

public void decode(long maxSize,
                   Integer... id)
decodes an interval set encoded with: http://www.limewire.org/wiki/index.php?title=HashTreeRangeEncoding

Parameters:
maxSize - the size of the file
id - integers from the encoding

invert

public IntervalSet invert(long maxSize)
Creates an IntervalSet that is the negative to this IntervalSet.

Returns:
IntervalSet containing all ranges not contained in this

getNeededIntervals

public Iterator<Range> getNeededIntervals(long maxSize)
Returns:
An iterator or intervals needed to fill in the holes in this IntervalSet. Note that the IntervalSet does not know the maximum value of all the intervals.

clone

public IntervalSet clone()
                  throws CloneNotSupportedException
Clones the IntervalSet. The underlying intervals are the same (so they should never be modified), but the TreeSet this is backed off of is new.

Overrides:
clone in class Object
Throws:
CloneNotSupportedException

toString

public String toString()
Lists the contained intervals.

Overrides:
toString in class Object

equals

public boolean equals(Object o)
Compares two IntervalSets

Overrides:
equals in class Object

toBytes

public IntervalSet.ByteIntervals toBytes()
Returns:
packed representation of the intervals. at position 0 are all intervals that can fit in 31 bit representation, at position 1 are the long ones (currently 40 bits).

parseBytes

public static IntervalSet parseBytes(byte[] ranges,
                                     byte[] ranges5)
                              throws IOException
Parses an IntervalSet from a byte array. At position 0 are intervals that fit in 31 bits, at position 1 are those that need 40 bits.

Throws:
IOException

LimeWire Collection Component API

Copyright © 2009 Lime Wire LLC. All Rights Reserved.