1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.millscript.office.spreadsheet.worksheets;
22
23 import org.millscript.commons.alert.alerts.Fault;
24 import org.millscript.commons.alert.alerts.Unimplemented;
25 import org.millscript.commons.util.IList;
26 import org.millscript.commons.util.ListIterator;
27 import org.millscript.commons.util.alerts.ListIndexOutOfBoundsAlert;
28 import org.millscript.commons.util.list.AbstractIList;
29 import org.millscript.office.spreadsheet.Cell;
30 import org.millscript.office.spreadsheet.Column;
31 import org.millscript.office.spreadsheet.Row;
32 import org.millscript.office.spreadsheet.Worksheet;
33 import org.millscript.office.spreadsheet.rows.IRowList;
34 import org.millscript.office.spreadsheet.rows.ISharedRowList;
35 import org.millscript.office.spreadsheet.rows.RowImpl;
36 import org.millscript.office.spreadsheet.rows.RowListIterator;
37
38 /**
39 *
40 */
41 public class WorksheetImpl extends AbstractIList< Row > implements Worksheet {
42
43 /**
44 * The backing data store for this worksheet, a two dimensional array,
45 * an array of array of cells. A null entry indicates the row is empty.
46 */
47 private Cell[][] rowData;
48
49 /**
50 *
51 */
52 public WorksheetImpl() {
53 }
54
55 /**
56 *
57 */
58 public WorksheetImpl( final Cell[][] rows ) {
59 this.rowData = rows;
60 }
61
62 /**
63 * Constructs a new worksheet with a copy of the specified backing row
64 * array, starting and ending at the specified indices.
65 *
66 * @param rows the backing row array to copy
67 * @param start the index(one based, inclusive) of the first element in the
68 * slice
69 * @param end the index(one based, inclusive) of the last element in the
70 * slice. If end < start, the new list will be empty
71 */
72 public WorksheetImpl( final Cell[][] rows, final int start, final int end ) {
73 if ( start > end ) {
74 this.rowData = null;
75 } else if ( start < 1 || start > rows.length ) {
76 throw new ListIndexOutOfBoundsAlert(
77 "First index in slice must be between 1 and the number of rows"
78 ).culprit(
79 "index",
80 start
81 ).decorate( rows ).mishap();
82 } else if ( end > rows.length ) {
83 throw new ListIndexOutOfBoundsAlert(
84 "Last index in slice must not be greater than the number of rows"
85 ).culprit(
86 "index",
87 end
88 ).decorate( rows ).mishap();
89 } else {
90 this.rowData = new Cell[ end - start + 1 ][];
91 try {
92 System.arraycopy( rows, start - 1, this.rowData, 0, this.rowData.length );
93 } catch ( Exception ex ) {
94 throw new Fault(
95 "Failed to take a copy of the specified array slice"
96 ).setParentThrowable( ex ).mishap();
97 }
98 }
99 }
100
101 /**
102 * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
103 */
104 @Override
105 protected Row doGet( final int pos ) {
106 final Cell[] row = this.rowData[ pos - 1 ];
107 if ( row == null ) {
108 return new RowImpl();
109 } else {
110 return new RowImpl( row, true );
111 }
112 }
113
114 /**
115 * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
116 */
117 @Override
118 protected IList< Row > doSlice( final int first, final int last, final boolean share ) {
119 if ( share ) {
120 return new ISharedRowList( this.rowData, first, last );
121 } else {
122
123 return new IRowList( this.rowData, first, last );
124 }
125 }
126
127 /**
128 * @see org.millscript.office.spreadsheet.Worksheet#firstUsedColumn()
129 */
130 public int firstUsedColumn() {
131
132 throw new Unimplemented(
133 "A firstUsedColumn operation was required"
134 ).culprit( "class", this.getClass() );
135 }
136
137 /**
138 * @see org.millscript.office.spreadsheet.Worksheet#firstUsedRow()
139 */
140 public int firstUsedRow() {
141
142 throw new Unimplemented(
143 "A firstUsedRow operation was required"
144 ).culprit( "class", this.getClass() );
145 }
146
147 /**
148 * @see org.millscript.office.spreadsheet.Worksheet#getCell(int, int)
149 */
150 public Cell getCell( final int row, final int col ) {
151 return this.get( row ).get( col );
152 }
153
154 /**
155 * @see org.millscript.office.spreadsheet.Worksheet#getCell0(int, int)
156 */
157 public Cell getCell0( final int row, final int col ) {
158 return this.get0( row ).get0( col );
159 }
160
161 /**
162 * @see org.millscript.office.spreadsheet.Worksheet#getColumn(int)
163 */
164 public Column getColumn( final int pos ) {
165
166 throw new Unimplemented(
167 "A getColumn operation was required"
168 ).culprit( "class", this.getClass() );
169 }
170
171 /**
172 * @see org.millscript.office.spreadsheet.Worksheet#getColumn(java.lang.Integer)
173 */
174 public Column getColumn( final Integer pos ) {
175
176 throw new Unimplemented(
177 "A getColumn operation was required"
178 ).culprit( "class", this.getClass() );
179 }
180
181 /**
182 * @see org.millscript.office.spreadsheet.Worksheet#getColumn0(int)
183 */
184 public Column getColumn0( final int pos ) {
185
186 throw new Unimplemented(
187 "A getColumn0 operation was required"
188 ).culprit( "class", this.getClass() );
189 }
190
191 /**
192 * @see org.millscript.office.spreadsheet.Worksheet#getColumn0(java.lang.Integer)
193 */
194 public Column getColumn0( final Integer pos ) {
195
196 throw new Unimplemented(
197 "A getColumn0 operation was required"
198 ).culprit( "class", this.getClass() );
199 }
200
201 /**
202 * @see org.millscript.office.spreadsheet.Worksheet#getColumnList()
203 */
204 public IList< Column > getColumnList() {
205
206 throw new Unimplemented(
207 "A getColumnList operation was required"
208 ).culprit( "class", this.getClass() );
209 }
210
211 /**
212 * @see org.millscript.commons.util.IList#indexOf(V)
213 */
214 public int indexOf( final Row value ) {
215
216 throw new Unimplemented(
217 "A indexOf operation was required"
218 ).culprit( "class", this.getClass() );
219 }
220
221 public void initialiseRow( final int rowIndex, final int lastCol ) {
222
223 if ( this.rowData == null ) {
224 this.rowData = new Cell[ rowIndex ][];
225 } else if ( this.rowData.length < rowIndex ) {
226 final Cell[][] newRowData = new Cell[ rowIndex ][];
227 System.arraycopy( this.rowData, 0, newRowData, 0, this.rowData.length );
228 this.rowData = newRowData;
229 }
230
231 final Cell[] row = this.rowData[ rowIndex - 1 ];
232
233 if ( row == null ) {
234 this.rowData[ rowIndex - 1 ] = new Cell[ lastCol ];
235 } else if ( row.length < lastCol ) {
236 final Cell[] newRow = new Cell[ lastCol ];
237 System.arraycopy( row, 0, newRow, 0, row.length );
238 this.rowData[ rowIndex - 1 ] = newRow;
239 }
240 }
241
242 public void insertCell( final int rowIndex, final int colIndex, final Cell data ) {
243 this.initialiseRow( rowIndex, colIndex );
244 this.rowData[ rowIndex - 1 ][ colIndex - 1 ] = data;
245 }
246
247 /**
248 * @see org.millscript.commons.util.IList#iterator(boolean)
249 */
250 public ListIterator< Row > iterator( final boolean share ) {
251 return new RowListIterator( this.rowData, share );
252 }
253
254 /**
255 * @see org.millscript.office.spreadsheet.Worksheet#lastUsedColumn()
256 */
257 public int lastUsedColumn() {
258
259 throw new Unimplemented(
260 "A lastUsedColumn operation was required"
261 ).culprit( "class", this.getClass() );
262 }
263
264 /**
265 * @see org.millscript.office.spreadsheet.Worksheet#lastUsedRow()
266 */
267 public int lastUsedRow() {
268
269 throw new Unimplemented(
270 "A lastUsedRow operation was required"
271 ).culprit( "class", this.getClass() );
272 }
273
274 /**
275 * @see org.millscript.office.spreadsheet.Worksheet#numberOfColumns()
276 */
277 public int numberOfColumns() {
278
279 throw new Unimplemented(
280 "A numberOfColumns operation was required"
281 ).culprit( "class", this.getClass() );
282 }
283
284 /**
285 * @see org.millscript.office.spreadsheet.Worksheet#numberOfRows()
286 */
287 public int numberOfRows() {
288
289 throw new Unimplemented(
290 "A numberOfRows operation was required"
291 ).culprit( "class", this.getClass() );
292 }
293
294 /**
295 * @see org.millscript.commons.util.IMap#size()
296 */
297 public int size() {
298 return this.rowData.length;
299 }
300
301 }