dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

assert Ranges.closed(1L, 2L).encloses(Ranges.closedOpen(1L, 3L)) fails; #1153

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It seems to me that for a discrete domain where a < b, and c immediately 
follows b, then the following should hold:

Ranges.closed(a, b).equals(Ranges.closedOpen(a, c) == true

Or maybe I'm missing something.

Original issue reported on code.google.com by lopez.ju...@gmail.com on 21 Sep 2012 at 12:22

GoogleCodeExporter commented 9 years ago
This is 13.0.1

Original comment by lopez.ju...@gmail.com on 21 Sep 2012 at 12:23

GoogleCodeExporter commented 9 years ago
It sounds like you're thinking of this as "if two ranges contain the same sets 
of elements in the discrete domain, they're equal". This just isn't the case, 
though, and the methods (including equals and encloses) make that pretty clear 
I think. Part of the reason for that is that a Range isn't aware of the 
discrete domain. The two ranges in your example will behave differently for 
certain operations, so they aren't equal.

ContiguousSet has the behavior that it sounds like you want. A ContiguousSet is 
aware of the DiscreteDomain and as such knows exactly what elements in the 
domain it contains. So the following should hold:

ContiguousSet.create(Range.closed(1L, 2L), DiscreteDomains.longs()).containsAll(
    ContiguousSet.create(Range.open(1L, 3L), DiscreteDomains.longs())) == true

ContigousSet.create(Range.closed(a, b), domain).equals(
    ContiguousSet.create(Range.closedOpen(a, c), domain)) == true

Original comment by cgdec...@gmail.com on 21 Sep 2012 at 1:18

GoogleCodeExporter commented 9 years ago
You had me worried that our docs don't cover this, but it looks like they do :) 
 Range.equals says:

"Returns true if object is a range having the same endpoints and bound types as 
this range. Note that discrete ranges such as (1..4) and [2..3] are not equal 
to one another, despite the fact that they each contain precisely the same set 
of values. Similarly, empty ranges are not equal unless they have exactly the 
same representation, so [3..3), (3..3], (4..4] are all unequal."

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect
/Range.html#equals(java.lang.Object)

Is there anything else that it would be helpful to include?

Original comment by cpov...@google.com on 21 Sep 2012 at 1:18

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 21 Sep 2012 at 1:19

GoogleCodeExporter commented 9 years ago
Thanks. My bad.

Original comment by lopez.ju...@gmail.com on 21 Sep 2012 at 1:23

GoogleCodeExporter commented 9 years ago
Also, Range.canonical(DiscreteDomain) will convert Ranges.closed(a, b) and 
Ranges.closedOpen(a, c) to the same output -- and those two *will* be equal 
according to Range.equals().

Original comment by wasserman.louis on 21 Sep 2012 at 2:22

GoogleCodeExporter commented 9 years ago
I see.  Interesting.  Thanks, this is really helpful.

Original comment by lopez.ju...@gmail.com on 21 Sep 2012 at 2:33

GoogleCodeExporter commented 9 years ago
Yep.  As you might guess, Range.canonical(DiscreteDomain) is what you use to 
test if two ranges are "really" the same (in that domain), since a Range by 
itself doesn't know anything about the domain.

Original comment by wasserman.louis on 21 Sep 2012 at 2:39

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08