Skip to content

Commit

Permalink
Completed issue google#6822 and added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yunru committed Oct 24, 2024
1 parent d075ad6 commit 45fe6fd
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions guava/src/com/google/common/collect/ImmutableRangeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K
BinaryOperator<V> mergeFunction) {

return Collector.of(
TreeRangeMap::<K, V>create, // 使用类型推断来创建 TreeRangeMap 实例
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);
}

Expand All @@ -112,13 +112,13 @@ public class ImmutableRangeMap<K extends Comparable<?>, V> implements RangeMap<K
ImmutableList.Builder<Range<K>> rangesBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<>();

// 遍历 map 中的所有范围
// 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());
}

// 构造不可变的 ImmutableRangeMap
// Construct an immutable ImmutableRangeMap.
return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build());
}
);
Expand Down

0 comments on commit 45fe6fd

Please sign in to comment.