- 论坛徽章:
- 0
|
项目中,我们常常遇到需要同步数据的情况,这个时候我们需要将 旧数据 与 新数据 做对比。处理三种情况的数据:①存在于旧数据,不存在于新数据的信息,进行移除操作;②存在于旧数据,也存在于新数据的信息,进行更新操作;③不存在于旧数据,存在于新数据的信息,进行添加操作。
同步比较器基类- /**
- * 基本 同步比较器
- * @author linling
- *
- */
- public class BasicComparator {
-
- public interface ListCompareHandler<T> {
-
- public void handlerNewEntry(T t);
-
- public void handlerNoMoreEntry(T t);
-
- public void handlerSameEntry(T t);
-
- /**
- * if oneList contain otherList return boolean , Otherwise return false;
- * @param oneList
- * @param otherList
- * @return
- */
- public boolean contain(List<T> oneList, T t) throws Exception;
- }
-
- public <T> void compareTo(List<T> oldList, List<T> newList, ListCompareHandler<T> handler) throws Exception{
- for(Iterator<T> iter = oldList.iterator(); iter.hasNext();) {
- T t = iter.next();
- if(!handler.contain(newList, t)) {
- handler.handlerNoMoreEntry(t);
- }
- }
-
- for(Iterator<T> iter = newList.iterator(); iter.hasNext();) {
- T t = iter.next();
- if(handler.contain(oldList, t)) {
- handler.handlerSameEntry(t);
- }else {
- handler.handlerNewEntry(t);
- }
- }
- }
-
- public interface SetCompareHandler<T> {
- public void handlerNewEntry(T t);
-
- public void handlerNoMoreEntry(T t);
-
- public void handlerSameEntry(T t);
-
- /**
- * if oneSet contain otherSet return true, Otherwise return false;
- * @param oneSet
- * @param otherSet
- * @return
- */
- public boolean contain(Set<T> oneSet, T t) throws Exception;
- }
-
- public <T> void compareTo(Set<T> oldSet, Set<T> newSet, SetCompareHandler<T> handler) throws Exception{
- for(Iterator<T> iter = oldSet.iterator(); iter.hasNext();) {
- T t = iter.next();
- if(!handler.contain(newSet, t)) {
- handler.handlerNoMoreEntry(t);
- }
- }
-
- for(Iterator<T> iter = newSet.iterator();iter.hasNext();) {
- T t = iter.next();
- if(handler.contain(oldSet, t)) {
- handler.handlerSameEntry(t);
- }else {
- handler.handlerNewEntry(t);
- }
- }
- }
-
- public interface MapCompareHandler<K,V> {
- public void handlerNewEntry(Map.Entry<K, V> entry);
-
- public void handlerNoMoreEntry(Map.Entry<K, V> entry);
-
- public void handlerSameEntry(Map.Entry<K, V> entry);
-
- /**
- * if oneMap contain otherMap return true, Otherwise return false;
- * @param oneMap
- * @param otherMap
- * @return
- */
- public boolean contain(Map<K,V> oneMap, Map.Entry<K, V> entry) throws Exception;
- }
-
- public <K,V> void compareTo(Map<K,V> newMap, Map<K,V> oldMap, MapCompareHandler<K,V> handler) throws Exception{
- Set<Map.Entry<K, V>> oldEntrySet = oldMap.entrySet();
- for(Iterator<Map.Entry<K, V>> iter = oldEntrySet.iterator(); iter.hasNext();) {
- Map.Entry<K, V> entry = iter.next();
- if(!handler.contain(newMap, entry)) {
- handler.handlerNoMoreEntry(entry);
- }
- }
-
- Set<Map.Entry<K, V>> newEntrySet = newMap.entrySet();
- for(Iterator<Map.Entry<K, V>> iter = newEntrySet.iterator(); iter.hasNext();) {
- Map.Entry<K, V> entry = iter.next();
- if(handler.contain(oldMap, entry)) {
- handler.handlerSameEntry(entry);
- }else {
- handler.handlerNewEntry(entry);
- }
- }
- }
- }
-
-
-
- 下面是实现了上面 基本比较器的基本类,负责初始化List和Set比较器的handler操作。我在项目中只需要集成下面这个类就好。
-
- package com.bayern;
-
- import java.util.List;
- import java.util.Set;
-
-
- /**
- * 基本 全同步比较器 基础类
- * @author linling
- *
- * @param <VO>
- */
- public abstract class ComparatorAdapter<VO> extends BasicComparator {
-
- @SuppressWarnings("rawtypes")
- protected BasicService basicService;
-
- public abstract void setBasicService(BasicService<VO> basicService);
-
-
- public class ListCompareHandlerAdapter implements ListCompareHandler<VO> {
- public void handlerNewEntry(VO vo) {
- basicService.save(vo);
- }
-
- public void handlerNoMoreEntry(VO vo) {
- basicService.delete(vo);
- }
-
- public void handlerSameEntry(VO vo) {
- basicService.update(vo);
- }
-
- /**
- * 该方法必须被子类覆盖重写
- */
- @Override
- public boolean contain(List<VO> oneList, VO t) throws Exception{
- throw new Exception();
- }
- }
-
- public class SetCompareHandlerAdapter implements SetCompareHandler<VO> {
- public void handlerNewEntry(VO vo) {
- basicService.save(vo);
- }
-
- public void handlerNoMoreEntry(VO vo) {
- basicService.delete(vo);
- }
-
- public void handlerSameEntry(VO vo) {
- basicService.update(vo);
- }
-
- /**
- * 该方法必须被子类覆盖重写
- */
- @Override
- public boolean contain(Set<VO> oneSet, VO t) throws Exception{
- throw new Exception();
- }
-
- }
- }
-
-
-
- 最后,来个实际的比较器:
- package com.bayern;
-
- import java.util.Iterator;
- import java.util.List;
- import java.util.UUID;
-
- import javax.annotation.Resource;
-
- import org.springframework.stereotype.Component;
-
- /**
- * 摄像头比较器
- * @author linling
- *
- */
- @Component(value="cameraComparator")
- public class CameraComparator extends ComparatorAdapter<VoCamera>{
-
-
- public CameraComparator(){
- }
-
- @Resource(name="cameraServiceImpl")
- @Override
- public void setBasicService(BasicService<VoCamera> basicService) {
- super.basicService = basicService;
- }
-
- public class CameraComparatorHandler extends ListCompareHandlerAdapter{
- private long schoolId;
- public CameraComparatorHandler(long schoolId) {
- this.schoolId = schoolId;
- }
- @Override
- public boolean contain(List<VoCamera> oneList, VoCamera t) throws Exception{
- boolean exists = false;
- for(Iterator<VoCamera> iter = oneList.iterator(); iter.hasNext();) {
- VoCamera vo = iter.next();
- if(t.getCameraSignature().equals(vo.getCameraSignature()) && schoolId == vo.getSchoolId().longValue()) {
- if(!CommonUtils.stringIsBlank(vo.getCameraId()) && CommonUtils.stringIsBlank(t.getCameraId())) {
- t.setCameraId(vo.getCameraId());
- }
- exists = true;
- break;
- }
- }
- return exists;
- }
-
- @Override
- public void handlerNewEntry(VoCamera vo) {
- vo.setCameraId(UUID.randomUUID().toString());
- super.handlerNewEntry(vo);
- }
-
- }
-
- }
复制代码 |
|