Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ImmutableRangeMap.toImmutableRangeMap with Key, Value, and Merge Functions (#6822)Issue 6822 #7482

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Mirrious
Copy link

Description

This PR implements a new Collector method called toImmutableRangeMap, which allows building an ImmutableRangeMap from a stream of elements. This functionality is useful when dealing with overlapping ranges, where a custom mergeFunction is used to handle overlapping cases.

Key Features:

  • Key Function (keyFunction): A function to derive the range key from the input elements.
  • Value Function (valueFunction): A function to extract the values from the input elements.
  • Merge Function (mergeFunction): A binary operator to resolve values when ranges overlap.

Code Explanation

The method toImmutableRangeMap uses a TreeRangeMap to collect elements, while handling overlapping ranges:

  • If an overlapping range is detected, the mergeFunction is applied to merge the values.
  • The result is an immutable ImmutableRangeMap, constructed from the collected ranges and values.

Usage Example

Here's an example of how the new method can be used:

RangeMap<Instant, Integer> load(List<Job> jobs) {
    return jobs.stream().collect(
        ImmutableRangeMap.toImmutableRangeMap(
            Job::span,
            job -> 1,
            Integer::sum
        )
    );
}

Test Coverage

  • Tested with overlapping ranges to ensure the mergeFunction correctly handles conflicts.
  • Verified that the output ImmutableRangeMap is accurately constructed with non-overlapping ranges.
  • Checked the immutability of the resulting ImmutableRangeMap.

Related Issues

Fixes #6822.

Yunru and others added 4 commits October 17, 2024 01:52
Provide API to see if a cache records statistics
Provide API to see if a cache records statistics
Revert commit
Copy link

google-cla bot commented Oct 24, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants