Skip to content

Commit

Permalink
[MINOR] Improve code quality of ComparableVersion class (apache#10934)
Browse files Browse the repository at this point in the history
Co-authored-by: hanmo1 <ISFA-9844>
Co-authored-by: Y Ethan Guo <[email protected]>
  • Loading branch information
balloon72 and yihua authored Sep 11, 2024
1 parent 276133b commit ac79b14
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@
* This class is copied from {@code org.apache.hadoop.util.ComparableVersion} to avoid Hadoop dependency.
*/
public class ComparableVersion
implements Comparable<ComparableVersion> {
implements Comparable<ComparableVersion> {
private static final String INVALID_ITEM = "invalid item: ";

private String value;

private String canonical;
Expand All @@ -81,8 +83,8 @@ private interface Item {
* Represents a numeric item in the version item list.
*/
private static class IntegerItem
implements ComparableVersion.Item {
private static final BigInteger BIG_INTEGER_ZERO = new BigInteger("0");
implements ComparableVersion.Item {
private static final BigInteger BIG_INTEGER_ZERO = BigInteger.ZERO;

private final BigInteger value;

Expand Down Expand Up @@ -120,7 +122,7 @@ public int compareTo(ComparableVersion.Item item) {
return 1; // 1.1 > 1-1

default:
throw new RuntimeException("invalid item: " + item.getClass());
throw new RuntimeException(INVALID_ITEM + item.getClass());
}
}

Expand All @@ -133,7 +135,7 @@ public String toString() {
* Represents a string in the version item list, usually a qualifier.
*/
private static class StringItem
implements ComparableVersion.Item {
implements ComparableVersion.Item {
private static final String[] QUALIFIERS = {"alpha", "beta", "milestone", "rc", "snapshot", "", "sp"};

private static final List<String> QUALIFIER_LIST = Arrays.asList(QUALIFIERS);
Expand Down Expand Up @@ -216,7 +218,7 @@ public int compareTo(ComparableVersion.Item item) {
return -1; // 1.any < 1-1

default:
throw new RuntimeException("invalid item: " + item.getClass());
throw new RuntimeException(INVALID_ITEM + item.getClass());
}
}

Expand Down Expand Up @@ -275,7 +277,7 @@ public int compareTo(ComparableVersion.Item item) {
ComparableVersion.Item r = right.hasNext() ? right.next() : null;

// if this is shorter, then invert the compare and mul with -1
int result = l == null ? -1 * r.compareTo(l) : l.compareTo(r);
int result = (l == null && r != null) ? -1 : (l != null && r == null) ? 1 : l.compareTo(r);

if (result != 0) {
return result;
Expand All @@ -285,10 +287,11 @@ public int compareTo(ComparableVersion.Item item) {
return 0;

default:
throw new RuntimeException("invalid item: " + item.getClass());
throw new RuntimeException(INVALID_ITEM + item.getClass());
}
}

@Override
public String toString() {
StringBuilder buffer = new StringBuilder("(");
for (Iterator<ComparableVersion.Item> iter = iterator(); iter.hasNext(); ) {
Expand All @@ -315,7 +318,7 @@ public final void parseVersion(String version) {

ComparableVersion.ListItem list = items;

Stack<ComparableVersion.Item> stack = new Stack<ComparableVersion.Item>();
Stack<ComparableVersion.Item> stack = new Stack<>();
stack.push(list);

boolean isDigit = false;
Expand Down Expand Up @@ -346,7 +349,8 @@ public final void parseVersion(String version) {
if ((i + 1 < version.length()) && Character.isDigit(version.charAt(i + 1))) {
// new ListItem only if previous were digits and new char is a digit,
// ie need to differentiate only 1.1 from 1-1
list.add(list = new ComparableVersion.ListItem());
ComparableVersion.ListItem item = new ComparableVersion.ListItem();
list.add(item);

stack.push(list);
}
Expand Down

0 comments on commit ac79b14

Please sign in to comment.