All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.tinygroup.different.impl.AbstractDifferentImpl Maven / Gradle / Ivy

The newest version!
/**
 *  Copyright (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  Licensed under the GPL, Version 3.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.gnu.org/licenses/gpl.html
 *
 *  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.
 * --------------------------------------------------------------------------
 *  版权 (c) 1997-2013, tinygroup.org ([email protected]).
 *
 *  本开源软件遵循 GPL 3.0 协议;
 *  如果您不遵循此协议,则不被允许使用此文件。
 *  你可以从下面的地址获取完整的协议文本
 *
 *       http://www.gnu.org/licenses/gpl.html
 */
package org.tinygroup.different.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.tinygroup.different.DataWithOperation;
import org.tinygroup.different.Different;

/**
 * 差异化比较抽象类
* 主要用于实现公共方法或通家长方法 * * @author luoguo * * @param * 要比较的实体 */ public abstract class AbstractDifferentImpl> implements Different { protected List oldList = null; protected Comparator comparator = null; public void setObjectComparator(Comparator comparator) { this.comparator = comparator; } public List> differentOrdered(List newList) { List> result = null; if (newList != null) { Collections.sort(newList); } if (this.oldList == null) {// 如果原来没有,则直接返回 result = getNewList(newList); } else if (newList == null) {// 如果新的没有,则置空 result = getRemoveList(oldList); } else { result = getDifferentList(this.oldList, newList); } this.oldList = newList; return result; } protected List> getNewList(List newList) { List> list = new ArrayList>(); if (newList != null) { for (T data : newList) { list.add(new DataWithOperation(DataWithOperation.ADD, data)); } } return list; } protected List> getRemoveList(List oldList) { List> list = new ArrayList>(); for (T data : oldList) { list.add(new DataWithOperation(DataWithOperation.REMOVE, data)); } return list; } protected int compare(T oldObject, T newObject) { if (comparator != null) { return comparator.compare(oldObject, newObject); } else { return oldObject.compareTo(newObject); } } protected List> getDifferentList(List oldList, List newList) { List> result = new ArrayList>(); int oldPos = 0, newPos = 0; while (oldPos < oldList.size() && newPos < newList.size()) { T oldObject = oldList.get(oldPos); T newObject = newList.get(newPos); if (oldObject.compareTo(newObject) == 0) { // 如果相等,则两个同时相加 if (compare(oldObject, newObject) != 0) { result.add(new DataWithOperation( DataWithOperation.MODIFY, newObject)); } oldPos++; newPos++; continue; } if (oldObject.compareTo(newObject) < 0) { result.add(new DataWithOperation(DataWithOperation.REMOVE, oldList.get(oldPos++))); continue; } if (newObject.compareTo(oldObject) < 0) { result.add(new DataWithOperation(DataWithOperation.ADD, newList.get(newPos++))); continue; } } while (newPos < newList.size()) { result.add(new DataWithOperation(DataWithOperation.ADD, newList .get(newPos++))); } while (oldPos < oldList.size()) { result.add(new DataWithOperation(DataWithOperation.REMOVE, oldList.get(oldPos++))); } return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy