KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > aggregates > FormulaRecordAggregate


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hssf.record.aggregates;
19
20 import org.apache.poi.hssf.record.*;
21
22 /**
23  * The formula record aggregate is used to join together the formula record and it's
24  * (optional) string record and (optional) Shared Formula Record (template reads, excel optimization).
25  *
26  * @author Glen Stampoultzis (glens at apache.org)
27  */

28 public class FormulaRecordAggregate
29         extends Record
30         implements CellValueRecordInterface, Comparable JavaDoc
31 {
32     public final static short sid = -2000;
33
34     private FormulaRecord formulaRecord;
35     private StringRecord stringRecord;
36     
37     /**
38      * will only be set through the RecordFactory
39      */

40      private SharedFormulaRecord sharedFormulaRecord;
41
42     public FormulaRecordAggregate( FormulaRecord formulaRecord, StringRecord stringRecord )
43     {
44         this.formulaRecord = formulaRecord;
45         this.stringRecord = stringRecord;
46     }
47
48     /**
49      * Used only in the clone
50      * @param formulaRecord
51      * @param stringRecord
52      * @param sharedRecord
53      */

54     public FormulaRecordAggregate( FormulaRecord formulaRecord, StringRecord stringRecord, SharedFormulaRecord sharedRecord)
55     {
56           this.formulaRecord = formulaRecord;
57           this.stringRecord = stringRecord;
58           this.sharedFormulaRecord = sharedRecord;
59     }
60
61
62
63     protected void validateSid( short id )
64     {
65     }
66
67     protected void fillFields( byte[] data, short size, int offset )
68     {
69     }
70
71     /**
72      * called by the class that is responsible for writing this sucker.
73      * Subclasses should implement this so that their data is passed back in a
74      * byte array.
75      *
76      * @param offset to begin writing at
77      * @param data byte array containing instance data
78      * @return number of bytes written
79      */

80
81     public int serialize( int offset, byte[] data )
82     {
83         int pos = offset;
84         pos += formulaRecord.serialize(pos, data);
85         if (this.getSharedFormulaRecord() != null)
86         {
87                 pos += getSharedFormulaRecord().serialize(pos, data);
88         }
89          if (stringRecord != null)
90         {
91             pos += stringRecord.serialize(pos, data);
92         }
93         return pos - offset;
94         
95     }
96
97     /**
98      * gives the current serialized size of the record. Should include the sid and reclength (4 bytes).
99      */

100     public int getRecordSize()
101     {
102         int size = formulaRecord.getRecordSize() + (stringRecord == null ? 0 : stringRecord.getRecordSize());
103         size += (getSharedFormulaRecord() == null) ? 0 : getSharedFormulaRecord().getRecordSize();
104         return size;
105     }
106
107
108     /**
109      * return the non static version of the id for this record.
110      */

111     public short getSid()
112     {
113         return sid;
114     }
115
116     public void setStringRecord( StringRecord stringRecord )
117     {
118         this.stringRecord = stringRecord;
119     }
120
121     public void setFormulaRecord( FormulaRecord formulaRecord )
122     {
123         this.formulaRecord = formulaRecord;
124     }
125
126     public FormulaRecord getFormulaRecord()
127     {
128         return formulaRecord;
129     }
130
131     public StringRecord getStringRecord()
132     {
133         return stringRecord;
134     }
135
136     public boolean isEqual(CellValueRecordInterface i)
137     {
138         return formulaRecord.isEqual( i );
139     }
140
141     public boolean isAfter(CellValueRecordInterface i)
142     {
143         return formulaRecord.isAfter( i );
144     }
145
146     public boolean isBefore(CellValueRecordInterface i)
147     {
148         return formulaRecord.isBefore( i );
149     }
150
151     public short getXFIndex()
152     {
153         return formulaRecord.getXFIndex();
154     }
155
156     public void setXFIndex(short xf)
157     {
158         formulaRecord.setXFIndex( xf );
159     }
160
161     public void setColumn(short col)
162     {
163         formulaRecord.setColumn( col );
164     }
165
166     public void setRow(int row)
167     {
168         formulaRecord.setRow( row );
169     }
170
171     public short getColumn()
172     {
173         return formulaRecord.getColumn();
174     }
175
176     public int getRow()
177     {
178         return formulaRecord.getRow();
179     }
180
181     public int compareTo(Object JavaDoc o)
182     {
183         return formulaRecord.compareTo( o );
184     }
185
186     public boolean equals(Object JavaDoc obj)
187     {
188         return formulaRecord.equals( obj );
189     }
190
191     public String JavaDoc toString()
192     {
193         return formulaRecord.toString();
194     }
195     
196     /**
197      * @see java.lang.Object#clone()
198      */

199     public Object JavaDoc clone() {
200             StringRecord clonedString = (stringRecord == null) ? null : (StringRecord)stringRecord.clone();
201             SharedFormulaRecord clonedShared = (sharedFormulaRecord == null) ? null : (SharedFormulaRecord)sharedFormulaRecord.clone();
202             
203         return new FormulaRecordAggregate((FormulaRecord) this.formulaRecord.clone(), clonedString, clonedShared);
204     }
205
206
207
208    /**
209     * @return SharedFormulaRecord
210     */

211    public SharedFormulaRecord getSharedFormulaRecord() {
212       return sharedFormulaRecord;
213    }
214
215    /**
216     * Sets the sharedFormulaRecord, only set from RecordFactory since they are not generated by POI and are an Excel optimization
217     * @param sharedFormulaRecord The sharedFormulaRecord to set
218     */

219    public void setSharedFormulaRecord(SharedFormulaRecord sharedFormulaRecord) {
220       this.sharedFormulaRecord = sharedFormulaRecord;
221    }
222
223    /*
224     * Setting to true so that this value does not abort the whole ValueAggregation
225     * (non-Javadoc)
226     * @see org.apache.poi.hssf.record.Record#isInValueSection()
227     */

228    public boolean isInValueSection() {
229
230       return true;
231    }
232    
233    public String JavaDoc getStringValue() {
234         if(stringRecord==null) return null;
235         return stringRecord.getString();
236    }
237
238 }
239
Popular Tags