add Range.overlaps(Range) method
Seems very strange that the Range class doesn't have an overlaps(Range) or intersects(Range) method.
I expect it would essentially be
public boolean overlaps(Range other) {
return isConnected(other) && !intersection(other).isEmpty();
}
This is difficult without a DiscreteDomain. Your current implementation would treat [0, 1) and (0, 1] as overlapping, even though there aren't actually any integers in their intersection.
I see 32 hits in Google code for isConnected[^;{}]*intersection[^;{}]*isEmpty (PCRE:yes, exclude com/google/common/collect). I wonder if these are worth a look, either as evidence for the usefulness of the method or evidence that the approach is bug-prone.
I've made this call several times in my own code when dealing with ranges. Ranges that are part of a DiscreteDomain deserve special consideration. As this method is mostly a convenience to save code and increase readability, perhaps two versions of the method, one for DiscreteDomain Ranges, and one for non-discrete? The method for DiscreteDomain ranges could canonicalize the ranges sent in and the caller would get their expected result. The non-discrete method could do the connected/intersection call as-is. Otherwise, a single method with a clear API documentation would be nice as well. Either way, I think it's a method that would see some use and I would love to see it added.
Would be very useful! We're using isConnected(other) && !intersection(other).isEmpty() too (for Range<LocalDateTime>)
FYI This is how PostgreSQL compares ranges
https://github.com/postgres/postgres/blob/a77315fdf2a197a925e670be2d8b376c4ac02efc/src/backend/utils/adt/rangetypes.c#L823-L854
Is there any plan to implement overlap method on Range?
so, is it done now? if not, i'm current coming into the same problem and very glad to contribute my implementation @cpovirk