DATAKV-38
+ eliminate BasicNumberToStringSerializer + enhance GenericToString serializer by adding a default GenericService
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
/*
|
||||
* Copyright 2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.keyvalue.redis.serializer;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Simple toString() serializer for the core (lang) numberic JDK types.
|
||||
*
|
||||
* @see String#valueOf(Object)
|
||||
* @see Long#valueOf(String)
|
||||
* @author Costin Leau
|
||||
*/
|
||||
public class BasicNumberToStringSerializer<T extends Number> implements RedisSerializer<T> {
|
||||
|
||||
private final Charset charset;
|
||||
private final Constructor<T> ctor;
|
||||
|
||||
public BasicNumberToStringSerializer(Class<T> type) {
|
||||
this(type, Charset.forName("UTF8"));
|
||||
}
|
||||
|
||||
public BasicNumberToStringSerializer(Class<T> type, Charset charset) {
|
||||
Assert.notNull(type);
|
||||
this.charset = charset;
|
||||
|
||||
if (!(Byte.class.isAssignableFrom(type) || Short.class.isAssignableFrom(type)
|
||||
|| Long.class.isAssignableFrom(type) || Integer.class.isAssignableFrom(type)
|
||||
|| Float.class.isAssignableFrom(type) || Double.class.isAssignableFrom(type))) {
|
||||
throw new IllegalArgumentException("Type " + type + " not supported");
|
||||
}
|
||||
|
||||
try {
|
||||
ctor = type.getConstructor(String.class);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalArgumentException("Cannot find suitable constructor for " + type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T deserialize(byte[] bytes) {
|
||||
String string = new String(bytes, charset);
|
||||
return BeanUtils.instantiateClass(ctor, string);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(T object) {
|
||||
String string = String.valueOf(object);
|
||||
return string.getBytes(charset);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.convert.support.ConversionServiceFactory;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
@@ -40,7 +41,7 @@ import org.springframework.util.Assert;
|
||||
public class GenericToStringSerializer<T> implements RedisSerializer<T>, BeanFactoryAware {
|
||||
|
||||
private final Charset charset;
|
||||
private Converter converter;
|
||||
private Converter converter = new Converter(ConversionServiceFactory.createDefaultConversionService());
|
||||
private Class<T> type;
|
||||
|
||||
public GenericToStringSerializer(Class<T> type) {
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
import org.springframework.data.keyvalue.redis.core.ValueOperations;
|
||||
import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.GenericToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
@@ -50,7 +50,7 @@ public class RedisAtomicInteger extends Number implements Serializable, KeyBound
|
||||
public RedisAtomicInteger(String redisCounter, RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<String, Integer>(factory);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new BasicNumberToStringSerializer<Integer>(Integer.class));
|
||||
redisTemplate.setValueSerializer(new GenericToStringSerializer<Integer>(Integer.class));
|
||||
redisTemplate.setExposeConnection(true);
|
||||
this.key = redisCounter;
|
||||
this.generalOps = redisTemplate;
|
||||
|
||||
@@ -24,7 +24,7 @@ import org.springframework.data.keyvalue.redis.core.RedisOperations;
|
||||
import org.springframework.data.keyvalue.redis.core.RedisTemplate;
|
||||
import org.springframework.data.keyvalue.redis.core.SessionCallback;
|
||||
import org.springframework.data.keyvalue.redis.core.ValueOperations;
|
||||
import org.springframework.data.keyvalue.redis.serializer.BasicNumberToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.GenericToStringSerializer;
|
||||
import org.springframework.data.keyvalue.redis.serializer.StringRedisSerializer;
|
||||
|
||||
/**
|
||||
@@ -50,7 +50,7 @@ public class RedisAtomicLong extends Number implements Serializable, KeyBound<St
|
||||
public RedisAtomicLong(String redisCounter, RedisConnectionFactory factory) {
|
||||
RedisTemplate<String, Long> redisTemplate = new RedisTemplate<String, Long>(factory);
|
||||
redisTemplate.setKeySerializer(new StringRedisSerializer());
|
||||
redisTemplate.setValueSerializer(new BasicNumberToStringSerializer<Long>(Long.class));
|
||||
redisTemplate.setValueSerializer(new GenericToStringSerializer<Long>(Long.class));
|
||||
redisTemplate.setExposeConnection(true);
|
||||
this.key = redisCounter;
|
||||
this.generalOps = redisTemplate;
|
||||
|
||||
Reference in New Issue
Block a user