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

#7381 #7493

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

#7381 #7493

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 {

}
16 changes: 16 additions & 0 deletions guava/src/com/google/common/cache/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,20 @@ public interface Cache<K, V> {
* performed -- if any -- is implementation-dependent.
*/
void cleanUp();

/**
* Returns whether this cache is recording statistics.
*
* <p>If this method returns {@code false}, the {@link #stats()} method will return
* a {@link CacheStats} instance with zero for all values.
*
* <p>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;
}
}
9 changes: 9 additions & 0 deletions guava/src/com/google/common/cache/LocalCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
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 @@ -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;
Expand Down Expand Up @@ -66,6 +67,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, // 使用类型推断来创建 TreeRangeMap 实例
(map, element) -> {
Range<K> key = keyFunction.apply(element);
V value = valueFunction.apply(element);

// 检查是否有重叠的范围
RangeMap<K, V> overlappingMap = map.subRangeMap(key);
if (!overlappingMap.asMapOfRanges().isEmpty()) {
// 如果存在重叠范围,则合并这些值
for (Map.Entry<Range<K>, 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<Range<K>> rangesBuilder = new ImmutableList.Builder<>();
ImmutableList.Builder<V> valuesBuilder = new ImmutableList.Builder<>();

// 遍历 map 中的所有范围
for (Map.Entry<Range<K>, 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.
*
Expand Down