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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.google.common.collect;

public class toImmutableRangeMapTest {

}
62 changes: 62 additions & 0 deletions guava/src/com/google/common/collect/ImmutableRangeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collector;
import javax.annotation.CheckForNull;
Expand Down Expand Up @@ -67,6 +68,67 @@ public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K
return CollectCollectors.toImmutableRangeMap(keyFunction, valueFunction);
}

/**
* Returns a {@code Collector} that accumulates elements into a {@code ImmutableRangeMap},
* resolving overlapping ranges using the provided merge function.
*
* @param keyFunction the function to derive the range key from elements
* @param valueFunction the function to derive the value from elements
* @param mergeFunction the binary operator used to merge values of overlapping ranges
* @param <T> the type of input elements
* @param <K> the key type of the map, extending Comparable
* @param <V> the value type of the map
* @return a {@code Collector} that collects elements into a {@code ImmutableRangeMap}
*/
public static <T, K extends Comparable<? super K>, V> Collector<T, ?, ImmutableRangeMap<K, V>> toImmutableRangeMap(
Function<? super T, Range<K>> keyFunction,
Function<? super T, ? extends V> valueFunction,
BinaryOperator<V> mergeFunction) {

return Collector.of(
TreeRangeMap::<K, V>create, // Use type inference to create a TreeRangeMap instance.
(map, element) -> {
Range<K> key = keyFunction.apply(element);
V value = valueFunction.apply(element);

// Check for overlapping ranges
RangeMap<K, V> overlappingMap = map.subRangeMap(key);
if (!overlappingMap.asMapOfRanges().isEmpty()) {
// If there are overlapping ranges, merge the values
for (Map.Entry<Range<K>, V> entry : overlappingMap.asMapOfRanges().entrySet()) {
V existingValue = entry.getValue();
value = mergeFunction.apply(existingValue, value);
}
// Remove the original overlapping range
map.remove(key);
}

map.put(key, value);
},
(left, right) -> {
left.putAll(right);
return left;
},
map -> {
ImmutableList.Builder<Range<K>> rangesBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<>();

// Iterate over all ranges in the map
for (Map.Entry<Range<K>, V> entry : map.asMapOfRanges().entrySet()) {
rangesBuilder.add(entry.getKey());
valuesBuilder.add(entry.getValue());
}

// Construct an immutable ImmutableRangeMap.
return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build());
}
);
}





/**
* Returns an empty immutable range map.
*
Expand Down