关于 DiffUtil 笔记
准备2个Model
public class LoginModel implements Serializable {
private static final long serialVersionUID = -8532417847222176161L;
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "LoginModel{"
+ "username='" + username + "'"
+ ", password='" + password + "'"
+ "}";
}
}
public class DiffResult<T> {
private List<T> addedList;
private List<T> changedList;
private List<T> deletedList;
public List<T> getAddedList() {
return addedList;
}
public DiffResult<T> setAddedList(List<T> addedList) {
this.addedList = addedList;
return this;
}
public List<T> getChangedList() {
return changedList;
}
public DiffResult<T> setChangedList(List<T> changedList) {
this.changedList = changedList;
return this;
}
public List<T> getDeletedList() {
return deletedList;
}
public DiffResult<T> setDeletedList(List<T> deletedList) {
this.deletedList = deletedList;
return this;
}
}
创建DiffUtil
public class DiffUtil {
public static <T> DiffResult<T> diffList(List<T> baseList,
List<T> targetList,
Function<T, Object> primaryKeyExtractor,
Comparator<T> elementComparator) {
DiffResult<T> checkResult = checkEmptyAndReturn(baseList, targetList);
if (checkResult != null) {
return checkResult;
}
Map<Object,T> baseMap = new HashMap<>(4096);
for(T base : baseList){
Object key = primaryKeyExtractor.apply(base);
baseMap.put(key,base);
}
List<T> addedList = new ArrayList<>();
List<T> deletedList = new ArrayList<>();
List<T> changedList = new ArrayList<>();
for (T target : targetList) {
Object key = primaryKeyExtractor.apply(target);
T base = baseMap.get(key);
if(base == null){
addedList.add(target);
}else{
baseMap.remove(key);
if (elementComparator.compare(base, target) != 0) {
changedList.add(target);
}
}
}
Set<Map.Entry<Object, T>> entrySet = baseMap.entrySet();
if(ObjectUtils.isNotEmpty(entrySet)){
for(Map.Entry<Object, T> entry:entrySet){
deletedList.add(entry.getValue());
}
}
return new DiffResult<T>()
.setChangedList(changedList)
.setAddedList(addedList)
.setDeletedList(deletedList);
}
private static <T> DiffResult<T> checkEmptyAndReturn(List<T> baseList, List<T> targetList) {
if (ObjectUtils.isEmpty(baseList) && ObjectUtils.isEmpty(targetList)) {
return new DiffResult<T>();
}
if (ObjectUtils.isEmpty(baseList) && ObjectUtils.isNotEmpty(targetList)) {
return new DiffResult<T>();
}
if (ObjectUtils.isNotEmpty(baseList) && ObjectUtils.isEmpty(targetList)) {
return new DiffResult<T>();
}
return null;
}
}
TEST
public static void main(String[] args) {
List<LoginModel> command = new ArrayList<>();
LoginModel loginModel1 = new LoginModel();
loginModel1.setUsername("AAA");
loginModel1.setPassword("123");
LoginModel loginModel2 = new LoginModel();
loginModel2.setUsername("BBB");
loginModel2.setPassword("123");
command.add(loginModel1);
command.add(loginModel2);
System.out.println("command: " + command);
List<LoginModel> db = new ArrayList<>();
LoginModel loginModel3 = new LoginModel();
loginModel3.setUsername("AAA");
loginModel3.setPassword("123");
LoginModel loginModel4 = new LoginModel();
loginModel4.setUsername("CCC");
loginModel4.setPassword("123");
db.add(loginModel3);
db.add(loginModel4);
System.out.println("db: " + db);
Function<LoginModel, Object> function = LoginModel::getUsername;
DiffResult<LoginModel> diffResult = DiffUtil.diffList(db, command, function, new Comparator<LoginModel>() {
@Override
public int compare(LoginModel o1, LoginModel o2) {
int i = 0;
if (o1.getPassword().equals(o2.getPassword())) {
i = 1;
}
return i;
}
});
System.out.println(diffResult);
}