From cb1c89448ac1aa04ecdb96a598965fab9a765c91 Mon Sep 17 00:00:00 2001 From: Yunru Date: Thu, 17 Oct 2024 01:52:37 +1100 Subject: [PATCH 1/4] Completed toImmutableRangeMap method --- .../collect/toImmutableRangeMapTest.java | 5 ++ .../common/collect/ImmutableRangeMap.java | 62 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 guava-tests/test/com/google/common/collect/toImmutableRangeMapTest.java diff --git a/guava-tests/test/com/google/common/collect/toImmutableRangeMapTest.java b/guava-tests/test/com/google/common/collect/toImmutableRangeMapTest.java new file mode 100644 index 000000000000..b1b226ae57b4 --- /dev/null +++ b/guava-tests/test/com/google/common/collect/toImmutableRangeMapTest.java @@ -0,0 +1,5 @@ +package com.google.common.collect; + +public class toImmutableRangeMapTest { + +} diff --git a/guava/src/com/google/common/collect/ImmutableRangeMap.java b/guava/src/com/google/common/collect/ImmutableRangeMap.java index 2ab707714eb6..2cb47a18471c 100644 --- a/guava/src/com/google/common/collect/ImmutableRangeMap.java +++ b/guava/src/com/google/common/collect/ImmutableRangeMap.java @@ -34,6 +34,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; @@ -66,6 +67,67 @@ public class ImmutableRangeMap, V> implements RangeMap the type of input elements + * @param the key type of the map, extending Comparable + * @param the value type of the map + * @return a {@code Collector} that collects elements into a {@code ImmutableRangeMap} + */ + public static , V> Collector> toImmutableRangeMap( + Function> keyFunction, + Function valueFunction, + BinaryOperator mergeFunction) { + + return Collector.of( + TreeRangeMap::create, // 使用类型推断来创建 TreeRangeMap 实例 + (map, element) -> { + Range key = keyFunction.apply(element); + V value = valueFunction.apply(element); + + // 检查是否有重叠的范围 + RangeMap overlappingMap = map.subRangeMap(key); + if (!overlappingMap.asMapOfRanges().isEmpty()) { + // 如果存在重叠范围,则合并这些值 + for (Map.Entry, V> entry : overlappingMap.asMapOfRanges().entrySet()) { + V existingValue = entry.getValue(); + value = mergeFunction.apply(existingValue, value); + } + // 移除原有的重叠范围 + map.remove(key); + } + + map.put(key, value); + }, + (left, right) -> { + left.putAll(right); + return left; + }, + map -> { + ImmutableList.Builder> rangesBuilder = new ImmutableList.Builder<>(); + ImmutableList.Builder valuesBuilder = new ImmutableList.Builder<>(); + + // 遍历 map 中的所有范围 + for (Map.Entry, V> entry : map.asMapOfRanges().entrySet()) { + rangesBuilder.add(entry.getKey()); + valuesBuilder.add(entry.getValue()); + } + + // 构造不可变的 ImmutableRangeMap + return new ImmutableRangeMap<>(rangesBuilder.build(), valuesBuilder.build()); + } + ); + } + + + + + /** * Returns an empty immutable range map. * From fe04230a2cebb2595f8d84a68b6a7cbb8414c2da Mon Sep 17 00:00:00 2001 From: Boyang Zhang Date: Sun, 20 Oct 2024 19:40:42 +1100 Subject: [PATCH 2/4] #7381 Provide API to see if a cache records statistics --- guava/src/com/google/common/cache/Cache.java | 16 ++++++++++++++++ .../src/com/google/common/cache/LocalCache.java | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/guava/src/com/google/common/cache/Cache.java b/guava/src/com/google/common/cache/Cache.java index 41cce70373a0..0a202c0b7db6 100644 --- a/guava/src/com/google/common/cache/Cache.java +++ b/guava/src/com/google/common/cache/Cache.java @@ -182,4 +182,20 @@ public interface Cache { * performed -- if any -- is implementation-dependent. */ void cleanUp(); + + /** + * Returns whether this cache is recording statistics. + * + *

If this method returns {@code false}, the {@link #stats()} method will return + * a {@link CacheStats} instance with zero for all values. + * + *

The default implementation returns {@code false}. Implementations that support + * statistics recording should override this method to return {@code true} when appropriate. + * + * @return {@code true} if this cache is recording statistics, {@code false} otherwise + * @since 33 + */ + default boolean isRecordingStats() { + return false; + } } diff --git a/guava/src/com/google/common/cache/LocalCache.java b/guava/src/com/google/common/cache/LocalCache.java index b1a4438aced4..328c027c9f89 100644 --- a/guava/src/com/google/common/cache/LocalCache.java +++ b/guava/src/com/google/common/cache/LocalCache.java @@ -4956,6 +4956,15 @@ public CacheStats stats() { return aggregator.snapshot(); } + /** + * Returns whether this cache is recording statistics. + * + * @return {@code true} if this cache is recording statistics, {@code false} otherwise + */ + public boolean isRecordingStats() { + return !localCache.globalStatsCounter.equals(CacheBuilder.EMPTY_STATS); + } + @Override public void cleanUp() { localCache.cleanUp(); From d075ad61af8c3ea095a0e346a22444ae2e2627bf Mon Sep 17 00:00:00 2001 From: Boyang Zhang Date: Sun, 20 Oct 2024 21:45:14 +1100 Subject: [PATCH 3/4] #7381 Provide API to see if a cache records statistics Revert commit --- guava/src/com/google/common/cache/Cache.java | 16 ---------------- .../src/com/google/common/cache/LocalCache.java | 9 --------- 2 files changed, 25 deletions(-) diff --git a/guava/src/com/google/common/cache/Cache.java b/guava/src/com/google/common/cache/Cache.java index 0a202c0b7db6..41cce70373a0 100644 --- a/guava/src/com/google/common/cache/Cache.java +++ b/guava/src/com/google/common/cache/Cache.java @@ -182,20 +182,4 @@ public interface Cache { * performed -- if any -- is implementation-dependent. */ void cleanUp(); - - /** - * Returns whether this cache is recording statistics. - * - *

If this method returns {@code false}, the {@link #stats()} method will return - * a {@link CacheStats} instance with zero for all values. - * - *

The default implementation returns {@code false}. Implementations that support - * statistics recording should override this method to return {@code true} when appropriate. - * - * @return {@code true} if this cache is recording statistics, {@code false} otherwise - * @since 33 - */ - default boolean isRecordingStats() { - return false; - } } diff --git a/guava/src/com/google/common/cache/LocalCache.java b/guava/src/com/google/common/cache/LocalCache.java index 328c027c9f89..b1a4438aced4 100644 --- a/guava/src/com/google/common/cache/LocalCache.java +++ b/guava/src/com/google/common/cache/LocalCache.java @@ -4956,15 +4956,6 @@ public CacheStats stats() { return aggregator.snapshot(); } - /** - * Returns whether this cache is recording statistics. - * - * @return {@code true} if this cache is recording statistics, {@code false} otherwise - */ - public boolean isRecordingStats() { - return !localCache.globalStatsCounter.equals(CacheBuilder.EMPTY_STATS); - } - @Override public void cleanUp() { localCache.cleanUp(); From 45fe6fd4b230bd62c472eda47666e90369c74ceb Mon Sep 17 00:00:00 2001 From: Yunru Date: Fri, 25 Oct 2024 02:30:04 +1100 Subject: [PATCH 4/4] Completed issue #6822 and added comments --- .../com/google/common/collect/ImmutableRangeMap.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/guava/src/com/google/common/collect/ImmutableRangeMap.java b/guava/src/com/google/common/collect/ImmutableRangeMap.java index 2cb47a18471c..fa38b6883a1c 100644 --- a/guava/src/com/google/common/collect/ImmutableRangeMap.java +++ b/guava/src/com/google/common/collect/ImmutableRangeMap.java @@ -85,20 +85,20 @@ public class ImmutableRangeMap, V> implements RangeMap mergeFunction) { return Collector.of( - TreeRangeMap::create, // 使用类型推断来创建 TreeRangeMap 实例 + TreeRangeMap::create, // Use type inference to create a TreeRangeMap instance. (map, element) -> { Range key = keyFunction.apply(element); V value = valueFunction.apply(element); - // 检查是否有重叠的范围 + // Check for overlapping ranges RangeMap overlappingMap = map.subRangeMap(key); if (!overlappingMap.asMapOfRanges().isEmpty()) { - // 如果存在重叠范围,则合并这些值 + // If there are overlapping ranges, merge the values for (Map.Entry, V> entry : overlappingMap.asMapOfRanges().entrySet()) { V existingValue = entry.getValue(); value = mergeFunction.apply(existingValue, value); } - // 移除原有的重叠范围 + // Remove the original overlapping range map.remove(key); } @@ -112,13 +112,13 @@ public class ImmutableRangeMap, V> implements RangeMap> rangesBuilder = new ImmutableList.Builder<>(); ImmutableList.Builder valuesBuilder = new ImmutableList.Builder<>(); - // 遍历 map 中的所有范围 + // Iterate over all ranges in the map for (Map.Entry, 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()); } );