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 | import java.util.*; |
24 | |
25 | /** |
26 | * The Grid specifies which coordinates are valid cells, and stores a value for |
27 | * each. |
28 | * @author <a href="http://sourceforge.net/users/stevensa">Andrew Stevens</a> |
29 | */ |
30 | public class Grid extends HashMap<Coordinate, Integer> { |
31 | |
32 | /** |
33 | * Holds value of property size. |
34 | */ |
35 | private int size; |
36 | |
37 | /** |
38 | * Convert a given (integer) value to a String form (single character) |
39 | * suitable for display. |
40 | */ |
41 | public static String digit(int i) { |
42 | String ret = "?"; |
43 | |
44 | if (i == 0) { |
45 | ret = "-"; |
46 | } else if (i > 0) { |
47 | ret = String.valueOf(Character.forDigit(i, 36)).toUpperCase(); |
48 | } |
49 | |
50 | return ret; |
51 | } |
52 | |
53 | /** |
54 | * Creates a new instance of Grid. |
55 | * @param size |
56 | */ |
57 | public Grid(int size) { |
58 | this.size = size; |
59 | } |
60 | |
61 | /** |
62 | * Getter for property size. |
63 | * @return Value of property size. |
64 | */ |
65 | public int getSize() { |
66 | return this.size; |
67 | } |
68 | |
69 | public Integer put(Coordinate key, Integer value) { |
70 | if (key == null) { |
71 | throw new IllegalArgumentException("Null coordinates not allowed in Grid"); |
72 | } |
73 | if (key.getColumn() >= size || key.getRow() >= size) { |
74 | throw new IllegalArgumentException("Coordinate " + key.toString() + |
75 | " outside of Grid bounds"); |
76 | } |
77 | return super.put(key, value); |
78 | } |
79 | |
80 | public String toString() { |
81 | StringBuffer retValue = new StringBuffer(); |
82 | |
83 | for (int row = 0; row < this.size; row++) { |
84 | for (int column = 0 ; column < this.size; column++) { |
85 | Integer value = this.get(new Coordinate(row, column)); |
86 | retValue.append(value == null ? " " : value.toString()); |
87 | } |
88 | retValue.append('\n'); |
89 | } |
90 | |
91 | return retValue.toString(); |
92 | } |
93 | |
94 | } |