|
|
@@ -8,7 +8,7 @@ import java.lang.reflect.Modifier;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
-/**
|
|
|
+/**
|
|
|
* 用于构建 {@link java.lang.Comparable#compareTo(Object)} 方法的辅助工具
|
|
|
*
|
|
|
* <p>
|
|
|
@@ -33,7 +33,7 @@ import java.util.Comparator;
|
|
|
* }
|
|
|
* }
|
|
|
* </pre>
|
|
|
- *
|
|
|
+ *
|
|
|
* 字段值按照顺序比较,如果某个字段返回非0结果,比较终止,使用{@code toComparison()}返回结果,后续比较忽略。
|
|
|
*
|
|
|
* <p>
|
|
|
@@ -52,7 +52,7 @@ import java.util.Comparator;
|
|
|
*/
|
|
|
public class CompareToBuilder implements Builder<Integer> {
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
-
|
|
|
+
|
|
|
/** 当前比较状态 */
|
|
|
private int comparison;
|
|
|
|
|
|
@@ -64,7 +64,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
}
|
|
|
|
|
|
//-----------------------------------------------------------------------
|
|
|
- /**
|
|
|
+ /**
|
|
|
* 通过反射比较两个Bean对象,对象字段可以为private。比较规则如下:
|
|
|
*
|
|
|
* <ul>
|
|
|
@@ -93,7 +93,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* <p>Compares two <code>Object</code>s via reflection.</p>
|
|
|
*
|
|
|
* <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
|
|
|
- * is used to bypass normal access control checks. This will fail under a
|
|
|
+ * is used to bypass normal access control checks. This will fail under a
|
|
|
* security manager unless the appropriate permissions are set.</p>
|
|
|
*
|
|
|
* <ul>
|
|
|
@@ -125,7 +125,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* <p>Compares two <code>Object</code>s via reflection.</p>
|
|
|
*
|
|
|
* <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
|
|
|
- * is used to bypass normal access control checks. This will fail under a
|
|
|
+ * is used to bypass normal access control checks. This will fail under a
|
|
|
* security manager unless the appropriate permissions are set.</p>
|
|
|
*
|
|
|
* <ul>
|
|
|
@@ -158,7 +158,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* <p>Compares two <code>Object</code>s via reflection.</p>
|
|
|
*
|
|
|
* <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
|
|
|
- * is used to bypass normal access control checks. This will fail under a
|
|
|
+ * is used to bypass normal access control checks. This will fail under a
|
|
|
* security manager unless the appropriate permissions are set.</p>
|
|
|
*
|
|
|
* <ul>
|
|
|
@@ -191,7 +191,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* <p>Compares two <code>Object</code>s via reflection.</p>
|
|
|
*
|
|
|
* <p>Fields can be private, thus <code>AccessibleObject.setAccessible</code>
|
|
|
- * is used to bypass normal access control checks. This will fail under a
|
|
|
+ * is used to bypass normal access control checks. This will fail under a
|
|
|
* security manager unless the appropriate permissions are set.</p>
|
|
|
*
|
|
|
* <ul>
|
|
|
@@ -220,10 +220,10 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* @since 2.2 (2.0 as <code>reflectionCompare(Object, Object, boolean, Class)</code>)
|
|
|
*/
|
|
|
public static int reflectionCompare(
|
|
|
- final Object lhs,
|
|
|
- final Object rhs,
|
|
|
- final boolean compareTransients,
|
|
|
- final Class<?> reflectUpToClass,
|
|
|
+ final Object lhs,
|
|
|
+ final Object rhs,
|
|
|
+ final boolean compareTransients,
|
|
|
+ final Class<?> reflectUpToClass,
|
|
|
final String... excludeFields) {
|
|
|
|
|
|
if (lhs == rhs) {
|
|
|
@@ -248,7 +248,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
/**
|
|
|
* <p>Appends to <code>builder</code> the comparison of <code>lhs</code>
|
|
|
* to <code>rhs</code> using the fields defined in <code>clazz</code>.</p>
|
|
|
- *
|
|
|
+ *
|
|
|
* @param lhs left-hand object
|
|
|
* @param rhs right-hand object
|
|
|
* @param clazz <code>Class</code> that defines fields to be compared
|
|
|
@@ -263,7 +263,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
final CompareToBuilder builder,
|
|
|
final boolean useTransients,
|
|
|
final String[] excludeFields) {
|
|
|
-
|
|
|
+
|
|
|
final Field[] fields = clazz.getDeclaredFields();
|
|
|
AccessibleObject.setAccessible(fields, true);
|
|
|
for (int i = 0; i < fields.length && builder.comparison == 0; i++) {
|
|
|
@@ -299,7 +299,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
comparison = superCompareTo;
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//-----------------------------------------------------------------------
|
|
|
/**
|
|
|
* <p>Appends to the <code>builder</code> the comparison of
|
|
|
@@ -311,7 +311,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* a <code>null</code> object is less than a non-<code>null</code> object</li>
|
|
|
* <li>Check the object contents</li>
|
|
|
* </ol>
|
|
|
- *
|
|
|
+ *
|
|
|
* <p><code>lhs</code> must either be an array or implement {@link Comparable}.</p>
|
|
|
*
|
|
|
* @param lhs left-hand object
|
|
|
@@ -440,7 +440,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
/**
|
|
|
* Appends to the <code>builder</code> the comparison of
|
|
|
* two <code>short</code>s.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param lhs left-hand value
|
|
|
* @param rhs right-hand value
|
|
|
* @return this - used to chain append calls
|
|
|
@@ -472,7 +472,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
/**
|
|
|
* Appends to the <code>builder</code> the comparison of
|
|
|
* two <code>byte</code>s.
|
|
|
- *
|
|
|
+ *
|
|
|
* @param lhs left-hand value
|
|
|
* @param rhs right-hand value
|
|
|
* @return this - used to chain append calls
|
|
|
@@ -574,7 +574,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
public CompareToBuilder append(final Object[] lhs, final Object[] rhs) {
|
|
|
return append(lhs, rhs, null);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* <p>Appends to the <code>builder</code> the deep comparison of
|
|
|
* two <code>Object</code> arrays.</p>
|
|
|
@@ -949,7 +949,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* the <code>builder</code> has judged the "left-hand" side
|
|
|
* as less than, greater than, or equal to the "right-hand"
|
|
|
* side.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return final comparison result
|
|
|
* @see #build()
|
|
|
*/
|
|
|
@@ -962,7 +962,7 @@ public class CompareToBuilder implements Builder<Integer> {
|
|
|
* the <code>builder</code> has judged the "left-hand" side
|
|
|
* as less than, greater than, or equal to the "right-hand"
|
|
|
* side.
|
|
|
- *
|
|
|
+ *
|
|
|
* @return final comparison result as an Integer
|
|
|
* @see #toComparison()
|
|
|
* @since 3.0
|