KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jxl > biff > formula > BinaryOperator


1 /*********************************************************************
2 *
3 * Copyright (C) 2002 Andrew Khan
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 ***************************************************************************/

19
20 package jxl.biff.formula;
21
22 import java.util.Stack JavaDoc;
23
24 import jxl.biff.IntegerHelper;
25
26 /**
27  * A cell reference in a formula
28  */

29 abstract class BinaryOperator extends Operator implements ParsedThing
30 {
31   /**
32    * Constructor
33    */

34   public BinaryOperator()
35   {
36   }
37
38   /**
39    * Reads the ptg data from the array starting at the specified position
40    *
41    * @param data the RPN array
42    * @param pos the current position in the array, excluding the ptg identifier
43    * @return the number of bytes read
44    */

45   public int read(byte[] data, int pos)
46   {
47     return 0;
48   }
49
50   /**
51    * Gets the operands for this operator from the stack
52    */

53   public void getOperands(Stack JavaDoc s)
54   {
55     ParseItem o1 = (ParseItem) s.pop();
56     ParseItem o2 = (ParseItem) s.pop();
57
58     add(o1);
59     add(o2);
60   }
61
62   /**
63    * Gets the string version of this binary operator
64    *
65    * @param buf a the string buffer
66    */

67   public void getString(StringBuffer JavaDoc buf)
68   {
69     ParseItem[] operands = getOperands();
70     operands[1].getString(buf);
71     buf.append(getSymbol());
72     operands[0].getString(buf);
73   }
74
75   /**
76    * Adjusts all the relative cell references in this formula by the
77    * amount specified. Used when copying formulas
78    *
79    * @param colAdjust the amount to add on to each relative cell reference
80    * @param rowAdjust the amount to add on to each relative row reference
81    */

82   public void adjustRelativeCellReferences(int colAdjust, int rowAdjust)
83   {
84     ParseItem[] operands = getOperands();
85     operands[1].adjustRelativeCellReferences(colAdjust, rowAdjust);
86     operands[0].adjustRelativeCellReferences(colAdjust, rowAdjust);
87   }
88
89   /**
90    * Called when a column is inserted on the specified sheet. Tells
91    * the formula parser to update all of its cell references beyond this
92    * column
93    *
94    * @param sheetIndex the sheet on which the column was inserted
95    * @param col the column number which was inserted
96    * @param currentSheet TRUE if this formula is on the sheet in which the
97    * column was inserted, FALSE otherwise
98    */

99   void columnInserted(int sheetIndex, int col, boolean currentSheet)
100   {
101     ParseItem[] operands = getOperands();
102     operands[1].columnInserted(sheetIndex, col, currentSheet);
103     operands[0].columnInserted(sheetIndex, col, currentSheet);
104   }
105
106   /**
107    * Called when a column is inserted on the specified sheet. Tells
108    * the formula parser to update all of its cell references beyond this
109    * column
110    *
111    * @param sheetIndex the sheet on which the column was removed
112    * @param col the column number which was removed
113    * @param currentSheet TRUE if this formula is on the sheet in which the
114    * column was inserted, FALSE otherwise
115    */

116   void columnRemoved(int sheetIndex, int col, boolean currentSheet)
117   {
118     ParseItem[] operands = getOperands();
119     operands[1].columnRemoved(sheetIndex, col, currentSheet);
120     operands[0].columnRemoved(sheetIndex, col, currentSheet);
121   }
122
123   /**
124    * Called when a column is inserted on the specified sheet. Tells
125    * the formula parser to update all of its cell references beyond this
126    * column
127    *
128    * @param sheetIndex the sheet on which the row was inserted
129    * @param row the row number which was inserted
130    * @param currentSheet TRUE if this formula is on the sheet in which the
131    * column was inserted, FALSE otherwise
132    */

133   void rowInserted(int sheetIndex, int row, boolean currentSheet)
134   {
135     ParseItem[] operands = getOperands();
136     operands[1].rowInserted(sheetIndex, row, currentSheet);
137     operands[0].rowInserted(sheetIndex, row, currentSheet);
138   }
139
140   /**
141    * Called when a column is inserted on the specified sheet. Tells
142    * the formula parser to update all of its cell references beyond this
143    * column
144    *
145    * @param sheetIndex the sheet on which the row was removed
146    * @param row the row number which was removed
147    * @param currentSheet TRUE if this formula is on the sheet in which the
148    * column was inserted, FALSE otherwise
149    */

150   void rowRemoved(int sheetIndex, int row, boolean currentSheet)
151   {
152     ParseItem[] operands = getOperands();
153     operands[1].rowRemoved(sheetIndex, row, currentSheet);
154     operands[0].rowRemoved(sheetIndex, row, currentSheet);
155   }
156
157   /**
158    * Gets the token representation of this item in RPN
159    *
160    * @return the bytes applicable to this formula
161    */

162   byte[] getBytes()
163   {
164     // Get the data for the operands
165
ParseItem[] operands = getOperands();
166     byte[] data = new byte[0];
167
168     // Get the operands in reverse order to get the RPN
169
for (int i = operands.length - 1 ; i >= 0 ; i--)
170     {
171       byte[] opdata = operands[i].getBytes();
172
173       // Grow the array
174
byte[] newdata = new byte[data.length + opdata.length];
175       System.arraycopy(data, 0, newdata, 0, data.length);
176       System.arraycopy(opdata, 0, newdata, data.length, opdata.length);
177       data = newdata;
178     }
179
180     // Add on the operator byte
181
byte[] newdata = new byte[data.length + 1];
182     System.arraycopy(data, 0, newdata, 0, data.length);
183     newdata[data.length] = getToken().getCode();
184
185     return newdata;
186   }
187
188   /**
189    * Abstract method which gets the binary operator string symbol
190    *
191    * @return the string symbol for this token
192    */

193   abstract String JavaDoc getSymbol();
194
195   /**
196    * Abstract method which gets the token for this operator
197    *
198    * @return the string symbol for this token
199    */

200   abstract Token getToken();
201
202 }
203
204
205
206
207
208
Popular Tags