Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
Using a hash table for these kinds of objects may or may not be a good idea due to the unlikelihood of two doubles computed at different times using different sets of operations being exactly equal (==) to one another. What are you trying to accomplish by storing them in a hash table? Dan On Mar 31, 8:20 am, googelybear wrote: > Hi, > > this question might seem stupid but how can one implement hashCode() > and equals() for a simple 2d point class as follows: > > public class Point { > > private double fX; > > private double fY; > > public double getX() { > return fX; > } > > public void setX(double x) { > this.fX= x; > } > > public double getY() { > return fY; > } > > public void setY(double y) { > this.fY= y; > } > > } > > The generated equals() and hashCode() methods from Eclipse do NOT work > with GWT as method Double.doubleToLongBits() is missing: > > @Override > public int hashCode() { > final int prime= 31; > int result= 1; > long temp; > temp= Double.doubleToLongBits(fX); > result= prime * result + (int) (temp ^ (temp >>> 32)); > temp= Double.doubleToLongBits(fY); > result= prime * result + (int) (temp ^ (temp >>> 32)); > return result; > } > > @Override > public boolean equals(Object obj) { > if (this == obj) > return true; > if (obj == null) > return false; > if (!(obj instanceof Point)) > return false; > Point other= (Point) obj; > if (Double.doubleToLongBits(fX) != > Double.doubleToLongBits(other.fX)) > return false; > if (Double.doubleToLongBits(fY) != > Double.doubleToLongBits(other.fY)) > return false; > return true; > } > > Does anyone have an idea how to implement this? > > thanks a bunch, > > Dennis -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
jchimene wrote: > On Mar 31, 9:36 am, Luis Fernando Planella Gonzalez > wrote: > >>> Anyone has a better idea? >>> >> public boolean equals(Object obj) { >> if (!(obj instanceof Point)) { >> return false; >> } >> Point p = (Point) obj; >> return fX == p.fY && fY == p.fY; >> >> } >> >> public int hashCode() { >> return (int) (pX + pY); >> > > Does this say that Point(2.0,0.0) and Point(0.0,2.0) have the same > hashCode()? > > Yes, but this sort of thing is inevitable. You can't map two doubles onto one integer without some duplication. But that doesn't matter. Consider this hashCode() implementation: public int hashCode() { return 0; } This will work in all circumstances. It fulfills the hashCode contract, it just doesn't make a HashMap with Point keys perform very well. Even then, if you only have very small maps, this won't matter. My point is that you shouldn't get too hung up on how this is implemented - just use something fast and simple. Paul -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
Posted issue http://code.google.com/p/google-web-toolkit/issues/detail?id=4805. As for equals, why not just compare the double values directly, vs bitwise equality? Unless you're concerned with the various versions of NaN and infinity this should work fine; in fact you might even want to allow a bit of a margin to deal with rounding. jchimene, you'd want to do something like the original poster to avoid that, 31*hashOfpX + hashOfpY. On Mar 31, 11:46 am, kozura wrote: > Ach, you're right, I hadn't bothered to check in deployed mode, > assumed GWT did the right thing! This should be posted as an issue, > as hashCode() is listed as implemented, but it should be done > correctly. So I guess toString is the best way to go in the > meantime.. > > On Mar 31, 10:34 am, Thomas Broyer wrote: > > > On Mar 31, 5:30 pm, kozura wrote: > > > > ((Double)fX).hashCode() seems to work fine. > > > It's no more than a "return (int) value" (i.e. equivalent to "(int) > > fX") which is probably not "accurate" enough for a Point where you > > chose to use doubles to store the coordinates... -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
On Mar 31, 9:36 am, Luis Fernando Planella Gonzalez wrote: > > Anyone has a better idea? > > public boolean equals(Object obj) { > if (!(obj instanceof Point)) { > return false; > } > Point p = (Point) obj; > return fX == p.fY && fY == p.fY; > > } > > public int hashCode() { > return (int) (pX + pY); Does this say that Point(2.0,0.0) and Point(0.0,2.0) have the same hashCode()? > > } -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
Ach, you're right, I hadn't bothered to check in deployed mode, assumed GWT did the right thing! This should be posted as an issue, as hashCode() is listed as implemented, but it should be done correctly. So I guess toString is the best way to go in the meantime.. On Mar 31, 10:34 am, Thomas Broyer wrote: > On Mar 31, 5:30 pm, kozura wrote: > > > ((Double)fX).hashCode() seems to work fine. > > It's no more than a "return (int) value" (i.e. equivalent to "(int) > fX") which is probably not "accurate" enough for a Point where you > chose to use doubles to store the coordinates... -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
> Anyone has a better idea? public boolean equals(Object obj) { if (!(obj instanceof Point)) { return false; } Point p = (Point) obj; return fX == p.fY && fY == p.fY; } public int hashCode() { return (int) (pX + pY); } -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
On Mar 31, 5:30 pm, kozura wrote: > ((Double)fX).hashCode() seems to work fine. It's no more than a "return (int) value" (i.e. equivalent to "(int) fX") which is probably not "accurate" enough for a Point where you chose to use doubles to store the coordinates... -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
((Double)fX).hashCode() seems to work fine. -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
Found my answer here: http://groups.google.com/group/google-web-toolkit/browse_thread/thread/f44e5874eb20b41b/f34caf3a7bf64daa Simply hash the Double.toString() . Anyone has a better idea? On Mar 31, 2:53 pm, googelybear wrote: > It seems that this got reverted at some > point:http://code.google.com/p/google-web-toolkit/source/diff?spec=svn2434&;... > > does anyone know why? > > On Mar 31, 2:35 pm, googelybear wrote: > > > > > I just found a post in the gwt contributors group but I cannot find it > > anywhere in gwt, any > > ideas?http://groups.google.com/group/google-web-toolkit-contributors/browse... > > > On Mar 31, 2:20 pm, googelybear wrote: > > > > Hi, > > > > this question might seem stupid but how can one implement hashCode() > > > and equals() for a simple 2d point class as follows: > > > > public class Point { > > > > private double fX; > > > > private double fY; > > > > public double getX() { > > > return fX; > > > } > > > > public void setX(double x) { > > > this.fX= x; > > > } > > > > public double getY() { > > > return fY; > > > } > > > > public void setY(double y) { > > > this.fY= y; > > > } > > > > } > > > > The generated equals() and hashCode() methods from Eclipse do NOT work > > > with GWT as method Double.doubleToLongBits() is missing: > > > > @Override > > > public int hashCode() { > > > final int prime= 31; > > > int result= 1; > > > long temp; > > > temp= Double.doubleToLongBits(fX); > > > result= prime * result + (int) (temp ^ (temp >>> 32)); > > > temp= Double.doubleToLongBits(fY); > > > result= prime * result + (int) (temp ^ (temp >>> 32)); > > > return result; > > > } > > > > @Override > > > public boolean equals(Object obj) { > > > if (this == obj) > > > return true; > > > if (obj == null) > > > return false; > > > if (!(obj instanceof Point)) > > > return false; > > > Point other= (Point) obj; > > > if (Double.doubleToLongBits(fX) != > > > Double.doubleToLongBits(other.fX)) > > > return false; > > > if (Double.doubleToLongBits(fY) != > > > Double.doubleToLongBits(other.fY)) > > > return false; > > > return true; > > > } > > > > Does anyone have an idea how to implement this? > > > > thanks a bunch, > > > > Dennis -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
It seems that this got reverted at some point: http://code.google.com/p/google-web-toolkit/source/diff?spec=svn2434&r=2434&format=side&path=/trunk/user/super/com/google/gwt/emul/java/lang/Double.java does anyone know why? On Mar 31, 2:35 pm, googelybear wrote: > I just found a post in the gwt contributors group but I cannot find it > anywhere in gwt, any > ideas?http://groups.google.com/group/google-web-toolkit-contributors/browse... > > On Mar 31, 2:20 pm, googelybear wrote: > > > > > Hi, > > > this question might seem stupid but how can one implement hashCode() > > and equals() for a simple 2d point class as follows: > > > public class Point { > > > private double fX; > > > private double fY; > > > public double getX() { > > return fX; > > } > > > public void setX(double x) { > > this.fX= x; > > } > > > public double getY() { > > return fY; > > } > > > public void setY(double y) { > > this.fY= y; > > } > > > } > > > The generated equals() and hashCode() methods from Eclipse do NOT work > > with GWT as method Double.doubleToLongBits() is missing: > > > @Override > > public int hashCode() { > > final int prime= 31; > > int result= 1; > > long temp; > > temp= Double.doubleToLongBits(fX); > > result= prime * result + (int) (temp ^ (temp >>> 32)); > > temp= Double.doubleToLongBits(fY); > > result= prime * result + (int) (temp ^ (temp >>> 32)); > > return result; > > } > > > @Override > > public boolean equals(Object obj) { > > if (this == obj) > > return true; > > if (obj == null) > > return false; > > if (!(obj instanceof Point)) > > return false; > > Point other= (Point) obj; > > if (Double.doubleToLongBits(fX) != > > Double.doubleToLongBits(other.fX)) > > return false; > > if (Double.doubleToLongBits(fY) != > > Double.doubleToLongBits(other.fY)) > > return false; > > return true; > > } > > > Does anyone have an idea how to implement this? > > > thanks a bunch, > > > Dennis -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
Re: How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
I just found a post in the gwt contributors group but I cannot find it anywhere in gwt, any ideas? http://groups.google.com/group/google-web-toolkit-contributors/browse_thread/thread/a5193c6a82eddb5e/b400204dddcc8c64 On Mar 31, 2:20 pm, googelybear wrote: > Hi, > > this question might seem stupid but how can one implement hashCode() > and equals() for a simple 2d point class as follows: > > public class Point { > > private double fX; > > private double fY; > > public double getX() { > return fX; > } > > public void setX(double x) { > this.fX= x; > } > > public double getY() { > return fY; > } > > public void setY(double y) { > this.fY= y; > } > > } > > The generated equals() and hashCode() methods from Eclipse do NOT work > with GWT as method Double.doubleToLongBits() is missing: > > @Override > public int hashCode() { > final int prime= 31; > int result= 1; > long temp; > temp= Double.doubleToLongBits(fX); > result= prime * result + (int) (temp ^ (temp >>> 32)); > temp= Double.doubleToLongBits(fY); > result= prime * result + (int) (temp ^ (temp >>> 32)); > return result; > } > > @Override > public boolean equals(Object obj) { > if (this == obj) > return true; > if (obj == null) > return false; > if (!(obj instanceof Point)) > return false; > Point other= (Point) obj; > if (Double.doubleToLongBits(fX) != > Double.doubleToLongBits(other.fX)) > return false; > if (Double.doubleToLongBits(fY) != > Double.doubleToLongBits(other.fY)) > return false; > return true; > } > > Does anyone have an idea how to implement this? > > thanks a bunch, > > Dennis -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.
How to implement hashCode() and equals() for a point in 2D-space (x,y) in GWT?
Hi, this question might seem stupid but how can one implement hashCode() and equals() for a simple 2d point class as follows: public class Point { private double fX; private double fY; public double getX() { return fX; } public void setX(double x) { this.fX= x; } public double getY() { return fY; } public void setY(double y) { this.fY= y; } } The generated equals() and hashCode() methods from Eclipse do NOT work with GWT as method Double.doubleToLongBits() is missing: @Override public int hashCode() { final int prime= 31; int result= 1; long temp; temp= Double.doubleToLongBits(fX); result= prime * result + (int) (temp ^ (temp >>> 32)); temp= Double.doubleToLongBits(fY); result= prime * result + (int) (temp ^ (temp >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Point)) return false; Point other= (Point) obj; if (Double.doubleToLongBits(fX) != Double.doubleToLongBits(other.fX)) return false; if (Double.doubleToLongBits(fY) != Double.doubleToLongBits(other.fY)) return false; return true; } Does anyone have an idea how to implement this? thanks a bunch, Dennis -- You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to google-web-tool...@googlegroups.com. To unsubscribe from this group, send email to google-web-toolkit+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.