View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   // MillScript-Excel: an Open Spice interpreter and batch website creation tool
3   // Copyright (C) 2006 Open World Ltd, Kevin Rogers
4   //
5   // This file is part of MillScript-Excel.
6   //
7   // MillScript-Excel is free software; you can redistribute it and/or modify it under
8   // the terms of the GNU General Public License as published by the Free
9   // Software Foundation; either version 2 of the License, or (at your option)
10  // any later version.
11  //
12  // MillScript-Excel is distributed in the hope that it will be useful, but WITHOUT
13  // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  // more details.
16  //
17  // You should have received a copy of the GNU General Public License along with
18  // MillScript-Excel; if not, write to the Free Software Foundation, Inc., 59 Temple
19  // Place, Suite 330, Boston, MA  02111-1307  USA
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 &lt; 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             // Return a copy of the relevant slice of the list
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         // TODO Auto-generated method stub
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 }