Source code: org/gui4j/core/util/SparseMatrix.java
1 package org.gui4j.core.util;
2
3 import java.io.Serializable;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.Iterator;
7 import java.util.Map;
8 import java.util.Set;
9
10 import org.gui4j.util.Pair;
11
12 /**
13 * Generic Class for sparsely populated matrices. The size of the matrix is dynamic, i.e.
14 * you can specify arbitrary row and column parameters.
15 *
16 * Another way to look at this class is as a "two-dimensional" map. Instead of a single key
17 * it uses a row and a column element to store and retrieve data elements.
18 *
19 * The public Interface Traverser allows to traverse the matrix generically. The Traverser object is passed
20 * into <code>traverse(Traverser)</code> and is given the opportunity to work with each element of the matrix.
21 */
22 public final class SparseMatrix implements Serializable
23 {
24 /**
25 * Allows objects to traverse the matrix.
26 * @see SparseMatrix#traverse(SparseMatrix.Traverser)
27 */
28 public static interface Traverser
29 {
30 /**
31 * Called once for each element that is visited during traversal.
32 * There is no defined traversal sequence.
33 * If the Traverser manipulates any one of row, col or value the effects on the matrix are undefined.
34 * @param row the "row" of the traversal's current matrix cell
35 * @param col the "column" of the traversal's current matrix cell
36 * @param value the value of the traversal's current matrix cell
37 */
38 public void work(Object row, Object col, Object value);
39 }
40
41 // The map where all matrix elements are stored.
42 private final Map matrixMap = new HashMap();
43
44 /**
45 * Returns the value found at the specified row and column of the matrix.
46 * Returns null if there's no value for the specified row and column.
47 * @param row Object
48 * @param col Object
49 * @return Object
50 */
51 public Object get(Object row, Object col)
52 {
53 return matrixMap.get(new Pair(row, col));
54 }
55
56 /**
57 * Sets the value of the matrix at the specified row and column.
58 * @param row Object
59 * @param col Object
60 * @param value
61 */
62 public void set(Object row, Object col, Object value)
63 {
64 if (row == null || col == null)
65 {
66 throw new IllegalArgumentException("row or column may not be null.");
67 }
68
69 matrixMap.put(new Pair(row, col), value);
70 }
71
72 public void remove(Object row, Object col)
73 {
74 matrixMap.remove(new Pair(row, col));
75 }
76
77 /**
78 * Returns a Set of all used "columns" in the matrix.
79 * @return Set
80 */
81 public Set colSet()
82 {
83 Set colSet = new HashSet();
84
85 for (Iterator iterator = matrixMap.keySet().iterator(); iterator.hasNext();)
86 {
87 Pair pair = (Pair) iterator.next();
88 colSet.add(pair.getSecond());
89 }
90
91 return colSet;
92 }
93
94 /**
95 * Returns a Set of all used "rows" in the matrix.
96 * @return Set
97 */
98 public Set rowSet()
99 {
100 Set rowSet = new HashSet();
101
102 for (Iterator iterator = matrixMap.keySet().iterator(); iterator.hasNext();)
103 {
104 Pair pair = (Pair) iterator.next();
105 rowSet.add(pair.getFirst());
106 }
107
108 return rowSet;
109 }
110
111 /**
112 * Traverses the matrix and calls <code>work()</code> on the supplied
113 * Traverser object for each matrix element.
114 * The traversal sequence is undefined.
115 * @param traverser Traverser
116 */
117 public void traverse(Traverser traverser)
118 {
119 Iterator iter = matrixMap.entrySet().iterator();
120 while (iter.hasNext())
121 {
122 Map.Entry entry = (Map.Entry) iter.next();
123 Pair key = (Pair) entry.getKey();
124 Object row = key.getFirst();
125 Object col = key.getSecond();
126 traverser.work(row, col, entry.getValue());
127 }
128 }
129
130 /**
131 * Löscht alle Elemente
132 */
133 public void clear()
134 {
135 matrixMap.clear();
136 }
137
138 public boolean equals(Object obj)
139 {
140 if (this == obj)
141 {
142 return true;
143 }
144 if (!(obj instanceof SparseMatrix))
145 {
146 return false;
147 }
148
149 SparseMatrix other = (SparseMatrix) obj;
150
151 return this.matrixMap.equals(other.matrixMap);
152 }
153
154 public int hashCode()
155 {
156 return matrixMap.hashCode();
157 }
158
159 public String toString()
160 {
161 return "SparseMatrix (" + matrixMap.size() + " elements)";
162 }
163
164 }