1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.scripting.table;
12
13 import java.io.IOException;
14 import java.lang.reflect.InvocationTargetException;
15 import java.lang.reflect.Method;
16 import java.util.Arrays;
17 import java.util.Iterator;
18
19 import uk.ac.starlink.table.ColumnInfo;
20 import uk.ac.starlink.table.RowSequence;
21 import uk.ac.starlink.table.StarTable;
22 import uk.ac.starlink.table.WrapperStarTable;
23
24 /***
25 * Implementation of the Extra table methods.
26 * Done like this so that we can use an instance of this in each table class to simulate 'multiple inheritance'
27 * @author Noel Winstanley nw@jb.man.ac.uk 06-Dec-2004
28 *
29 */
30 abstract class ExtraTableMethodsImpl implements ExtraTableMethods{
31 static Class groovyCallableClass;
32 static Class groovyColumnWrapper;
33 static {
34 try {
35 groovyCallableClass = Class.forName("groovy.lang.Closure");
36 groovyColumnWrapper = Class.forName("org.astrogrid.scripting.table.GroovyColumnWrapperTable");
37 } catch (Exception e) {
38
39 groovyCallableClass = null;
40 groovyColumnWrapper = null;
41 }
42
43 }
44
45 public ScriptStarTable addColumn(ColumnInfo meta, Object colValue) {
46
47 if (colValue instanceof Method) {
48 return new MethodColumnWrapperTable(meta,(Method)colValue,getTable());
49 }
50 if (colValue instanceof ColumnFunction) {
51 return new FunctionColumnWrapperTable(meta,(ColumnFunction)colValue,getTable());
52 }
53 if (groovyCallableClass != null && groovyCallableClass.isAssignableFrom(colValue.getClass())) {
54 try {
55 return (ScriptStarTable) groovyColumnWrapper.getConstructors()[0].newInstance(new Object[]{meta,colValue,getTable()});
56 } catch (IllegalArgumentException e) {
57 throw new RuntimeException(e);
58 } catch (SecurityException e) {
59 throw new RuntimeException(e);
60 } catch (InstantiationException e) {
61 throw new RuntimeException(e);
62 } catch (IllegalAccessException e) {
63 throw new RuntimeException(e);
64 } catch (InvocationTargetException e) {
65 throw new RuntimeException(e);
66 }
67 }
68 return new ConstantColumnWrapperTable(meta,colValue,getTable());
69 }
70 public MutableScriptStarTable asMutableTable() throws IOException {
71 MutableScriptStarTable result = new MutableScriptStarTable(getTable());
72 RowSequence seq = getTable().getRowSequence();
73 while (seq.hasNext()) {
74 seq.next();
75 result.addRow(seq.getRow());
76 }
77 return result;
78 }
79 public ScriptStarTable removeColumn(int index) {
80 return new RemoveColumnWrapperTable(getTable(),index);
81
82 }
83 /*** to be implemented by extenioins - defines where the table comes from */
84 protected abstract StarTable getTable();
85
86
87 public Iterator iterator() throws IOException {
88 final RowSequence seq = getTable().getRowSequence();
89 return new Iterator() {
90
91 public void remove() {
92 throw new UnsupportedOperationException("Can't remove from tables");
93 }
94
95 public boolean hasNext() {
96 return seq.hasNext();
97 }
98
99 public Object next() {
100 try {
101 seq.next();
102 return Arrays.asList(seq.getRow());
103 } catch (IOException e) {
104 throw new RuntimeException("StarTable.iterator().next() throw exception",e);
105 }
106 }
107 };
108 }
109
110
111 public Iterator columnIterator(final int col) throws IOException {
112 final RowSequence seq = getTable().getRowSequence();
113 return new Iterator() {
114
115 public void remove() {
116 throw new UnsupportedOperationException("Can't remove from tables");
117 }
118
119 public boolean hasNext() {
120 return seq.hasNext();
121 }
122
123 public Object next() {
124 try {
125 seq.next();
126 return seq.getCell(col);
127 } catch (IOException e) {
128 throw new RuntimeException("StarTable.iterator().next() throw exception",e);
129 }
130 }
131 };
132 }
133
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151