1 | /* |
2 | * Copyright (c) 2005 The PseudoQ Project. |
3 | * |
4 | * This file is part of PseudoQ. |
5 | * |
6 | * PseudoQ is free software; you can redistribute it and/or modify |
7 | * it under the terms of the GNU Lesser General Public License as published by |
8 | * the Free Software Foundation; either version 2.1 of the License, or |
9 | * (at your option) any later version. |
10 | * |
11 | * PseudoQ is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU Lesser General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Lesser General Public License |
17 | * along with PseudoQ; if not, write to the Free Software |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
19 | */ |
20 | |
21 | package net.sourceforge.pseudoq.model; |
22 | |
23 | /** |
24 | * A (row, column) pair of coordinates. |
25 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
26 | */ |
27 | public class Coordinate { |
28 | /** Upper bound on coordinates, to make it easier to calculate the hashCode */ |
29 | public static final int MAX_INDEX = 255; |
30 | |
31 | /** Creates a new instance of Coordinate */ |
32 | public Coordinate(int row, int column) { |
33 | if (row < 0 || row > MAX_INDEX) { |
34 | throw new IllegalArgumentException("Invalid Coordinate row " + row); |
35 | } |
36 | if (column < 0 || column > MAX_INDEX) { |
37 | throw new IllegalArgumentException("Invalid Coordinate column " + column); |
38 | } |
39 | this.row = row; |
40 | this.column = column; |
41 | } |
42 | |
43 | /** |
44 | * Holds value of property row. |
45 | */ |
46 | private int row; |
47 | |
48 | /** |
49 | * Getter for property row. |
50 | * @return Value of property row. |
51 | */ |
52 | public int getRow() { |
53 | return this.row; |
54 | } |
55 | |
56 | /** |
57 | * Holds value of property column. |
58 | */ |
59 | private int column; |
60 | |
61 | /** |
62 | * Getter for property column. |
63 | * @return Value of property column. |
64 | */ |
65 | public int getColumn() { |
66 | return this.column; |
67 | } |
68 | |
69 | public boolean equals(Object obj) { |
70 | boolean retValue = false; |
71 | |
72 | if ((obj instanceof Coordinate) && (obj != null)) { |
73 | Coordinate other = (Coordinate) obj; |
74 | retValue = (other.row == this.row && other.column == this.column); |
75 | } |
76 | |
77 | return retValue; |
78 | } |
79 | |
80 | public int hashCode() { |
81 | return ((row * (MAX_INDEX + 1)) + column); |
82 | } |
83 | |
84 | public String toString() { |
85 | return "(" + row + ", " + column + ")"; |
86 | } |
87 | |
88 | } |