3

I am working with Guava's Range class for processing intervals. I wanted to know if it is possible to find the closest interval from a set of intervals to a given point/interval by using some of the Guava's collection containers ?

I tried searching for interval trees in Java and here is what I found. I would prefer to do it by using one of the Guava classes if possible.

http://picard.sourceforge.net/javadoc/net/sf/picard/util/IntervalTree.html http://tribble.googlecode.com/svn/trunk/src/org/broad/tribble/index/interval/IntervalTree.java

Thanks

user1998031
  • 127
  • 2
  • 7
  • Can you explain what "the closest interval" is for a point? For an interval? I don't understand how you can have a closest interval for a point. A point is either in a Range or not? Is "the closest interval" the interval which contains a point and the smallest difference between the point and the Range's endpoints? – EdH Mar 01 '13 at 03:56
  • For example is we have the following intervals [1, 10], [15, 20] then [15, 20] would be closest interval to the point 14 and [1, 10] would be the closest interval to 12 – user1998031 Mar 01 '13 at 04:11

1 Answers1

3

Guava doesn't provide this, though you might be able to build such a thing on top of a RangeSet by finding the first range before and after a given point.

But generally, Guava Ranges know nothing about distances, metrics, or anything except the comparison ordering of a type. They don't know that 10 is closer to 11 than 15.

Mohan Kumar
  • 621
  • 1
  • 8
  • 25
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Finding the first range before and after a given point should do the trick. How do I accomplish it in RangeSet ? Do I make a pseudo border around the point and search for overlaps, or is there some direct way ? – user1998031 Mar 01 '13 at 15:46
  • 1
    `RangeSet.subRangeSet(Range.greaterThan(point)).span().lowerEndpoint()` should give you the endpoint of the first range to the right of the point. – Louis Wasserman Mar 01 '13 at 17:06
  • Glancing at `RangeSet`, I'm curious why `C` is bounded by raw `Comparable` rather than `Comparable super C>`. **Edit:** I guess I should ask that about `Range` instead. – Paul Bellora Mar 02 '13 at 03:25
  • Ah just found the note about supporting "ungenerified (pre-JDK1.5) data types" for now - understood. – Paul Bellora Mar 02 '13 at 03:32