增加convertValue方法
This commit is contained in:
@@ -263,7 +263,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
entry.objectValue = formatValue(type, value);
|
entry.objectValue = Utility.convertValue(type, value);
|
||||||
entry.expireSeconds(expireSeconds);
|
entry.expireSeconds(expireSeconds);
|
||||||
entry.lastAccessed = System.currentTimeMillis();
|
entry.lastAccessed = System.currentTimeMillis();
|
||||||
} finally {
|
} finally {
|
||||||
@@ -496,9 +496,9 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
// OBJECT, ATOMIC, DOUBLE, SSET, ZSET, LIST, MAP;
|
// OBJECT, ATOMIC, DOUBLE, SSET, ZSET, LIST, MAP;
|
||||||
switch (entry.cacheType) {
|
switch (entry.cacheType) {
|
||||||
case ATOMIC:
|
case ATOMIC:
|
||||||
return formatValue(type, (AtomicLong) entry.objectValue);
|
return Utility.convertValue(type, (AtomicLong) entry.objectValue);
|
||||||
case DOUBLE:
|
case DOUBLE:
|
||||||
return formatValue(type, Double.longBitsToDouble(((AtomicLong) entry.objectValue).intValue()));
|
return Utility.convertValue(type, Double.longBitsToDouble(((AtomicLong) entry.objectValue).intValue()));
|
||||||
case SSET:
|
case SSET:
|
||||||
return (T) new LinkedHashSet(entry.setValue);
|
return (T) new LinkedHashSet(entry.setValue);
|
||||||
case ZSET:
|
case ZSET:
|
||||||
@@ -648,7 +648,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
boolean rs = entry.mapValue.putIfAbsent(field, formatValue(type, value)) == null;
|
boolean rs = entry.mapValue.putIfAbsent(field, Utility.convertValue(type, value)) == null;
|
||||||
entry.lastAccessed = System.currentTimeMillis();
|
entry.lastAccessed = System.currentTimeMillis();
|
||||||
return rs;
|
return rs;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -683,7 +683,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
Map map = entry.mapValue;
|
Map map = entry.mapValue;
|
||||||
List<T> rs = new ArrayList<>(fields.length);
|
List<T> rs = new ArrayList<>(fields.length);
|
||||||
for (String field : fields) {
|
for (String field : fields) {
|
||||||
rs.add((T) formatValue(type, map.get(field)));
|
rs.add((T) Utility.convertValue(type, map.get(field)));
|
||||||
}
|
}
|
||||||
return rs;
|
return rs;
|
||||||
});
|
});
|
||||||
@@ -698,7 +698,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
} else {
|
} else {
|
||||||
Map map = new LinkedHashMap();
|
Map map = new LinkedHashMap();
|
||||||
entry.mapValue.forEach((k, v) -> {
|
entry.mapValue.forEach((k, v) -> {
|
||||||
map.put(k, formatValue(type, v));
|
map.put(k, Utility.convertValue(type, v));
|
||||||
});
|
});
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
@@ -712,7 +712,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
if (entry == null) {
|
if (entry == null) {
|
||||||
return new ArrayList();
|
return new ArrayList();
|
||||||
} else {
|
} else {
|
||||||
return new ArrayList(entry.mapValue.values().stream().map(v -> formatValue(type, v)).toList());
|
return new ArrayList(entry.mapValue.values().stream().map(v -> Utility.convertValue(type, v)).toList());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -748,7 +748,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Object obj = entry.mapValue.get(field);
|
Object obj = entry.mapValue.get(field);
|
||||||
return obj == null ? null : formatValue(type, obj);
|
return obj == null ? null : Utility.convertValue(type, obj);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -786,7 +786,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
entry.mapValue.put(field, formatValue(type, value));
|
entry.mapValue.put(field, Utility.convertValue(type, value));
|
||||||
entry.lastAccessed = System.currentTimeMillis();
|
entry.lastAccessed = System.currentTimeMillis();
|
||||||
} finally {
|
} finally {
|
||||||
entry.unlock();
|
entry.unlock();
|
||||||
@@ -941,7 +941,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
return formatValue(componentType, entry.listValue.pollFirst());
|
return Utility.convertValue(componentType, entry.listValue.pollFirst());
|
||||||
} finally {
|
} finally {
|
||||||
entry.unlock();
|
entry.unlock();
|
||||||
}
|
}
|
||||||
@@ -993,7 +993,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
}
|
}
|
||||||
entry.lock();
|
entry.lock();
|
||||||
try {
|
try {
|
||||||
return formatValue(componentType, entry.listValue.pollLast());
|
return Utility.convertValue(componentType, entry.listValue.pollLast());
|
||||||
} finally {
|
} finally {
|
||||||
entry.unlock();
|
entry.unlock();
|
||||||
}
|
}
|
||||||
@@ -1077,7 +1077,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
for (int i = 0; i < Math.abs(count); i++) {
|
for (int i = 0; i < Math.abs(count); i++) {
|
||||||
int index = ThreadLocalRandom.current().nextInt(vals.size());
|
int index = ThreadLocalRandom.current().nextInt(vals.size());
|
||||||
T val = vals.get(index);
|
T val = vals.get(index);
|
||||||
list.add(formatValue(componentType, val));
|
list.add(Utility.convertValue(componentType, val));
|
||||||
}
|
}
|
||||||
} else { //不可以重复
|
} else { //不可以重复
|
||||||
if (count >= vals.size()) {
|
if (count >= vals.size()) {
|
||||||
@@ -1424,7 +1424,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
Set<T> list = new LinkedHashSet<>();
|
Set<T> list = new LinkedHashSet<>();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
list.add(formatValue(componentType, it.next()));
|
list.add(Utility.convertValue(componentType, it.next()));
|
||||||
if (++index >= count) {
|
if (++index >= count) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1453,7 +1453,7 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
Iterator it = cset.iterator();
|
Iterator it = cset.iterator();
|
||||||
Set<T> list = new LinkedHashSet<>();
|
Set<T> list = new LinkedHashSet<>();
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
list.add((T) formatValue(componentType, it.next()));
|
list.add((T) Utility.convertValue(componentType, it.next()));
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
} finally {
|
} finally {
|
||||||
@@ -1714,28 +1714,6 @@ public final class CacheMemorySource extends AbstractCacheSource {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> T formatValue(@Nullable Type componentType, Object obj) {
|
|
||||||
if (componentType == null || obj == null || componentType == obj.getClass()) {
|
|
||||||
return (T) obj;
|
|
||||||
}
|
|
||||||
if (componentType == String.class) {
|
|
||||||
obj = obj.toString();
|
|
||||||
} else if (componentType == long.class || componentType == Long.class) {
|
|
||||||
if (obj instanceof Number) {
|
|
||||||
obj = ((Number) obj).longValue();
|
|
||||||
} else {
|
|
||||||
obj = Long.parseLong(obj.toString());
|
|
||||||
}
|
|
||||||
} else if (componentType == int.class || componentType == Integer.class) {
|
|
||||||
if (obj instanceof Number) {
|
|
||||||
obj = ((Number) obj).intValue();
|
|
||||||
} else {
|
|
||||||
obj = Integer.parseInt(obj.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (T) obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<Long> dbsizeAsync() {
|
public CompletableFuture<Long> dbsizeAsync() {
|
||||||
return supplyFuture(() -> {
|
return supplyFuture(() -> {
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import java.util.stream.Stream;
|
|||||||
import java.util.zip.GZIPInputStream;
|
import java.util.zip.GZIPInputStream;
|
||||||
import javax.crypto.*;
|
import javax.crypto.*;
|
||||||
import javax.crypto.spec.SecretKeySpec;
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import org.redkale.convert.json.JsonConvert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -2681,6 +2682,56 @@ public final class Utility {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将源对象转换成目标类型
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param type 目标类型
|
||||||
|
* @param value 源对象
|
||||||
|
*
|
||||||
|
* @return 对象
|
||||||
|
*/
|
||||||
|
public static <T> T convertValue(Type type, Object value) {
|
||||||
|
if (type == null || value == null) {
|
||||||
|
return (T) value;
|
||||||
|
}
|
||||||
|
final Class clazz = TypeToken.typeToClass(type);
|
||||||
|
final Class vclz = value.getClass();
|
||||||
|
if (clazz == vclz) {
|
||||||
|
return (T) value;
|
||||||
|
} else if (clazz == String.class) {
|
||||||
|
return (T) value.toString();
|
||||||
|
} else if (clazz.isAssignableFrom(vclz)) {
|
||||||
|
return (T) value;
|
||||||
|
} else if (clazz == double.class || clazz == Double.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).doubleValue();
|
||||||
|
}
|
||||||
|
} else if (clazz == float.class || clazz == Float.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).floatValue();
|
||||||
|
}
|
||||||
|
} else if (clazz == long.class || clazz == Long.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).longValue();
|
||||||
|
}
|
||||||
|
} else if (clazz == int.class || clazz == Integer.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).intValue();
|
||||||
|
}
|
||||||
|
} else if (clazz == short.class || clazz == Short.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).shortValue();
|
||||||
|
}
|
||||||
|
} else if (clazz == byte.class || clazz == Byte.class) {
|
||||||
|
if (value instanceof Number) {
|
||||||
|
return (T) (Number) ((Number) value).byteValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
JsonConvert convert = JsonConvert.root();
|
||||||
|
return convert.convertFrom(type, convert.convertToBytes(value));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将buffer的内容转换成字符串, string参数不为空时会追加在buffer内容字符串之前
|
* 将buffer的内容转换成字符串, string参数不为空时会追加在buffer内容字符串之前
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user