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/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(); 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. *