免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1203 | 回复: 0

同步比较器工具 [复制链接]

论坛徽章:
0
发表于 2015-07-15 09:48 |显示全部楼层
项目中,我们常常遇到需要同步数据的情况,这个时候我们需要将 旧数据 与 新数据 做对比。处理三种情况的数据:①存在于旧数据,不存在于新数据的信息,进行移除操作;②存在于旧数据,也存在于新数据的信息,进行更新操作;③不存在于旧数据,存在于新数据的信息,进行添加操作。

同步比较器基类
  1. /**
  2. * 基本 同步比较器
  3. * @author linling
  4. *
  5. */
  6. public class BasicComparator {
  7.      
  8.     public interface  ListCompareHandler<T> {
  9.          
  10.         public void handlerNewEntry(T t);
  11.          
  12.         public void handlerNoMoreEntry(T t);
  13.          
  14.         public void handlerSameEntry(T t);
  15.          
  16.         /**
  17.          * if oneList contain otherList return boolean , Otherwise return false;
  18.          * @param oneList
  19.          * @param otherList
  20.          * @return
  21.          */
  22.         public boolean contain(List<T> oneList, T t) throws Exception;
  23.     }
  24.      
  25.     public <T> void compareTo(List<T> oldList, List<T> newList, ListCompareHandler<T> handler)  throws Exception{
  26.         for(Iterator<T> iter = oldList.iterator(); iter.hasNext();) {
  27.             T t = iter.next();
  28.             if(!handler.contain(newList, t)) {
  29.                 handler.handlerNoMoreEntry(t);
  30.             }
  31.         }
  32.          
  33.         for(Iterator<T> iter = newList.iterator(); iter.hasNext();) {
  34.             T t = iter.next();
  35.             if(handler.contain(oldList, t)) {
  36.                 handler.handlerSameEntry(t);
  37.             }else {
  38.                 handler.handlerNewEntry(t);
  39.             }
  40.         }
  41.     }
  42.      
  43.     public interface SetCompareHandler<T> {
  44.         public void handlerNewEntry(T t);
  45.          
  46.         public void handlerNoMoreEntry(T t);
  47.          
  48.         public void handlerSameEntry(T t);
  49.          
  50.         /**
  51.          * if oneSet contain otherSet return true, Otherwise return false;
  52.          * @param oneSet
  53.          * @param otherSet
  54.          * @return
  55.          */
  56.         public boolean contain(Set<T> oneSet, T t) throws Exception;
  57.     }
  58.      
  59.     public <T> void compareTo(Set<T> oldSet, Set<T> newSet, SetCompareHandler<T> handler) throws Exception{
  60.         for(Iterator<T> iter = oldSet.iterator(); iter.hasNext();) {
  61.             T t = iter.next();
  62.             if(!handler.contain(newSet, t)) {
  63.                 handler.handlerNoMoreEntry(t);
  64.             }
  65.         }

  66.         for(Iterator<T> iter = newSet.iterator();iter.hasNext();) {
  67.             T t = iter.next();
  68.             if(handler.contain(oldSet, t)) {
  69.                 handler.handlerSameEntry(t);
  70.             }else {
  71.                 handler.handlerNewEntry(t);
  72.             }
  73.         }
  74.     }
  75.      
  76.     public interface MapCompareHandler<K,V> {
  77.         public void handlerNewEntry(Map.Entry<K, V> entry);
  78.          
  79.         public void handlerNoMoreEntry(Map.Entry<K, V> entry);
  80.          
  81.         public void handlerSameEntry(Map.Entry<K, V> entry);
  82.          
  83.         /**
  84.          * if oneMap contain otherMap return true, Otherwise return false;
  85.          * @param oneMap
  86.          * @param otherMap
  87.          * @return
  88.          */
  89.         public boolean contain(Map<K,V> oneMap, Map.Entry<K, V> entry) throws Exception;
  90.     }
  91.      
  92.     public <K,V> void compareTo(Map<K,V> newMap, Map<K,V> oldMap, MapCompareHandler<K,V> handler) throws Exception{
  93.         Set<Map.Entry<K, V>> oldEntrySet = oldMap.entrySet();
  94.         for(Iterator<Map.Entry<K, V>> iter = oldEntrySet.iterator(); iter.hasNext();) {
  95.             Map.Entry<K, V> entry = iter.next();
  96.             if(!handler.contain(newMap, entry)) {
  97.                 handler.handlerNoMoreEntry(entry);
  98.             }
  99.         }
  100.          
  101.         Set<Map.Entry<K, V>> newEntrySet = newMap.entrySet();
  102.         for(Iterator<Map.Entry<K, V>> iter = newEntrySet.iterator(); iter.hasNext();) {
  103.             Map.Entry<K, V> entry = iter.next();
  104.             if(handler.contain(oldMap, entry)) {
  105.                 handler.handlerSameEntry(entry);
  106.             }else {
  107.                 handler.handlerNewEntry(entry);
  108.             }
  109.         }
  110.     }
  111. }



  112. 下面是实现了上面 基本比较器的基本类,负责初始化List和Set比较器的handler操作。我在项目中只需要集成下面这个类就好。

  113. package com.bayern;

  114. import java.util.List;
  115. import java.util.Set;


  116. /**
  117. * 基本 全同步比较器  基础类
  118. * @author linling
  119. *
  120. * @param <VO>
  121. */
  122. public abstract class ComparatorAdapter<VO> extends BasicComparator {

  123.     @SuppressWarnings("rawtypes")
  124.     protected BasicService basicService;
  125.      
  126.     public abstract void setBasicService(BasicService<VO> basicService);
  127.      
  128.      
  129.     public class ListCompareHandlerAdapter implements ListCompareHandler<VO> {
  130.         public void handlerNewEntry(VO vo) {
  131.             basicService.save(vo);
  132.         }
  133.          
  134.         public void handlerNoMoreEntry(VO vo) {
  135.             basicService.delete(vo);
  136.         }
  137.          
  138.         public void handlerSameEntry(VO vo) {
  139.             basicService.update(vo);
  140.         }

  141.         /**
  142.          * 该方法必须被子类覆盖重写
  143.          */
  144.         @Override
  145.         public boolean contain(List<VO> oneList, VO t) throws Exception{
  146.             throw new Exception();
  147.         }
  148.     }
  149.      
  150.     public class SetCompareHandlerAdapter implements SetCompareHandler<VO> {
  151.         public void handlerNewEntry(VO vo) {
  152.             basicService.save(vo);
  153.         }
  154.          
  155.         public void handlerNoMoreEntry(VO vo) {
  156.             basicService.delete(vo);
  157.         }
  158.          
  159.         public void handlerSameEntry(VO vo) {
  160.             basicService.update(vo);
  161.         }

  162.         /**
  163.          * 该方法必须被子类覆盖重写
  164.          */
  165.         @Override
  166.         public boolean contain(Set<VO> oneSet, VO t)  throws Exception{
  167.             throw new Exception();
  168.         }

  169.     }
  170. }



  171. 最后,来个实际的比较器:
  172. package com.bayern;

  173. import java.util.Iterator;
  174. import java.util.List;
  175. import java.util.UUID;

  176. import javax.annotation.Resource;

  177. import org.springframework.stereotype.Component;

  178. /**
  179. * 摄像头比较器
  180. * @author linling
  181. *
  182. */
  183. @Component(value="cameraComparator")
  184. public class CameraComparator extends ComparatorAdapter<VoCamera>{

  185.      
  186.     public CameraComparator(){
  187.     }
  188.      
  189.     @Resource(name="cameraServiceImpl")
  190.     @Override
  191.     public void setBasicService(BasicService<VoCamera> basicService) {
  192.         super.basicService = basicService;
  193.     }

  194.     public class CameraComparatorHandler extends ListCompareHandlerAdapter{
  195.         private long schoolId;
  196.         public CameraComparatorHandler(long schoolId) {
  197.             this.schoolId = schoolId;
  198.         }
  199.         @Override
  200.         public boolean contain(List<VoCamera> oneList, VoCamera t)  throws Exception{
  201.             boolean exists = false;
  202.             for(Iterator<VoCamera> iter = oneList.iterator(); iter.hasNext();) {
  203.                 VoCamera vo = iter.next();
  204.                 if(t.getCameraSignature().equals(vo.getCameraSignature()) && schoolId == vo.getSchoolId().longValue()) {
  205.                     if(!CommonUtils.stringIsBlank(vo.getCameraId()) && CommonUtils.stringIsBlank(t.getCameraId())) {
  206.                         t.setCameraId(vo.getCameraId());
  207.                     }
  208.                     exists = true;
  209.                     break;
  210.                 }
  211.             }
  212.             return exists;
  213.         }
  214.          
  215.         @Override
  216.         public void handlerNewEntry(VoCamera vo) {
  217.             vo.setCameraId(UUID.randomUUID().toString());
  218.             super.handlerNewEntry(vo);
  219.         }
  220.          
  221.     }
  222.      
  223. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP