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.rows;
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.Row;
31
32 /**
33 *
34 */
35 public class IRowList extends AbstractIList< Row > {
36
37 /**
38 * The backing data store for this worksheet, a two dimensional array,
39 * an array of array of cells. A null entry indicates the row is empty.
40 */
41 private Cell[][] rowData;
42
43 /**
44 *
45 */
46 public IRowList( final Cell[][] rows ) {
47 this.rowData = rows;
48 }
49
50 /**
51 * Constructs a new worksheet with a copy of the specified backing row
52 * array, starting and ending at the specified indices.
53 *
54 * @param rows the backing row array to copy
55 * @param start the index(one based, inclusive) of the first element in the
56 * slice
57 * @param end the index(one based, inclusive) of the last element in the
58 * slice. If end < start, the new list will be empty
59 */
60 public IRowList( final Cell[][] rows, final int start, final int end ) {
61 if ( start > end ) {
62 this.rowData = null;
63 } else if ( start < 1 || start > rows.length ) {
64 throw new ListIndexOutOfBoundsAlert(
65 "First index in slice must be between 1 and the number of rows"
66 ).culprit(
67 "index",
68 start
69 ).decorate( rows ).mishap();
70 } else if ( end > rows.length ) {
71 throw new ListIndexOutOfBoundsAlert(
72 "Last index in slice must not be greater than the number of rows"
73 ).culprit(
74 "index",
75 end
76 ).decorate( rows ).mishap();
77 } else {
78 this.rowData = new Cell[ end - start + 1 ][];
79 try {
80 System.arraycopy( rows, start - 1, this.rowData, 0, this.rowData.length );
81 } catch ( Exception ex ) {
82 throw new Fault(
83 "Failed to take a copy of the specified array slice"
84 ).setParentThrowable( ex ).mishap();
85 }
86 }
87 }
88
89 /**
90 * @see org.millscript.commons.util.list.AbstractIList#doGet(int)
91 */
92 @Override
93 protected Row doGet( final int pos ) {
94 final Cell[] row = this.rowData[ pos - 1 ];
95 if ( row == null ) {
96 return new RowImpl();
97 } else {
98 return new RowImpl( row, true );
99 }
100 }
101
102 /**
103 * @see org.millscript.commons.util.list.AbstractIList#doSlice(int, int, boolean)
104 */
105 @Override
106 protected IList< Row > doSlice( int first, int last, boolean share ) {
107 if ( share ) {
108 return new ISharedRowList( this.rowData, first, last );
109 } else {
110
111 return new IRowList( this.rowData, first, last );
112 }
113 }
114
115 /**
116 * @see org.millscript.commons.util.IList#indexOf(V)
117 */
118 public int indexOf( Row value ) {
119
120 throw new Unimplemented(
121 "A indexOf operation was required"
122 ).culprit( "class", this.getClass() );
123 }
124
125 /**
126 * @see org.millscript.commons.util.IList#iterator(boolean)
127 */
128 public ListIterator< Row > iterator( boolean share ) {
129 return new RowListIterator( this.rowData, share );
130 }
131
132 /**
133 * @see org.millscript.commons.util.IMap#size()
134 */
135 public int size() {
136 return this.rowData.length;
137 }
138
139 }