public class CompareToBuilder extends Object implements Builder<Integer>
Comparable.compareTo(Object) 方法的辅助工具
在Bean对象中,所有相关字段都参与比对,继承的字段不参与。使用方法如下:
public class MyClass {
String field1;
int field2;
boolean field3;
...
public int compareTo(Object o) {
MyClass myClass = (MyClass) o;
return new CompareToBuilder()
.appendSuper(super.compareTo(o)
.append(this.field1, myClass.field1)
.append(this.field2, myClass.field2)
.append(this.field3, myClass.field3)
.toComparison();
}
}
字段值按照顺序比较,如果某个字段返回非0结果,比较终止,使用toComparison()返回结果,后续比较忽略。
也可以使用reflectionCompare 方法通过反射比较字段,使用方法如下:
public int compareTo(Object o) {
return CompareToBuilder.reflectionCompare(this, o);
}
| Constructor and Description |
|---|
CompareToBuilder()
构造,构造后调用append方法增加比较项,然后调用
toComparison()获取结果 |
| Modifier and Type | Method and Description |
|---|---|
CompareToBuilder |
append(boolean[] left,
boolean[] right)
附加到builder的深层比较,两个boolean数组.
|
CompareToBuilder |
append(boolean left,
boolean right)
附加到builder的深层比较,两个boolean值比较
|
CompareToBuilder |
append(byte[] left,
byte[] right)
附加到builder的深层比较,两个byte数组.
|
CompareToBuilder |
append(byte left,
byte right)
附加到builder的深层比较,两个byte值比较
|
CompareToBuilder |
append(char[] left,
char[] right)
附加到builder的深层比较,两个char数组.
|
CompareToBuilder |
append(char left,
char right)
附加到builder的深层比较,两个char值比较
|
CompareToBuilder |
append(double[] left,
double[] right)
附加到builder的深层比较,两个double数组.
|
CompareToBuilder |
append(double left,
double right)
附加到builder的深层比较,两个double值比较
|
CompareToBuilder |
append(float[] left,
float[] right)
附加到builder的深层比较,两个float数组.
|
CompareToBuilder |
append(float left,
float right)
附加到builder的深层比较,两个float值比较
|
CompareToBuilder |
append(int[] left,
int[] right)
附加到builder的深层比较,两个int数组.
|
CompareToBuilder |
append(int left,
int right)
附加到builder的深层比较,两个int值比较
|
CompareToBuilder |
append(long[] left,
long[] right)
附加到builder的深层比较,两个long数组.
|
CompareToBuilder |
append(long left,
long right)
附加到builder的深层比较,两个long值比较
|
CompareToBuilder |
append(Object[] left,
Object[] right)
附加到builder的深层比较,两个Object数组
使用==检查数组是否相同
检查是否为null, null小于非null
检查数组长度,长度较短的数组小于长度较长的数组
使用
append(Object, Object)逐个元素检查数组内容
|
CompareToBuilder |
append(Object[] left,
Object[] right,
Comparator<?> comparator)
附加到builder的深层比较,两个Object数组
使用==检查数组是否相同
检查是否为null, null小于非null
检查数组长度,长度较短的数组小于长度较长的数组
使用
append(Object, Object, Comparator)逐个元素检查数组内容
|
CompareToBuilder |
append(Object left,
Object right)
附加到builder的深层比较,两个Object数组
使用==检查数组是否相同
检查是否为null, null小于非null
检查数组长度,长度较短的数组小于长度较长的数组
使用
append(Object, Object)逐个元素检查数组内容
|
CompareToBuilder |
append(Object left,
Object right,
Comparator<?> comparator)
附加到builder的深层比较,两个Object数组
使用==检查数组是否相同
检查是否为null, null小于非null
检查数组长度,长度较短的数组小于长度较长的数组
使用
append(Object, Object, Comparator)逐个元素检查数组内容
|
CompareToBuilder |
append(short[] left,
short[] right)
附加到builder的深层比较,两个short数组.
|
CompareToBuilder |
append(short left,
short right)
附加到builder的深层比较,两个short值比较
|
CompareToBuilder |
appendSuper(int superCompareTo)
附加到 builder compareTo(Object) 超类的结果。
|
Integer |
build()
返回一个负整数、一个正整数或零
builder判断左边小于、大于或等于右边。
|
static int |
reflectionCompare(Object left,
Object right)
通过反射比较两个Bean对象,对象字段可以为private。比较规则如下:
static字段不比较
Transient字段不参与比较
父类字段参与比较
|
static int |
reflectionCompare(Object left,
Object right,
boolean compareTransients)
Compares two
Objects via reflection. |
static int |
reflectionCompare(Object left,
Object right,
boolean compareTransients,
Class<?> reflectUpToClass,
String... excludeFields)
Compares two
Objects via reflection. |
static int |
reflectionCompare(Object left,
Object right,
Collection<String> excludeFields)
Compares two
Objects via reflection. |
static int |
reflectionCompare(Object left,
Object right,
String... excludeFields)
Compares two
Objects via reflection. |
int |
toComparison()
返回一个负整数、一个正整数或零
builder判断左边小于、大于或等于右边。
|
public CompareToBuilder()
toComparison()获取结果public static int reflectionCompare(Object left, Object right)
如果被比较的两个对象都为null,被认为相同。
left - 第一个对象right - 第二个对象left
is less than, equal to, or greater than rightNullPointerException - if either (but not both) parameters are
nullClassCastException - if right is not assignment-compatible
with leftpublic static int reflectionCompare(Object left, Object right, boolean compareTransients)
Compares two Objects via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. This will fail under a
security manager unless the appropriate permissions are set.
compareTransients is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.If both left and right are null,
they are considered equal.
left - left-hand objectright - right-hand objectcompareTransients - whether to compare transient fieldsleft
is less than, equal to, or greater than rightNullPointerException - if either left or right
(but not both) is nullClassCastException - if right is not assignment-compatible
with leftpublic static int reflectionCompare(Object left, Object right, Collection<String> excludeFields)
Compares two Objects via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. This will fail under a
security manager unless the appropriate permissions are set.
compareTransients is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.If both left and right are null,
they are considered equal.
left - left-hand objectright - right-hand objectexcludeFields - Collection of String fields to excludeleft
is less than, equal to, or greater than rightNullPointerException - if either left or right
(but not both) is nullClassCastException - if right is not assignment-compatible
with leftpublic static int reflectionCompare(Object left, Object right, String... excludeFields)
Compares two Objects via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. This will fail under a
security manager unless the appropriate permissions are set.
compareTransients is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.If both left and right are null,
they are considered equal.
left - left-hand objectright - right-hand objectexcludeFields - array of fields to excludeleft
is less than, equal to, or greater than rightNullPointerException - if either left or right
(but not both) is nullClassCastException - if right is not assignment-compatible
with leftpublic static int reflectionCompare(Object left, Object right, boolean compareTransients, Class<?> reflectUpToClass, String... excludeFields)
Compares two Objects via reflection.
Fields can be private, thus AccessibleObject.setAccessible
is used to bypass normal access control checks. This will fail under a
security manager unless the appropriate permissions are set.
compareTransients is true,
compares transient members. Otherwise ignores them, as they
are likely derived fields.reflectUpToClass.
If reflectUpToClass is null, compares all superclass fields.If both left and right are null,
they are considered equal.
left - left-hand objectright - right-hand objectcompareTransients - whether to compare transient fieldsreflectUpToClass - last superclass for which fields are comparedexcludeFields - fields to excludeleft
is less than, equal to, or greater than rightNullPointerException - if either left or right
(but not both) is nullClassCastException - if right is not assignment-compatible
with leftreflectionCompare(Object, Object, boolean, Class))public CompareToBuilder appendSuper(int superCompareTo)
superCompareTo - 调用 super.compareTo(Object)结果public CompareToBuilder append(Object left, Object right)
append(Object, Object)逐个元素检查数组内容left - 数值right - 数值public CompareToBuilder append(Object left, Object right, Comparator<?> comparator)
append(Object, Object, Comparator)逐个元素检查数组内容left - 数值right - 数值comparator - 用来比较数值元素的比较器.public CompareToBuilder append(long left, long right)
left - 数值right - 数值public CompareToBuilder append(int left, int right)
left - 数值right - 数值public CompareToBuilder append(short left, short right)
left - 数值right - 数值public CompareToBuilder append(char left, char right)
left - 数值right - 数值public CompareToBuilder append(byte left, byte right)
left - 数值right - 数值public CompareToBuilder append(double left, double right)
left - 数值right - 数值public CompareToBuilder append(float left, float right)
left - 数值right - 数值public CompareToBuilder append(boolean left, boolean right)
left - 数值right - 数值public CompareToBuilder append(Object[] left, Object[] right)
append(Object, Object)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(Object[] left, Object[] right, Comparator<?> comparator)
append(Object, Object, Comparator)逐个元素检查数组内容left - 数组right - 数组comparator - 用来比较数组元素的比较器.public CompareToBuilder append(long[] left, long[] right)
append(long, long)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(int[] left, int[] right)
append(int, int)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(short[] left, short[] right)
append(short, short)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(char[] left, char[] right)
append(char, char)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(byte[] left, byte[] right)
append(byte, byte)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(double[] left, double[] right)
append(double, double)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(float[] left, float[] right)
append(float, float)逐个元素检查数组内容left - 数组right - 数组public CompareToBuilder append(boolean[] left, boolean[] right)
append(boolean, boolean)逐个元素检查数组内容left - 数组right - 数组public int toComparison()
build()public Integer build()
build in interface Builder<Integer>toComparison()Copyright © 2019. All rights reserved.