View Javadoc

1   /*$Id: AbstractColumnWrapperTable.java,v 1.2 2004/12/07 16:50:33 jdt Exp $
2    * Created on 07-Dec-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.scripting.table;
12  
13  import java.io.IOException;
14  import java.util.Iterator;
15  import java.util.List;
16  import java.util.Map;
17  
18  import uk.ac.starlink.table.ColumnInfo;
19  import uk.ac.starlink.table.RowSequence;
20  import uk.ac.starlink.table.StarTable;
21  import uk.ac.starlink.table.WrapperStarTable;
22  
23  /*** abstract class for wrappers that define a new column in some way.
24   * overrides all methods to do with meta-data. Extensions of this class just have to define the cell values.
25   * @author Noel Winstanley nw@jb.man.ac.uk 07-Dec-2004
26   *
27   */
28  abstract class AbstractColumnWrapperTable extends WrapperScriptStarTable  {
29  
30      /*** Construct a new AbstractColumnWrapperTable
31       * @param meta metadata for new column
32       * @param original original table
33       * 
34       */
35      public AbstractColumnWrapperTable(ColumnInfo meta, StarTable original) {
36          super(original);
37          this.col = meta;
38          this.originalColCount = original.getColumnCount();
39      }
40      protected final ColumnInfo col;
41      protected final int originalColCount;
42     
43      public Object getCell(long arg0, int arg1) throws IOException {
44          if (arg1 == originalColCount) {
45              try {
46                  return computeValue(super.getRow(arg0));
47              } catch (IOException e) {
48                  throw e;
49              } catch (Exception e) {
50                  IOException ioe = new IOException(e.getMessage());
51                  ioe.initCause(e);
52                  throw ioe;
53              }
54          } else {
55              return super.getCell(arg0, arg1);
56          }
57      }
58  
59      public int getColumnCount() {
60          return originalColCount + 1;
61      }
62      public ColumnInfo getColumnInfo(int arg0) {
63          if (arg0 == originalColCount) {
64              return col;
65          } else {
66              return super.getColumnInfo(arg0);
67          }
68      }
69      public Object[] getRow(long arg0) throws IOException {
70          Object[] row =  super.getRow(arg0);
71          try {
72          Object val = computeValue(row);
73          Object[] newRow = new Object[originalColCount + 1];
74          System.arraycopy(row,0,newRow,0,row.length);
75          newRow[originalColCount] = val;
76          return newRow;
77          } catch (IOException e) {
78              throw e;
79          } catch (Exception e) {
80              IOException ioe = new IOException(e.getMessage());
81              ioe.initCause(e);
82              throw ioe;
83          }        
84      }
85      public RowSequence getRowSequence() throws IOException {
86          final RowSequence seq = super.getRowSequence();
87          return new RowSequence() {
88  
89              public void next() throws IOException {
90                  seq.next();
91              }
92  
93              public boolean hasNext() {
94                  return seq.hasNext();
95              }
96  
97              public Object getCell(int arg0) throws IOException {
98                  if (arg0 == originalColCount) {
99                      try {
100                     return computeValue(seq.getRow());
101                     } catch (IOException e) {
102                         throw e;
103                     } catch (Exception e) {
104                         IOException ioe = new IOException(e.getMessage());
105                         ioe.initCause(e);
106                         throw ioe;
107                     }                    
108                 } else {
109                     return seq.getCell(arg0);
110                 }
111             }
112 
113             public Object[] getRow() throws IOException {
114                 Object[] row =  seq.getRow();
115                 try {
116                 Object val = computeValue(row);
117                 Object[] newRow = new Object[originalColCount + 1];
118                 System.arraycopy(row,0,newRow,0,row.length);
119                 newRow[originalColCount] = val;
120                 return newRow;
121                 } catch (IOException e) {
122                     throw e;
123                 } catch (Exception e) {
124                     IOException ioe = new IOException(e.getMessage());
125                     ioe.initCause(e);
126                     throw ioe;
127                 }                
128             }
129 
130             public void close() throws IOException {
131                 seq.close();
132             }
133         };
134     }
135     /*** define the value for a cell in the new column, based on the values of the other cells in this row */
136     protected abstract Object computeValue(Object[] row) throws Exception;
137     
138 }
139 
140 
141 /* 
142 $Log: AbstractColumnWrapperTable.java,v $
143 Revision 1.2  2004/12/07 16:50:33  jdt
144 merges from scripting-nww-805
145 
146 Revision 1.1.2.1  2004/12/07 14:47:58  nw
147 got table manipulation working.
148  
149 */