Skip to content

增加对比后返回对象的工具方法,并进行测试\优化代码 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ public CacheReflectionDiffBuilder(final T lhs, final T rhs, final ToStringStyle
this.diffBuilder = new DiffBuilder<>(lhs, rhs, style);
}

public CacheReflectionDiffBuilder<T> setExcludeFieldSet(Set<String> excludeFieldSet) {

public void setExcludeFieldSet(Set<String> excludeFieldSet) {
// fix set后还是在当前对象,简化代码
this.excludeFieldSet = excludeFieldSet;
return this;
}

@Override
public DiffResult<T> build() {
if (left.equals(right)) {
return diffBuilder.build();
Expand Down
62 changes: 52 additions & 10 deletions src/main/java/com/openquartz/javaobjdiff/DiffUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.openquartz.javaobjdiff;

import com.openquartz.javaobjdiff.util.IteratorUtils;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.openquartz.javaobjdiff.util.IteratorUtils;

/**
* @author svnee
Expand All @@ -13,17 +13,13 @@ public class DiffUtils {
private DiffUtils() {
}

public static <T> String diff(T source, T target, ToStringStyle toStringStyle, String... excludeFiled) {
CacheReflectionDiffBuilder<T> diffBuilder = new CacheReflectionDiffBuilder<>(source, target, toStringStyle);
diffBuilder.setExcludeFieldSet(IteratorUtils.toSet(excludeFiled));
return diffBuilder.build().toString();
public static <T> String diff(T source, T target, ToStringStyle toStringStyle, String... excludeField) {
return buildDiff(source, target, toStringStyle, excludeField).toString();
}

public static <T> String diff(T source, T target, String prefix, String... excludeFiled) {
CacheReflectionDiffBuilder<T> diffBuilder =
new CacheReflectionDiffBuilder<>(source, target, new SimplePrefixToStringStyle(prefix));
diffBuilder.setExcludeFieldSet(IteratorUtils.toSet(excludeFiled));
return diffBuilder.build().toString();
public static <T> String diff(T source, T target, String prefix, String... excludeField) {
ToStringStyle toStringStyle = new SimplePrefixToStringStyle(prefix);
return buildDiff(source, target, toStringStyle, excludeField).toString();
}

public static <T> String diff(T source, T target) {
Expand All @@ -33,4 +29,50 @@ public static <T> String diff(T source, T target) {
public static <T> String diff(T source, T target, String... excludeField) {
return diff(source, target, EMPTY, excludeField);
}

/**
* Compare differences and return {@link DiffResult} for more flexible operation and judgment.
*
* @param source Source object
* @param target Target object
* @param prefix Specified prefix, default is empty string
* @param excludeField Fields to exclude from comparison
* @param <T> Type of objects to compare
* @return {@link DiffResult}
*/
public static <T> DiffResult<T> diffObj(T source, T target, String prefix, String... excludeField) {
ToStringStyle toStringStyle = new SimplePrefixToStringStyle(prefix);
return buildDiff(source, target, toStringStyle, excludeField);
}

/**
* Compare differences and return {@link DiffResult} for more flexible operation and judgment.
*
* @param source Source object
* @param target Target object
* @param <T> Type of objects to compare
* @return {@link DiffResult}
*/
public static <T> DiffResult<T> diffObj(T source, T target) {
return diffObj(source, target, EMPTY, new String[]{});
}

/**
* Compare differences and return {@link DiffResult} for more flexible operation and judgment.
*
* @param source Source object
* @param target Target object
* @param excludeField Fields to exclude from comparison
* @param <T> Type of objects to compare
* @return {@link DiffResult}
*/
public static <T> DiffResult<T> diffObj(T source, T target, String... excludeField) {
return diffObj(source, target, EMPTY, excludeField);
}

private static <T> DiffResult<T> buildDiff(T source, T target, ToStringStyle toStringStyle, String... excludeField) {
CacheReflectionDiffBuilder<T> diffBuilder = new CacheReflectionDiffBuilder<>(source, target, toStringStyle);
diffBuilder.setExcludeFieldSet(IteratorUtils.toSet(excludeField));
return diffBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public SimplePrefixToStringStyle(String prefix) {
setUseIdentityHashCode(false);
}

@Override
protected void appendDetail(StringBuffer buffer, String fieldName, Object value) {
buffer.append(value);
}
Expand All @@ -33,6 +34,7 @@ protected void appendDetail(StringBuffer buffer, String fieldName, Object value)
*
* @param useFieldNames the new useFieldNames flag
*/
@Override
protected void setUseFieldNames(final boolean useFieldNames) {
this.useFieldNames = useFieldNames;
super.setUseFieldNames(useFieldNames);
Expand All @@ -43,6 +45,7 @@ protected void setUseFieldNames(final boolean useFieldNames) {
*
* @param fieldNameValueSeparator the new field name value separator text
*/
@Override
protected void setFieldNameValueSeparator(String fieldNameValueSeparator) {
if (fieldNameValueSeparator == null) {
fieldNameValueSeparator = StringUtils.EMPTY;
Expand Down
22 changes: 16 additions & 6 deletions src/test/java/com/openquartz/javaobjdiff/test/DiffUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.openquartz.javaobjdiff.test;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.openquartz.javaobjdiff.DiffResult;
import com.openquartz.javaobjdiff.DiffUtils;
import com.openquartz.javaobjdiff.test.bean.Address;
import com.openquartz.javaobjdiff.test.bean.Packet;
import com.openquartz.javaobjdiff.test.bean.Person;
import com.openquartz.javaobjdiff.test.bean.Sex;
import com.openquartz.javaobjdiff.util.IteratorUtils;
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class DiffUtilTest {

public static void main(String[] args) {
public static void main(String[] args) {

Address address1 = new Address();
address1.setCity("shanghai");
Expand All @@ -37,8 +39,8 @@ public static void main(String[] args) {
person1.setIdCard("321556199301152110");
person1.setAddress(address1);
person1.setBirthDate(LocalDateTime.now());
person1.setFriendIdList(IteratorUtils.toList(1,2,3));
person1.setPacketList(IteratorUtils.toList(packet1,packet2));
person1.setFriendIdList(IteratorUtils.toList(1, 2, 3));
person1.setPacketList(IteratorUtils.toList(packet1, packet2));


Packet packet3 = new Packet();
Expand All @@ -53,12 +55,20 @@ public static void main(String[] args) {
person2.setIdCard("301556199301152110");
person2.setAddress(address2);
person2.setBirthDate(LocalDateTime.now().plusDays(-10));
person2.setFriendIdList(IteratorUtils.toList(2,3,4));
person2.setFriendIdList(IteratorUtils.toList(2, 3, 4));
person2.setPacketList(IteratorUtils.toList(packet3));

String diff = DiffUtils.diff(person1, person2);
System.out.println(diff);

// test diff obj ,根据需要返回object对象,方便后续操作流程
DiffResult<Person> diffResult = DiffUtils.diffObj(person1, person2);
//根据是否存在差异决定使用的对象
Person personDiff = null;
if (diffResult.isDiff()) {
personDiff = diffResult.getRight();
}
System.out.println(personDiff.toString());
}

}
21 changes: 19 additions & 2 deletions src/test/java/com/openquartz/javaobjdiff/test/bean/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.openquartz.javaobjdiff.test.bean;

import java.time.LocalDateTime;
import java.util.List;

import com.openquartz.javaobjdiff.annotation.DiffBean;
import com.openquartz.javaobjdiff.annotation.DiffCompare;
import com.openquartz.javaobjdiff.annotation.DiffFormat;
Expand All @@ -8,8 +11,6 @@
import com.openquartz.javaobjdiff.formatter.datamask.EmailDataMask;
import com.openquartz.javaobjdiff.formatter.datamask.IdCardDataMask;
import com.openquartz.javaobjdiff.formatter.datamask.MobileDataMask;
import java.time.LocalDateTime;
import java.util.List;

public class Person {

Expand Down Expand Up @@ -121,4 +122,20 @@ public String getIdCard() {
public void setIdCard(String idCard) {
this.idCard = idCard;
}

@Override
public String toString() {
return "{" +
"name:'" + name + '\'' +
", sex:" + sex +
", mobile:'" + mobile + '\'' +
", email:'" + email + '\'' +
", idCard:'" + idCard + '\'' +
", age:" + age +
", birthDate:" + birthDate +
", address:" + address +
", packetList:" + packetList +
", friendIdList:" + friendIdList +
'}';
}
}