Skip to content

Commit

Permalink
支持指定排除或只包含某些字段
Browse files Browse the repository at this point in the history
  • Loading branch information
dadiyang committed Jul 4, 2019
1 parent ae98da3 commit a76c3c3
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.github.dadiyang</groupId>
<artifactId>equator</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>
<parent>
<groupId>org.sonatype.oss</groupId>
Expand Down
57 changes: 53 additions & 4 deletions src/main/java/com/github/dadiyang/equator/AbstractEquator.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ public abstract class AbstractEquator implements Equator {
private static final List<Class<?>> WRAPPER = Arrays.asList(Byte.class, Short.class,
Integer.class, Long.class, Float.class, Double.class, Character.class,
Boolean.class, String.class);
private final List<String> includeFields;
private final List<String> excludeFields;

public AbstractEquator() {
includeFields = Collections.emptyList();
excludeFields = Collections.emptyList();
}

/**
* 指定包含或排除某些字段
*
* @param includeFields 包含字段,若为 null 或空集,则不指定
* @param excludeFields 排除字段,若为 null 或空集,则不指定
*/
public AbstractEquator(List<String> includeFields, List<String> excludeFields) {
this.includeFields = includeFields;
this.excludeFields = excludeFields;
}

/**
* 只要没有不相等的属性,两个对象就全相等
Expand All @@ -35,17 +53,48 @@ public boolean isEquals(Object first, Object second) {
* @return 属性是否相等
*/
protected boolean isFieldEquals(FieldInfo fieldInfo) {
return nullableEquals(fieldInfo.getFirstVal(), fieldInfo.getSecondVal());
// 先判断排除,如果需要排除,则无论在不在包含范围,都一律不比对
if (isExclude(fieldInfo)) {
return true;
}
if (!isInclude(fieldInfo)) {
return true;
}
// 如果有指定需要包含的字段
if (includeFields == null || includeFields.isEmpty() || !includeFields.contains(fieldInfo.getFieldName())) {
return nullableEquals(fieldInfo.getFirstVal(), fieldInfo.getSecondVal());
} else {
return true;
}
}

/**
* 确定是否需要比较这个字段,子类可以扩展这个方法,自定义判断方式
*/
protected boolean isInclude(FieldInfo fieldInfo) {
// 没有指定需要包含的字段,则全部都包含
if (includeFields == null || includeFields.isEmpty()) {
return true;
}
return includeFields.contains(fieldInfo.getFieldName());
}

/**
* 确定是否需要需要排除这个字段,子类可以扩展这个方法,自定义判断方式
*/
protected boolean isExclude(FieldInfo fieldInfo) {
// 如果有指定需要排除的字段,而且当前字段是需要排除字段,则直接返回 true
return excludeFields != null && !excludeFields.isEmpty() && excludeFields.contains(fieldInfo.getFieldName());
}

/**
* 如果原始数据类型的对象则直接进行比对
* 如果简单数据类型的对象则直接进行比对
*
* @param first 对象1
* @param second 对象2
* @return 不同的字段信息,相等返回空集,不等则 FieldInfo 的字段名为对象的类型名称
*/
protected List<FieldInfo> comparePrimitive(Object first, Object second) {
List<FieldInfo> compareSimpleField(Object first, Object second) {
boolean eq = Objects.equals(first, second);
if (eq) {
return Collections.emptyList();
Expand All @@ -64,7 +113,7 @@ protected List<FieldInfo> comparePrimitive(Object first, Object second) {
* @param second 对象2
* @return 是否为原始数据类型
*/
protected boolean isPrimitive(Object first, Object second) {
boolean isSimpleField(Object first, Object second) {
Object obj = first == null ? second : first;
Class<?> clazz = obj.getClass();
return clazz.isPrimitive() || WRAPPER.contains(clazz);
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/github/dadiyang/equator/FieldBaseEquator.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@
* date 2018/11/22
*/
public class FieldBaseEquator extends AbstractEquator {
public FieldBaseEquator() {
}

/**
* 指定包含或排除某些字段
*
* @param includeFields 包含字段,若为 null 或空集,则不指定
* @param excludeFields 排除字段,若为 null 或空集,则不指定
*/
public FieldBaseEquator(List<String> includeFields, List<String> excludeFields) {
super(includeFields, excludeFields);
}

/**
* {@inheritDoc}
*/
Expand All @@ -18,9 +31,9 @@ public List<FieldInfo> getDiffFields(Object first, Object second) {
if (first == second) {
return Collections.emptyList();
}
// 先尝试判断是否为原始数据类型
if (isPrimitive(first, second)) {
return comparePrimitive(first, second);
// 先尝试判断是否为简单数据类型
if (isSimpleField(first, second)) {
return compareSimpleField(first, second);
}
Object obj = first == null ? second : first;
Class<?> clazz = obj.getClass();
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/github/dadiyang/equator/GetterBaseEquator.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public class GetterBaseEquator extends AbstractEquator {
private static final String GET_IS = "get|is";
private static final String GET_CLASS = "getClass";

public GetterBaseEquator() {
}

/**
* 指定包含或排除某些字段
*
* @param includeFields 包含字段,若为 null 或空集,则不指定
* @param excludeFields 排除字段,若为 null 或空集,则不指定
*/
public GetterBaseEquator(List<String> includeFields, List<String> excludeFields) {
super(includeFields, excludeFields);
}

/**
* {@inheritDoc}
*/
Expand All @@ -26,9 +39,9 @@ public List<FieldInfo> getDiffFields(Object first, Object second) {
if (first == null && second == null) {
return Collections.emptyList();
}
// 先尝试判断是否为原始数据类型
if (isPrimitive(first, second)) {
return comparePrimitive(first, second);
// 先尝试判断是否为普通数据类型
if (isSimpleField(first, second)) {
return compareSimpleField(first, second);
}
List<FieldInfo> diffField = new LinkedList<>();
Object obj = first == null ? second : first;
Expand Down Expand Up @@ -97,7 +110,7 @@ private String uncapitalize(final String str) {
if (firstCodepoint == newCodePoint) {
return str;
}
final int newCodePoints[] = new int[strLen];
final int[] newCodePoints = new int[strLen];
int outOffset = 0;
newCodePoints[outOffset++] = newCodePoint;
for (int inOffset = Character.charCount(firstCodepoint); inOffset < strLen; ) {
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/com/github/dadiyang/equator/EquatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,25 @@ public static List<Object[]> getParams() {
new FieldInfo("hobbies", String[].class, null, hobby2),
new FieldInfo("expired", boolean.class, null, false));
ps.add(params);

// 测试排除字段
Date now = new Date();
params = new Object[5];
params[0] = new GetterBaseEquator(null, Arrays.asList("id", "username"));
params[1] = new User(0, "noteq", now, new String[]{"program", "coding"});
params[2] = new User(1, "yang", now, new String[]{"program", "coding"});
params[3] = true;
params[4] = Collections.emptyList();
ps.add(params);

// 测试只包含特定字段
params = new Object[5];
params[0] = new GetterBaseEquator(Collections.singletonList("expireTime"), null);
params[1] = new User(0, "noteq2", now, new String[]{"program", "coding"});
params[2] = new User(1, "yang", now, new String[]{"program", "coding"});
params[3] = true;
params[4] = Collections.emptyList();
ps.add(params);
return ps;
}

Expand Down Expand Up @@ -132,6 +151,20 @@ public void getDiffFields() {
assertArrayEquals("不等的属性与预期不一致", expectDiffField.toArray(), fields.toArray());
}

@Test
public void name() {
Equator equator = new GetterBaseEquator() {
@Override
protected boolean isFieldEquals(FieldInfo fieldInfo) {
if ("id".equalsIgnoreCase(fieldInfo.getFieldName())) {
return true;
}
return super.isFieldEquals(fieldInfo);
}
};
equator.isEquals(user1, user2);
}

private Comparator<? super FieldInfo> getStringComparator() {
return new Comparator<FieldInfo>() {
@Override
Expand Down

0 comments on commit a76c3c3

Please sign in to comment.