View Javadoc

1   /*
2    * Trip Tracker, a real-time position tracking system for the Internet.
3    * Copyright (C) 2006  Team Trip Tracker
4    *
5    * This program is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License as published by the
7    * Free Software Foundation; either version 2 of the License, or (at your
8    * option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful, but
11   * WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13   * General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License along
16   * with this program; if not, write to the Free Software Foundation, Inc.,
17   * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
18   */
19  
20  package triptracker.client.map.core;
21  
22  import java.util.Iterator;
23  import java.util.ListIterator;
24  import java.util.NoSuchElementException;
25  
26  public class Matrix2D<E> implements Iterable<E> {
27  	private E[][] mat;
28  	
29  	public Matrix2D() { }
30  	
31  	public Matrix2D(E[][] matrix) {
32  		mat = matrix;
33  	}
34  
35  	public void setMatrix(E[][] matrix) {
36  		mat = matrix;
37  	}
38  	
39  	public E[][] getMatrix() {
40  		return mat;
41  	}
42  	
43  	public void set(int i, int j, E value) {
44  		mat[i][j] = value;
45  	}
46  	
47  	public E get(int i, int j) {
48  		return mat[i][j];
49  	}
50  	
51  	public int getLength(int dimension) {
52  		if (dimension < 0 || dimension > 1) {
53  			throw new IllegalArgumentException();
54  		}
55  		
56  		switch (dimension) {
57  		case 0: return mat.length;
58  		case 1: return mat[0].length;
59  		default: return 0;
60  		}
61  	}
62  
63  	public Iterator<E> iterator() {
64  		return new MatrixIterator();
65  	}
66  	
67  	private class MatrixIterator implements ListIterator<E> {
68  		private final int length = mat.length * mat[0].length;
69  		private int cursor = 0;
70  		private int cursorI = 0;
71  		private int cursorJ = 0;
72  
73  		/***
74  		 * {@inheritDoc}
75  		 */
76  		public boolean hasNext() {
77  			if (cursor < length) {
78  				return true;
79  			} else {
80  				return false;
81  			}
82  		}
83  
84  		/***
85  		 * {@inheritDoc}
86  		 */
87  		public E next() {
88  			if (!hasNext()) {
89  				throw new NoSuchElementException();
90  			}
91  			
92  			E o = mat[cursorI][cursorJ];
93  			moveCursor(true);
94  			
95  			return o;
96  		}
97  
98  		/***
99  		 * {@inheritDoc}
100 		 */
101 		public boolean hasPrevious() {
102 			if (cursor > 0) {
103 				return true;
104 			} else {
105 				return false;
106 			}
107 		}
108 
109 		/***
110 		 * {@inheritDoc}
111 		 */
112 		public E previous() {
113 			if (!hasPrevious()) {
114 				throw new NoSuchElementException();
115 			}
116 			
117 			moveCursor(false);
118 			return mat[cursorI][cursorJ];
119 		}
120 
121 		/***
122 		 * {@inheritDoc}
123 		 */
124 		public int nextIndex() {
125 			return cursor;
126 		}
127 
128 		/***
129 		 * {@inheritDoc}
130 		 */
131 		public int previousIndex() {
132 			return (cursor - 1);
133 		}
134 
135 		/***
136 		 * {@inheritDoc}
137 		 */
138 		public void remove() {
139 			throw new UnsupportedOperationException();
140 		}
141 
142 		/***
143 		 * {@inheritDoc}
144 		 */
145 		public void set(E o) {
146 			System.out.println("Setting new Matrix value");
147 			mat[cursorI][cursorJ] = o;
148 		}
149 
150 		/***
151 		 * {@inheritDoc}
152 		 */
153 		public void add(E o) {
154 			throw new UnsupportedOperationException();
155 		}
156 		
157 		/***
158 		 * Move the cursor forward or backwards in the list and recalculate
159 		 * cursorI and cursorJ.
160 		 * 
161 		 * @param forward if true move forward, if false move backwards
162 		 */
163 		private void moveCursor(boolean forward) {
164 			if (forward) {
165 				cursor++;				
166 			} else {
167 				cursor--;
168 			}
169 			
170 			cursorI = (cursor % mat.length);
171 			cursorJ = (cursor / mat.length);
172 		}
173 
174 	}
175 		
176 }