Skip to content

修改CollectionUtils的兼容问题 #1

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

Open
wants to merge 1 commit into
base: main
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
Expand Up @@ -14,7 +14,6 @@
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.lang.NonNull;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -83,7 +82,7 @@ static class EnhancedCacheAdapter implements EnhancedCache {

@Override
public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
HashMap<Object, ValueWrapper> map = CollectionUtils.newHashMap(keys.size());
HashMap<Object, ValueWrapper> map = new HashMap<>(keys.size());
keys.forEach(k -> {
ValueWrapper valueWrapper = cache.get(k);
map.put(k, valueWrapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import lombok.SneakyThrows;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -39,15 +39,15 @@ public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
map = cache.getAllPresent(keys);
}

Map<Object, ValueWrapper> newMap = CollectionUtils.newHashMap(keys.size());
Map<Object, ValueWrapper> newMap = new HashMap<>(keys.size());
map.forEach((key, value) -> newMap.put(key, toValueWrapper(value)));

return newMap;
}

@Override
public void multiPut(Map<?, ?> map) {
Map<Object, Object> newMap = CollectionUtils.newHashMap(map.size());
Map<Object, Object> newMap = new HashMap<>(map.size());
map.forEach((key, value) -> newMap.put(key, toStoreValue(value)));

getNativeCache().putAll(newMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.core.serializer.support.SerializationDelegate;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -51,7 +51,7 @@ protected ConcurrentMapEnhancedCache(String name, ConcurrentMap<Object, Object>

@Override
public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
Map<Object, ValueWrapper> map = CollectionUtils.newHashMap(keys.size());
Map<Object, ValueWrapper> map = new HashMap<>(keys.size());

keys.forEach(key -> {
ValueWrapper valueWrapper = this.get(key);
Expand All @@ -64,7 +64,7 @@ public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
@Override
@SneakyThrows
public void multiPut(Map<?, ?> map) {
Map<Object, Object> newMap = CollectionUtils.newHashMap(map.size());
Map<Object, Object> newMap = new HashMap<>(map.size());
map.forEach((k, v) -> newMap.put(k, toStoreValue(v)));

ConcurrentMap<Object, Object> store = getNativeCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -34,7 +34,7 @@ public EhcacheEnhancedCache(Ehcache ehcache) {
public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
Map<Object, Element> map = getNativeCache().getAll(keys);

Map<Object, ValueWrapper> newMap = CollectionUtils.newHashMap(keys.size());
Map<Object, ValueWrapper> newMap = new HashMap<>(keys.size());
map.forEach((key, value) -> newMap.put(key, toValueWrapper(value)));

return newMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Method;
import java.time.Duration;
Expand Down Expand Up @@ -61,7 +60,7 @@ public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
throw new IllegalStateException();
}

HashMap<Object, ValueWrapper> map = CollectionUtils.newHashMap(keyArr.length);
HashMap<Object, ValueWrapper> map = new HashMap<>(keyArr.length);
for (int i = 0, length = keyArr.length; i < length; i++) {
byte[] value = values.get(i);
ValueWrapper valueWrapper = toValueWrapper(value == null ? null : deserializeCacheValue(value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static class ListReturnTypeMaker implements ReturnTypeMaker<List<?>> {
if (parameterDetail.isStrictNull()) {
return Collections.emptyMap();
}
Map<Object, Object> map = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
Map<Object, Object> map = new HashMap<>(subCacheAsMultiArg.size());
for (Object o : subCacheAsMultiArg) {
map.put(o, null);
}
Expand All @@ -167,14 +167,14 @@ private static class ListReturnTypeMaker implements ReturnTypeMaker<List<?>> {
if (invokeValues.size() != subCacheAsMultiArg.size()) {
throw new IllegalStateException("The size of return list is not equal to the size of parameter list");
}
Map<Object, Object> map = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
Map<Object, Object> map = new HashMap<>(subCacheAsMultiArg.size());
Iterator<?> iterator = invokeValues.iterator();
subCacheAsMultiArg.forEach((argItem) -> {
map.put(argItem, iterator.next());
});
return map;
}
Map<Object, Object> map = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
Map<Object, Object> map = new HashMap<>(subCacheAsMultiArg.size());
invokeValues.forEach(i -> {
SimpleEvaluationContext context = CONTEXT_BUILDER.withRootObject(i).build();
map.put(parameterDetail.getAsElementFieldExpression().getValue(context), i);
Expand Down Expand Up @@ -217,7 +217,7 @@ private static class MapReturnTypeMaker implements ReturnTypeMaker<Map<?, ?>> {
if (parameterDetail.isStrictNull()) {
return Collections.emptyMap();
}
Map<Object, Object> map = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
Map<Object, Object> map = new HashMap<>(subCacheAsMultiArg.size());
for (Object o : subCacheAsMultiArg) {
map.put(o, null);
}
Expand All @@ -228,7 +228,7 @@ private static class MapReturnTypeMaker implements ReturnTypeMaker<Map<?, ?>> {
return invokeValues;
}

Map<Object, Object> map = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
Map<Object, Object> map = new HashMap<>(subCacheAsMultiArg.size());
for (Object o : subCacheAsMultiArg) {
map.put(o, invokeValues.get(o));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void performCacheEvict(
CacheAsMultiOperation<?> multiOperation = firstContext.getMultiOperation();
Collection<?> cacheAsMultiArg = contexts.getCacheAsMultiArg();
assert cacheAsMultiArg != null;
Map<Object, Object> argValueMap = CollectionUtils.newHashMap(cacheAsMultiArg.size());
Map<Object, Object> argValueMap = new HashMap<>(cacheAsMultiArg.size());

Collection<?> missCacheAsMultiArg = cacheAsMultiArg;

Expand Down Expand Up @@ -431,7 +431,7 @@ private Collection<Object> generateKeys(CacheAsMultiOperationContext context,
private Map<Object, Object> generateArgKeyMap(CacheAsMultiOperationContext context,
Collection<?> subCacheAsMultiArg) {

HashMap<Object, Object> argKeyMap = CollectionUtils.newHashMap(subCacheAsMultiArg.size());
HashMap<Object, Object> argKeyMap = new HashMap<>(subCacheAsMultiArg.size());
for (Object argItem : subCacheAsMultiArg) {
argKeyMap.put(argItem, context.generateKey(argItem, null));
}
Expand All @@ -442,7 +442,7 @@ private Map<Object, Object> generateArgKeyMap(CacheAsMultiOperationContext conte
private Map<Object, Object> generateKeyValueMap(CacheAsMultiOperationContext context,
Collection<?> subCacheAsMultiArg, Map<?, ?> argValueMap) {

HashMap<Object, Object> keyValueMap = CollectionUtils.newHashMap(argValueMap.size());
HashMap<Object, Object> keyValueMap = new HashMap<>(argValueMap.size());
for (Object argItem : subCacheAsMultiArg) {
Object value = argValueMap.get(argItem);
keyValueMap.put(context.generateKey(argItem, value), value);
Expand Down Expand Up @@ -604,8 +604,8 @@ public CacheAsMultiOperationContext(CacheAsMultiOperation<?> multiOperation, Cac
this.multiOperation = multiOperation;
this.superArgs = super.getArgs();
this.cacheAsMultiArg = (Collection<?>) args[multiOperation.getCacheAsMultiParameterPosition()];
this.isConditionPassingCache = CollectionUtils.newHashMap(cacheAsMultiArg.size());
this.keyCache = CollectionUtils.newHashMap(cacheAsMultiArg.size());
this.isConditionPassingCache = new HashMap<>(cacheAsMultiArg.size());
this.keyCache = new HashMap<>(cacheAsMultiArg.size());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import io.github.ms100.cacheasmulti.cache.EnhancedCache;
import org.springframework.cache.jcache.JCacheCache;
import org.springframework.util.CollectionUtils;

import javax.cache.Cache;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

Expand All @@ -28,15 +28,15 @@ public EnhancedJCacheCache(Cache<Object, Object> jcache, boolean allowNullValues
public Map<Object, ValueWrapper> multiGet(Collection<?> keys) {
Map<Object, Object> map = getNativeCache().getAll(new HashSet<>(keys));

Map<Object, ValueWrapper> newMap = CollectionUtils.newHashMap(keys.size());
Map<Object, ValueWrapper> newMap = new HashMap<>(keys.size());
map.forEach((key, value) -> newMap.put(key, toValueWrapper(value)));

return newMap;
}

@Override
public void multiPut(Map<?, ?> map) {
Map<Object, Object> newMap = CollectionUtils.newHashMap(map.size());
Map<Object, Object> newMap = new HashMap<>(map.size());
map.forEach((key, value) -> newMap.put(key, toStoreValue(value)));

getNativeCache().putAll(newMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
import org.springframework.cache.jcache.interceptor.JCacheOperation;
import org.springframework.cache.jcache.interceptor.SimpleExceptionCacheResolver;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import javax.cache.annotation.CacheInvocationContext;
import javax.cache.annotation.CacheInvocationParameter;
import javax.cache.annotation.CacheResult;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -64,7 +64,7 @@ public CacheAsMultiOperationContext(O multiOperation, Object target, Object[] ar
} else if (cacheAsMultiArg instanceof Map) {
size = ((Map<?, ?>) cacheAsMultiArg).size();
}
this.keyCache = CollectionUtils.newHashMap(size);
this.keyCache = new HashMap<>(size);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.util.CollectionUtils;

import javax.cache.annotation.CachePut;
import java.util.HashMap;
import java.util.Map;

/**
Expand Down Expand Up @@ -52,7 +53,7 @@ protected void cacheValues(CacheAsMultiOperationContext<CachePutAsMultiOperation
return;
}

Map<Object, Object> keyValueMap = CollectionUtils.newHashMap(cacheAsMultiArg.size());
Map<Object, Object> keyValueMap = new HashMap<>(cacheAsMultiArg.size());
cacheAsMultiArg.forEach((argItem, value) -> keyValueMap.put(context.generateKey(argItem), value));

EnhancedCache cache = resolveCache(context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import org.springframework.util.CollectionUtils;

import javax.cache.annotation.CacheResult;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;

/**
Expand All @@ -40,7 +37,7 @@ public Object invoke(CacheAsMultiOperationContext<CacheResultAsMultiOperation, C
}

// 参数与数据的映射
Map<Object, Object> argValueMap = CollectionUtils.newHashMap(cacheAsMultiArg.size());
Map<Object, Object> argValueMap = new HashMap<>(cacheAsMultiArg.size());

Collection<?> missCacheAsMultiArg = findInCache(context, cacheAsMultiArg, argValueMap);

Expand All @@ -63,7 +60,7 @@ public Object invoke(CacheAsMultiOperationContext<CacheResultAsMultiOperation, C
private Collection<?> findInCache(CacheAsMultiOperationContext<CacheResultAsMultiOperation, CacheResult> context,
Collection<?> cacheAsMultiArg, Map<Object, Object> argValueMap) {

Map<Object, Object> argKeyMap = CollectionUtils.newHashMap(cacheAsMultiArg.size());
Map<Object, Object> argKeyMap = new HashMap<>(cacheAsMultiArg.size());
cacheAsMultiArg.forEach(argItem -> argKeyMap.put(argItem, context.generateKey(argItem)));
if (log.isTraceEnabled()) {
log.trace("Find keys " + argKeyMap.values() + " for operation " + context.getOperation());
Expand Down Expand Up @@ -113,7 +110,7 @@ private Object invokeWithMissCacheAsMultiArg(CacheAsMultiOperationContext<CacheR

if (!CollectionUtils.isEmpty(missArgValueMap)) {
// 需要缓存的数据
Map<Object, Object> missKeyValueMap = CollectionUtils.newHashMap(missCacheAsMultiArg.size());
Map<Object, Object> missKeyValueMap = new HashMap<>(missCacheAsMultiArg.size());
missArgValueMap.forEach((argItem, value) -> missKeyValueMap.put(context.generateKey(argItem), value));
EnhancedCache cache = resolveCache(context);
// 缓存数据
Expand Down