4

We have been using the com.google.common.collect.RangeMap class in Google's guava library and have found it very useful. In particular, I would like to use the asMapOfRanges() method and depend on the iteration order of the returned Map to be in the order of the Ranges in each entry. The implementation of com.google.common.collect.ImmutableRangeMap seems to explicitly adhere to this implementation, but it doesn't seem to be mentioned in the JavaDoc anywhere. I'm wondering if I can depend on this behavior on any RangeMap, and it was just missed in the JavaDoc, or if iteration order is intended to be unspecified, in which case I should order the results before I iterate. Thanks for your help.

1 Answer 1

2

Well I've looked at the implementation also I came to the same conclusion, I mean this code:

static final Ordering<Range<?>> RANGE_LEX_ORDERING = new Ordering<Range<?>>() {
    @Override
    public int compare(Range<?> left, Range<?> right) {
      return ComparisonChain.start()
          .compare(left.lowerBound, right.lowerBound)
          .compare(left.upperBound, right.upperBound)
          .result();
    }
  };

Is sorting based on Range...

I also think that @Beta is the indication why this is not documented. They might changed it in future releases. In case I wanted to use it I would have one more additional unit test that would fail in case this changes, it will act as a safety net.

3
  • Making a unit test that will fail if they come back in the wrong order is a great suggestion, thanks. Sep 3, 2014 at 14:56
  • I don't have enough reputation yet, sorry! :) I tried! Sep 4, 2014 at 13:57
  • @AndrewRustleund :) np
    – Eugene
    Sep 4, 2014 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.