TP-metaprogrammation-test/Position.java

48 lines
947 B
Java
Raw Normal View History

2021-12-03 14:57:00 +00:00
import lombok.*;
/** Définir une position. */
@Data @Value
public class Position {
public int x;
public int y;
// public Position(int x, int y) {
// this.x = x;
// this.y = y;
// // <(NO_TRACE)>// <(/NO_TRACE)>System.out.println("...appel à Position(" + x + "," + y + ")" + " --> " + this);
// System.out.println("...appel à Position(" + x + "," + y + ")" + " --> " + this);
// }
@Override
public String toString() {
return super.toString() + "(" + x + "," + y + ")";
}
@Override
public boolean equals(Object obj) {
Position other = (Position) obj;
return other.x == this.x && other.y == this.y;
}
@Override
public int hashCode() {
// bijection de Z dans N
int n, m;
if (this.x >= 0) {
n = 2 * this.x;
} else {
n = -2 * this.x - 1;
}
if (this.y >= 0) {
m = 2 * this.y;
} else {
m = -2 * this.y - 1;
}
// bijection de N² dans N
int h = (n+m)*(n+m+1)/2+m;
return h;
}
}