1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * NumberRecord.java
58    *
59    * Created on October 1, 2001, 8:01 PM
60    */
61   package org.apache.poi.hssf.record;
62   
63   import org.apache.poi.util.LittleEndian;
64   import org.apache.poi.hssf.record.Record;
65   
66   /**
67    * Contains a numeric cell value. <P>
68    * REFERENCE:  PG 334 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
69    * @author Andrew C. Oliver (acoliver at apache dot org)
70    * @version 2.0-pre
71    */
72   
73   public class NumberRecord
74       extends Record
75       implements CellValueRecordInterface, Comparable
76   {
77       public static final short sid = 0x203;
78       private short             field_1_row;
79       private short             field_2_col;
80       private short             field_3_xf;
81       private double            field_4_value;
82   
83       /** Creates new NumberRecord */
84       public NumberRecord()
85       {
86       }
87   
88       /**
89        * Constructs a Number record and sets its fields appropriately.
90        *
91        * @param id     id must be 0x203 or an exception will be throw upon validation
92        * @param size  the size of the data area of the record
93        * @param data  data of the record (should not contain sid/len)
94        */
95   
96       public NumberRecord(short id, short size, byte [] data)
97       {
98           super(id, size, data);
99       }
100  
101      /**
102       * Constructs a Number record and sets its fields appropriately.
103       *
104       * @param id     id must be 0x203 or an exception will be throw upon validation
105       * @param size  the size of the data area of the record
106       * @param data  data of the record (should not contain sid/len)
107       * @param offset of the data
108       */
109  
110      public NumberRecord(short id, short size, byte [] data, int offset)
111      {
112          super(id, size, data, offset);
113      }
114  
115      /**
116       * called by the constructor, should set class level fields.  Should throw
117       * runtime exception for bad/icomplete data.
118       *
119       * @param data raw data
120       * @param size size of data
121       */
122  
123      protected void fillFields(byte [] data, short size, int offset)
124      {
125          field_1_row   = LittleEndian.getShort(data, 0 + offset);
126          field_2_col   = LittleEndian.getShort(data, 2 + offset);
127          field_3_xf    = LittleEndian.getShort(data, 4 + offset);
128          field_4_value = LittleEndian.getDouble(data, 6 + offset);
129      }
130  
131      public void setRow(short row)
132      {
133          field_1_row = row;
134      }
135  
136      public void setColumn(short col)
137      {
138          field_2_col = col;
139      }
140  
141      /**
142       * set the index to the ExtendedFormat
143       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
144       * @param xf  index to the XF record
145       */
146  
147      public void setXFIndex(short xf)
148      {
149          field_3_xf = xf;
150      }
151  
152      /**
153       * set the value for the cell
154       *
155       * @param value  double representing the value
156       */
157  
158      public void setValue(double value)
159      {
160          field_4_value = value;
161      }
162  
163      public short getRow()
164      {
165          return field_1_row;
166      }
167  
168      public short getColumn()
169      {
170          return field_2_col;
171      }
172  
173      /**
174       * get the index to the ExtendedFormat
175       * @see org.apache.poi.hssf.record.ExtendedFormatRecord
176       * @return index to the XF record
177       */
178  
179      public short getXFIndex()
180      {
181          return field_3_xf;
182      }
183  
184      /**
185       * get the value for the cell
186       *
187       * @return double representing the value
188       */
189  
190      public double getValue()
191      {
192          return field_4_value;
193      }
194  
195      public String toString()
196      {
197          StringBuffer buffer = new StringBuffer();
198  
199          buffer.append("[NUMBER]\n");
200          buffer.append("    .row            = ")
201              .append(Integer.toHexString(getRow())).append("\n");
202          buffer.append("    .col            = ")
203              .append(Integer.toHexString(getColumn())).append("\n");
204          buffer.append("    .xfindex        = ")
205              .append(Integer.toHexString(getXFIndex())).append("\n");
206          buffer.append("    .value          = ").append(getValue())
207              .append("\n");
208          buffer.append("[/NUMBER]\n");
209          return buffer.toString();
210      }
211  
212      /**
213       * called by the class that is responsible for writing this sucker.
214       * Subclasses should implement this so that their data is passed back in a
215       * byte array.
216       *
217       * @return byte array containing instance data
218       */
219  
220      public int serialize(int offset, byte [] data)
221      {
222          LittleEndian.putShort(data, 0 + offset, sid);
223          LittleEndian.putShort(data, 2 + offset, ( short ) 14);
224          LittleEndian.putShort(data, 4 + offset, getRow());
225          LittleEndian.putShort(data, 6 + offset, getColumn());
226          LittleEndian.putShort(data, 8 + offset, getXFIndex());
227          LittleEndian.putDouble(data, 10 + offset, getValue());
228          return getRecordSize();
229      }
230  
231      public int getRecordSize()
232      {
233          return 18;
234      }
235  
236      /**
237       * called by constructor, should throw runtime exception in the event of a
238       * record passed with a differing ID.
239       *
240       * @param id alleged id for this record
241       */
242  
243      protected void validateSid(short id)
244      {
245          if (id != sid)
246          {
247              throw new RecordFormatException("NOT A Number RECORD");
248          }
249      }
250  
251      public short getSid()
252      {
253          return this.sid;
254      }
255  
256      public boolean isBefore(CellValueRecordInterface i)
257      {
258          if (this.getRow() > i.getRow())
259          {
260              return false;
261          }
262          if ((this.getRow() == i.getRow())
263                  && (this.getColumn() > i.getColumn()))
264          {
265              return false;
266          }
267          if ((this.getRow() == i.getRow())
268                  && (this.getColumn() == i.getColumn()))
269          {
270              return false;
271          }
272          return true;
273      }
274  
275      public boolean isAfter(CellValueRecordInterface i)
276      {
277          if (this.getRow() < i.getRow())
278          {
279              return false;
280          }
281          if ((this.getRow() == i.getRow())
282                  && (this.getColumn() < i.getColumn()))
283          {
284              return false;
285          }
286          if ((this.getRow() == i.getRow())
287                  && (this.getColumn() == i.getColumn()))
288          {
289              return false;
290          }
291          return true;
292      }
293  
294      public boolean isEqual(CellValueRecordInterface i)
295      {
296          return ((this.getRow() == i.getRow())
297                  && (this.getColumn() == i.getColumn()));
298      }
299  
300      public boolean isInValueSection()
301      {
302          return true;
303      }
304  
305      public boolean isValue()
306      {
307          return true;
308      }
309  
310      public int compareTo(Object obj)
311      {
312          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
313  
314          if ((this.getRow() == loc.getRow())
315                  && (this.getColumn() == loc.getColumn()))
316          {
317              return 0;
318          }
319          if (this.getRow() < loc.getRow())
320          {
321              return -1;
322          }
323          if (this.getRow() > loc.getRow())
324          {
325              return 1;
326          }
327          if (this.getColumn() < loc.getColumn())
328          {
329              return -1;
330          }
331          if (this.getColumn() > loc.getColumn())
332          {
333              return 1;
334          }
335          return -1;
336      }
337  
338      public boolean equals(Object obj)
339      {
340          if (!(obj instanceof CellValueRecordInterface))
341          {
342              return false;
343          }
344          CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
345  
346          if ((this.getRow() == loc.getRow())
347                  && (this.getColumn() == loc.getColumn()))
348          {
349              return true;
350          }
351          return false;
352      }
353  }
354