You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HashablePoint.java 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright (c) Xerox Corporation 1998-2002. All rights reserved.
  3. Use and copying of this software and preparation of derivative works based
  4. upon this software are permitted. Any distribution of this software or
  5. derivative works must comply with all applicable United States export control
  6. laws.
  7. This software is made available AS IS, and Xerox Corporation makes no warranty
  8. about the software, its performance or its conformity to any specification.
  9. */
  10. package introduction;
  11. import java.util.Hashtable;
  12. public aspect HashablePoint {
  13. public int Point.hashCode() {
  14. return (int) (getX() + getY() % Integer.MAX_VALUE);
  15. }
  16. public boolean Point.equals(Object o) {
  17. if (o == this) { return true; }
  18. if (!(o instanceof Point)) { return false; }
  19. Point other = (Point)o;
  20. return (getX() == other.getX()) && (getY() == other.getY());
  21. }
  22. public static void main(String[] args) {
  23. Hashtable h = new Hashtable();
  24. Point p1 = new Point();
  25. p1.setRectangular(10, 10);
  26. Point p2 = new Point();
  27. p2.setRectangular(10, 10);
  28. System.out.println("p1 = " + p1);
  29. System.out.println("p2 = " + p2);
  30. System.out.println("p1.hashCode() = " + p1.hashCode());
  31. System.out.println("p2.hashCode() = " + p2.hashCode());
  32. h.put(p1, "P1");
  33. System.out.println("Got: " + h.get(p2));
  34. }
  35. }