Skip to content
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

8277868: Use Comparable.compare() instead of surrogate code #6575

Closed
wants to merge 8 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/math/BigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -4405,7 +4405,7 @@ private static int longCompareMagnitude(long x, long y) {
x = -x;
if (y < 0)
y = -y;
return (x < y) ? -1 : ((x == y) ? 0 : 1);
return Long.compare(x, y);
}

private static int saturateLong(long s) {
Expand Down
8 changes: 1 addition & 7 deletions src/java.base/share/classes/java/net/CookieManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,7 @@ public int compare(HttpCookie c1, HttpCookie c2) {
// Check creation time. Sort older first
long creation1 = c1.getCreationTime();
long creation2 = c2.getCreationTime();
if (creation1 < creation2) {
return -1;
}
if (creation1 > creation2) {
return 1;
}
return 0;
return Long.compare(creation1, creation2);
}
}
}
3 changes: 1 addition & 2 deletions src/java.base/share/classes/java/util/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -3416,8 +3416,7 @@ private void updateTime() {
}

private int compareTo(long t) {
long thisTime = getMillisOf(this);
return (thisTime > t) ? 1 : (thisTime == t) ? 0 : -1;
return Long.compare(getMillisOf(this), t);
}

private static long getMillisOf(Calendar calendar) {
Expand Down
12 changes: 3 additions & 9 deletions src/java.base/share/classes/java/util/Date.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1994, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -26,18 +26,13 @@
package java.util;

import java.text.DateFormat;
import java.time.LocalDate;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.lang.ref.SoftReference;
import java.time.Instant;
import sun.util.calendar.BaseCalendar;
import sun.util.calendar.CalendarDate;
import sun.util.calendar.CalendarSystem;
import sun.util.calendar.CalendarUtils;
import sun.util.calendar.Era;
import sun.util.calendar.Gregorian;
import sun.util.calendar.ZoneInfo;

/**
Expand Down Expand Up @@ -975,10 +970,9 @@ static final long getMillisOf(Date date) {
* @since 1.2
* @throws NullPointerException if {@code anotherDate} is null.
*/
@Override
public int compareTo(Date anotherDate) {
long thisTime = getMillisOf(this);
long anotherTime = getMillisOf(anotherDate);
return (thisTime<anotherTime ? -1 : (thisTime==anotherTime ? 0 : 1));
return Long.compare(getMillisOf(this), getMillisOf(anotherDate));
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/java.base/share/classes/java/util/UUID.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -512,10 +512,7 @@ public boolean equals(Object obj) {
public int compareTo(UUID val) {
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
return (this.mostSigBits < val.mostSigBits ? -1 :
(this.mostSigBits > val.mostSigBits ? 1 :
(this.leastSigBits < val.leastSigBits ? -1 :
(this.leastSigBits > val.leastSigBits ? 1 :
0))));
int mostSigBits = Long.compare(this.mostSigBits, val.mostSigBits);
return mostSigBits != 0 ? mostSigBits : Long.compare(this.leastSigBits, val.leastSigBits);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1533,12 +1533,7 @@ private NumericShaper(Range defaultContext, Set<Range> ranges) {
rangeArray = rangeSet.toArray(new Range[rangeSet.size()]);
if (rangeArray.length > BSEARCH_THRESHOLD) {
// sort rangeArray for binary search
Arrays.sort(rangeArray,
new Comparator<Range>() {
public int compare(Range s1, Range s2) {
return s1.base > s2.base ? 1 : s1.base == s2.base ? 0 : -1;
}
});
Arrays.sort(rangeArray, Comparator.comparingInt(s -> s.base));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/java.desktop/share/classes/java/awt/geom/Line2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ public static int relativeCCW(double x1, double y1,
}
}
}
return (ccw < 0.0) ? -1 : ((ccw > 0.0) ? 1 : 0);
return java.lang.Double.compare(ccw, 0.0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -27,16 +27,11 @@

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.dnd.*;
import java.awt.event.*;
import java.util.Enumeration;
import java.util.EventObject;
import java.util.Hashtable;
import java.util.TooManyListenersException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import javax.swing.text.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
import sun.swing.SwingUtilities2;
Expand Down Expand Up @@ -221,8 +216,8 @@ private static class Actions extends UIAction {
this.inSelection = true;

// look at the sign of dx and dy only
dx = sign(dx);
dy = sign(dy);
dx = Integer.signum(dx);
dy = Integer.signum(dy);

// make sure one is zero, but not both
assert (dx == 0 || dy == 0) && !(dx == 0 && dy == 0);
Expand Down Expand Up @@ -250,10 +245,6 @@ private void moveWithinTableRange(JTable table, int dx, int dy) {
leadColumn = clipToRange(leadColumn+dx, 0, table.getColumnCount());
}

private static int sign(int num) {
return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
}

/**
* Called to move within the selected range of the given JTable.
* This method uses the table's notion of selection, which is
Expand Down
10 changes: 2 additions & 8 deletions src/java.desktop/share/classes/javax/swing/text/GapContent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -504,13 +504,7 @@ protected void shiftGapEndUp(int newGapEnd) {
* @return < 0 if o1 < o2, 0 if the same, > 0 if o1 > o2
*/
final int compare(MarkData o1, MarkData o2) {
if (o1.index < o2.index) {
return -1;
} else if (o1.index > o2.index) {
return 1;
} else {
return 0;
}
return Integer.compare(o1.index, o2.index);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions src/java.desktop/share/classes/sun/awt/geom/Curve.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -730,13 +730,7 @@ public static double round(double v) {
}

public static int orderof(double x1, double x2) {
if (x1 < x2) {
return -1;
}
if (x1 > x2) {
return 1;
}
return 0;
return Double.compare(x1, x2);
}

public static long signeddiffbits(double y1, double y2) {
Expand Down
16 changes: 3 additions & 13 deletions src/java.desktop/share/classes/sun/java2d/Spans.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2000, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -315,19 +315,9 @@ boolean contains(float pos) {
* position. The end position is ignored
* in this ranking.
*/
@Override
public int compareTo(Span otherSpan) {
float otherStart = otherSpan.getStart();
int result;

if (mStart < otherStart) {
result = -1;
} else if (mStart > otherStart) {
result = 1;
} else {
result = 0;
}

return result;
return Float.compare(mStart, otherSpan.getStart());
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -79,7 +79,7 @@ public int compare(GraphicsPrimitive o1, GraphicsPrimitive o2) {
int id1 = o1.getUniqueID();
int id2 = o2.getUniqueID();

return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
return Integer.compare(id1, id2);
}
};

Expand All @@ -88,7 +88,7 @@ public int compare(Object o1, Object o2) {
int id1 = ((GraphicsPrimitive) o1).getUniqueID();
int id2 = ((PrimitiveSpec) o2).uniqueID;

return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
return Integer.compare(id1, id2);
}
};

Expand Down