装饰器的应用 DecoratingStringHashMapper

来源:互联网 发布:超氧换血疗法 知乎 编辑:程序博客网 时间:2024/05/16 06:02

springdataredis 中提供的hashmapper

hashmapper接口

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public interface HashMapper<T, K, V> {  
  2.   
  3.     Map<K, V> toHash(T object);  
  4.   
  5.     T fromHash(Map<K, V> hash);  
  6. }  

hashmapper的实现1

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class BeanUtilsHashMapper<T> implements HashMapper<T, String, String> {  
  2.   
  3.     private Class<T> type;  
  4.   
  5.     public BeanUtilsHashMapper(Class<T> type) {  
  6.         this.type = type;  
  7.     }  
  8.   
  9.     public T fromHash(Map<String, String> hash) {  
  10.   
  11.         T instance = org.springframework.beans.BeanUtils.instantiate(type);  
  12.         try {  
  13.             BeanUtils.populate(instance, hash);  
  14.         } catch (Exception ex) {  
  15.             throw new RuntimeException(ex);  
  16.         }  
  17.         return instance;  
  18.     }  
  19.   
  20.     /* 
  21.      * (non-Javadoc) 
  22.      * @see org.springframework.data.redis.hash.HashMapper#toHash(java.lang.Object) 
  23.      */  
  24.     @Override  
  25.     public Map<String, String> toHash(T object) {  
  26.         try {  
  27.   
  28.             Map<String, String> map = BeanUtils.describe(object);  
  29.   
  30.             Map<String, String> result = new LinkedHashMap<String, String>();  
  31.             for (Entry<String, String> entry : map.entrySet()) {  
  32.                 if (entry.getValue() != null) {  
  33.                     result.put(entry.getKey(), entry.getValue());  
  34.                 }  
  35.             }  
  36.   
  37.             return result;  
  38.   
  39.         } catch (Exception ex) {  
  40.             throw new IllegalArgumentException("Cannot describe object " + object, ex);  
  41.         }  
  42.     }  
  43. }  

hashmapper的实现2

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class JacksonHashMapper<T> implements HashMapper<T, String, Object> {  
  2.   
  3.     private final ObjectMapper mapper;  
  4.     private final JavaType userType;  
  5.     private final JavaType mapType = TypeFactory.defaultInstance()  
  6.             .constructMapType(Map.class, String.class, Object.class);  
  7.   
  8.     /** 
  9.      * Creates new {@link JacksonHashMapper}. 
  10.      *  
  11.      * @param type 
  12.      */  
  13.     public JacksonHashMapper(Class<T> type) {  
  14.   
  15.         this(type, new ObjectMapper());  
  16.         mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);  
  17.     }  
  18.   
  19.     public JacksonHashMapper(Class<T> type, ObjectMapper mapper) {  
  20.   
  21.         this.mapper = mapper;  
  22.         this.userType = TypeFactory.defaultInstance().constructType(type);  
  23.     }  
  24.   
  25.     @SuppressWarnings("unchecked")  
  26.     public T fromHash(Map<String, Object> hash) {  
  27.         return (T) mapper.convertValue(hash, userType);  
  28.     }  
  29.   
  30.     public Map<String, Object> toHash(T object) {  
  31.         return mapper.convertValue(object, mapType);  
  32.     }  
  33. }  

装饰器--用来优化hashmap的容量

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class DecoratingStringHashMapper<T> implements HashMapper<T, String, String> {  
  2.   
  3.     private final HashMapper<T, ?, ?> delegate;  
  4.   
  5.     public DecoratingStringHashMapper(HashMapper<T, ?, ?> mapper) {  
  6.         this.delegate = mapper;  
  7.     }  
  8.   
  9.     @SuppressWarnings({ "rawtypes""unchecked" })  
  10.     @Override  
  11.     public T fromHash(Map hash) {  
  12.         return (T) delegate.fromHash(hash);  
  13.     }  
  14.   
  15.     public Map<String, String> toHash(T object) {  
  16.         Map<?, ?> hash = delegate.toHash(object);  
  17.         Map<String, String> flatten = new LinkedHashMap<String, String>(hash.size());  
  18.         for (Map.Entry<?, ?> entry : hash.entrySet()) {  
  19.             flatten.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));  
  20.         }  
  21.         return flatten;  
  22.     }  
  23. }  


客户端的应用

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. private HashMapper<QuestShows, String, String> createHashMapper() {  
  2.         return new DecoratingStringHashMapper<QuestShows>(new BeanUtilsHashMapper<QuestShows>(QuestShows.class));  
  3.     }  

装饰器的实现类需要在构造方法中引入被装饰的类以便装饰
0 0
原创粉丝点击