KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > NumberRecord


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

17         
18
19 /*
20  * NumberRecord.java
21  *
22  * Created on October 1, 2001, 8:01 PM
23  */

24 package org.apache.poi.hssf.record;
25
26 import org.apache.poi.util.LittleEndian;
27 import org.apache.poi.hssf.record.Record;
28
29 /**
30  * Contains a numeric cell value. <P>
31  * REFERENCE: PG 334 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
32  * @author Andrew C. Oliver (acoliver at apache dot org)
33  * @author Jason Height (jheight at chariot dot net dot au)
34  * @version 2.0-pre
35  */

36
37 public class NumberRecord
38     extends Record
39     implements CellValueRecordInterface, Comparable JavaDoc
40 {
41     public static final short sid = 0x203;
42     //private short field_1_row;
43
private int field_1_row;
44     private short field_2_col;
45     private short field_3_xf;
46     private double field_4_value;
47
48     /** Creates new NumberRecord */
49     public NumberRecord()
50     {
51     }
52
53     /**
54      * Constructs a Number record and sets its fields appropriately.
55      *
56      * @param id id must be 0x203 or an exception will be throw upon validation
57      * @param size the size of the data area of the record
58      * @param data data of the record (should not contain sid/len)
59      */

60
61     public NumberRecord(short id, short size, byte [] data)
62     {
63         super(id, size, data);
64     }
65
66     /**
67      * Constructs a Number record and sets its fields appropriately.
68      *
69      * @param id id must be 0x203 or an exception will be throw upon validation
70      * @param size the size of the data area of the record
71      * @param data data of the record (should not contain sid/len)
72      * @param offset of the data
73      */

74
75     public NumberRecord(short id, short size, byte [] data, int offset)
76     {
77         super(id, size, data, offset);
78     }
79
80     /**
81      * called by the constructor, should set class level fields. Should throw
82      * runtime exception for bad/icomplete data.
83      *
84      * @param data raw data
85      * @param size size of data
86      */

87
88     protected void fillFields(byte [] data, short size, int offset)
89     {
90         //field_1_row = LittleEndian.getShort(data, 0 + offset);
91
field_1_row = LittleEndian.getUShort(data, 0 + offset);
92         field_2_col = LittleEndian.getShort(data, 2 + offset);
93         field_3_xf = LittleEndian.getShort(data, 4 + offset);
94         field_4_value = LittleEndian.getDouble(data, 6 + offset);
95     }
96
97     //public void setRow(short row)
98
public void setRow(int row)
99     {
100         field_1_row = row;
101     }
102
103     public void setColumn(short col)
104     {
105         field_2_col = col;
106     }
107
108     /**
109      * set the index to the ExtendedFormat
110      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
111      * @param xf index to the XF record
112      */

113
114     public void setXFIndex(short xf)
115     {
116         field_3_xf = xf;
117     }
118
119     /**
120      * set the value for the cell
121      *
122      * @param value double representing the value
123      */

124
125     public void setValue(double value)
126     {
127         field_4_value = value;
128     }
129
130     //public short getRow()
131
public int getRow()
132     {
133         return field_1_row;
134     }
135
136     public short getColumn()
137     {
138         return field_2_col;
139     }
140
141     /**
142      * get the index to the ExtendedFormat
143      * @see org.apache.poi.hssf.record.ExtendedFormatRecord
144      * @return index to the XF record
145      */

146
147     public short getXFIndex()
148     {
149         return field_3_xf;
150     }
151
152     /**
153      * get the value for the cell
154      *
155      * @return double representing the value
156      */

157
158     public double getValue()
159     {
160         return field_4_value;
161     }
162
163     public String JavaDoc toString()
164     {
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
166
167         buffer.append("[NUMBER]\n");
168         buffer.append(" .row = ")
169             .append(Integer.toHexString(getRow())).append("\n");
170         buffer.append(" .col = ")
171             .append(Integer.toHexString(getColumn())).append("\n");
172         buffer.append(" .xfindex = ")
173             .append(Integer.toHexString(getXFIndex())).append("\n");
174         buffer.append(" .value = ").append(getValue())
175             .append("\n");
176         buffer.append("[/NUMBER]\n");
177         return buffer.toString();
178     }
179
180     /**
181      * called by the class that is responsible for writing this sucker.
182      * Subclasses should implement this so that their data is passed back in a
183      * byte array.
184      *
185      * @return byte array containing instance data
186      */

187
188     public int serialize(int offset, byte [] data)
189     {
190         LittleEndian.putShort(data, 0 + offset, sid);
191         LittleEndian.putShort(data, 2 + offset, ( short ) 14);
192         //LittleEndian.putShort(data, 4 + offset, getRow());
193
LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
194         LittleEndian.putShort(data, 6 + offset, getColumn());
195         LittleEndian.putShort(data, 8 + offset, getXFIndex());
196         LittleEndian.putDouble(data, 10 + offset, getValue());
197         return getRecordSize();
198     }
199
200     public int getRecordSize()
201     {
202         return 18;
203     }
204
205     /**
206      * called by constructor, should throw runtime exception in the event of a
207      * record passed with a differing ID.
208      *
209      * @param id alleged id for this record
210      */

211
212     protected void validateSid(short id)
213     {
214         if (id != sid)
215         {
216             throw new RecordFormatException("NOT A Number RECORD");
217         }
218     }
219
220     public short getSid()
221     {
222         return this.sid;
223     }
224
225     public boolean isBefore(CellValueRecordInterface i)
226     {
227         if (this.getRow() > i.getRow())
228         {
229             return false;
230         }
231         if ((this.getRow() == i.getRow())
232                 && (this.getColumn() > i.getColumn()))
233         {
234             return false;
235         }
236         if ((this.getRow() == i.getRow())
237                 && (this.getColumn() == i.getColumn()))
238         {
239             return false;
240         }
241         return true;
242     }
243
244     public boolean isAfter(CellValueRecordInterface i)
245     {
246         if (this.getRow() < i.getRow())
247         {
248             return false;
249         }
250         if ((this.getRow() == i.getRow())
251                 && (this.getColumn() < i.getColumn()))
252         {
253             return false;
254         }
255         if ((this.getRow() == i.getRow())
256                 && (this.getColumn() == i.getColumn()))
257         {
258             return false;
259         }
260         return true;
261     }
262
263     public boolean isEqual(CellValueRecordInterface i)
264     {
265         return ((this.getRow() == i.getRow())
266                 && (this.getColumn() == i.getColumn()));
267     }
268
269     public boolean isInValueSection()
270     {
271         return true;
272     }
273
274     public boolean isValue()
275     {
276         return true;
277     }
278
279     public int compareTo(Object JavaDoc obj)
280     {
281         CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
282
283         if ((this.getRow() == loc.getRow())
284                 && (this.getColumn() == loc.getColumn()))
285         {
286             return 0;
287         }
288         if (this.getRow() < loc.getRow())
289         {
290             return -1;
291         }
292         if (this.getRow() > loc.getRow())
293         {
294             return 1;
295         }
296         if (this.getColumn() < loc.getColumn())
297         {
298             return -1;
299         }
300         if (this.getColumn() > loc.getColumn())
301         {
302             return 1;
303         }
304         return -1;
305     }
306
307     public boolean equals(Object JavaDoc obj)
308     {
309         if (!(obj instanceof CellValueRecordInterface))
310         {
311             return false;
312         }
313         CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
314
315         if ((this.getRow() == loc.getRow())
316                 && (this.getColumn() == loc.getColumn()))
317         {
318             return true;
319         }
320         return false;
321     }
322
323     public Object JavaDoc clone() {
324       NumberRecord rec = new NumberRecord();
325       rec.field_1_row = field_1_row;
326       rec.field_2_col = field_2_col;
327       rec.field_3_xf = field_3_xf;
328       rec.field_4_value = field_4_value;
329       return rec;
330     }
331 }
332
Popular Tags