question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Trades.java class is incorrectly comparing ID strings

See original GitHub issue

Trades.java contains the following:

@Override
public int compare(Trade trade1, Trade trade2) {
    return trade1.getId().compareTo(trade2.getId());
}

Since .getId() returns a String, comparing two Strings using compareTo is a lexicographical comparison, and would not work reliably, especially when the length of the String is different.

Consider this example:

public static void main(String[] args) {
  String id = "100";
  // check if the value is less than 100
  System.out.println(id.compareTo("99"));
  // check if value is equal to 100
  System.out.println(id.compareTo("100"));
  // check if value is greater than 100
  System.out.println(id.compareTo("101"));
}

Console:

-8
0
-1

"100".compareTo("99") returns a negative number which is lexicographically correct, but if these are trade Ids, then this is a bug.

We should therefore change the TradeIDComparator in Trades.java to convert the String into a BigInteger or Integer prior to the comparison.

something like this:

@Override
public int compare(Trade trade1, Trade trade2) {
	BigInteger id1 = new BigInteger(trade1.getId());
	BigInteger id2 = new BigInteger(trade2.getId());
	return id1.compareTo(id2);
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
cympcommented, Oct 19, 2018

@shimonm I addressed your problem in https://github.com/knowm/XChange/pull/2839, dealing with hexadecimal IDs as well.

private static final int[] ALLOWED_RADIXES = {10, 16};

@Override
public int compare(Trade trade1, Trade trade2) {
  for (int radix : ALLOWED_RADIXES) {
    try {
      BigInteger id1 = new BigInteger(trade1.getId(), radix);
      BigInteger id2 = new BigInteger(trade2.getId(), radix);
      return id1.compareTo(id2);
    } catch (NumberFormatException ignored) {
    }
  }
  return trade1.getId().compareTo(trade2.getId());
}
0reactions
shimonmcommented, Oct 19, 2018

Thank you! closing

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Comparing two identical strings with == returns false
In your code, you are comparing an Object with a String literal (also an object), which is not the same as comparing the...
Read more >
Comparable Interface: Comparing Strings - Saylor Academy
Objects that have an ordering are compared using the compareTo() method. ... When strings are compared, case-sensitive dictionary order is used.
Read more >
Groovy Language Documentation
class Component { Integer id String name } class CompositeObject implements Iterable<Component> { def components = [ new Component(id: 1, name: 'Foo'), ...
Read more >
How to run an if statement on a string variable (which value is ...
Use the [code ]string.equals(Object other)[/code] function to compare strings, not the [code ]==[/code] operator. The function checks the actual contents of ...
Read more >
Java 8 Date Time - 20 Examples of LocalDate, LocalTime ...
Example 20 - How to convert Date to String in Java 8, formatting dates. In the last two examples, though we have been...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found