Skip to content

Commit

Permalink
fix Compare bug
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Oct 17, 2018
1 parent 2f38741 commit f632188
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* 【core】 修复ReflectUtil获取空参数方法导致的问题(issue#INN5W@Gitee)
* 【json】 修复JSONArray.toList方法导致的问题(issue#INO3F@Gitee)
* 【core】 修复NumberUtil.parseLong中0转换问题方法导致的问题(issue#INO3F@Gitee)
* 【core】 修复CompareUtil循环引用问题(issue#180@Github)

-------------------------------------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ public static <T extends Comparable<? super T>> int compare(T c1, T c2) {
* @param <T> 被比较对象类型(必须实现Comparable接口)
* @param c1 对象1,可以为{@code null}
* @param c2 对象2,可以为{@code null}
* @param nullGreater 当被比较对象为null时是否排在前面
* @param isNullGreater 当被比较对象为null时是否排在前面
* @return 比较结果,如果c1 &lt; c2,返回数小于0,c1==c2返回0,c1 &gt; c2 大于0
* @see java.util.Comparator#compare(Object, Object)
*/
public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean nullGreater) {
public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean isNullGreater) {
if (c1 == c2) {
return 0;
} else if (c1 == null) {
return nullGreater ? 1 : -1;
return isNullGreater ? 1 : -1;
} else if (c2 == null) {
return nullGreater ? -1 : 1;
return isNullGreater ? -1 : 1;
}
return c1.compareTo(c2);
}
Expand All @@ -51,6 +51,7 @@ public static <T extends Comparable<? super T>> int compare(T c1, T c2, boolean
* @param isNullGreater null值是否做为最大值
* @return 比较结果,如果o1 &lt; o2,返回数小于0,o1==o2返回0,o1 &gt; o2 大于0
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> int compare(T o1, T o2, boolean isNullGreater) {
if (o1 == o2) {
return 0;
Expand All @@ -62,7 +63,7 @@ public static <T> int compare(T o1, T o2, boolean isNullGreater) {

if(o1 instanceof Comparable && o2 instanceof Comparable) {
//如果bean可比较,直接比较bean
return compare((Comparable<?>)o1, (Comparable<?>)o2, isNullGreater);
return ((Comparable)o1).compareTo(o2);
}

if(o1.equals(o2)) {
Expand Down

0 comments on commit f632188

Please sign in to comment.