Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to compare two JavaScriptObjects in GWT?

Tags:

java

gwt

It looks like hashCode() and equals() are declared as final. So overriding the implementation is not possible. It also states that equals() returns true if the objects are JavaScript identical (triple-equals). I'm not quite sure what that means as creating two identical JavaScriptObject's in GWT and comparing them with equals() returns false. Also it looks like hashcode() uses a monotonically increasing counter to assign a hash code to the underlying JavaScript object. If I wanted to store JavaScriptObjects in a Set this would complicate things. Any help would be much appreciated.

like image 926
affablebloke Avatar asked Jan 14 '11 00:01

affablebloke


1 Answers

It depends on which equality criteria you want to use for your situation.

  • If you want object identity, you can use the predefined equals() and hashCode() - and put the JavaScriptObjects directly in a HashSet.
  • If you need a different equality notion, you can write your own Comparator and put the JavaScriptObjects in e.g. a TreeSet, created by new TreeSet(comparator).
  • If you need to put JavaScriptObjects in a HashSet (not a TreeSet), and still need a different equality notion, you can't put the JavaScriptObjects directly in the Set. Then you'd have to write a wrapper class, which contains the JavaScriptObject, and implements equals() and hashCode().
like image 131
Chris Lercher Avatar answered Oct 25 '22 03:10

Chris Lercher