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 int field_1_row; 80 private short field_2_col; 81 private short field_3_xf; 82 private double field_4_value; 83 84 /** Creates new NumberRecord */ 85 public NumberRecord() 86 { 87 } 88 89 /** 90 * Constructs a Number record and sets its fields appropriately. 91 * 92 * @param id id must be 0x203 or an exception will be throw upon validation 93 * @param size the size of the data area of the record 94 * @param data data of the record (should not contain sid/len) 95 */ 96 97 public NumberRecord(short id, short size, byte [] data) 98 { 99 super(id, size, data); 100 } 101 102 /** 103 * Constructs a Number record and sets its fields appropriately. 104 * 105 * @param id id must be 0x203 or an exception will be throw upon validation 106 * @param size the size of the data area of the record 107 * @param data data of the record (should not contain sid/len) 108 * @param offset of the data 109 */ 110 111 public NumberRecord(short id, short size, byte [] data, int offset) 112 { 113 super(id, size, data, offset); 114 } 115 116 /** 117 * called by the constructor, should set class level fields. Should throw 118 * runtime exception for bad/icomplete data. 119 * 120 * @param data raw data 121 * @param size size of data 122 */ 123 124 protected void fillFields(byte [] data, short size, int offset) 125 { 126 //field_1_row = LittleEndian.getShort(data, 0 + offset); 127 field_1_row = LittleEndian.getUShort(data, 0 + offset); 128 field_2_col = LittleEndian.getShort(data, 2 + offset); 129 field_3_xf = LittleEndian.getShort(data, 4 + offset); 130 field_4_value = LittleEndian.getDouble(data, 6 + offset); 131 } 132 133 //public void setRow(short row) 134 public void setRow(int row) 135 { 136 field_1_row = row; 137 } 138 139 public void setColumn(short col) 140 { 141 field_2_col = col; 142 } 143 144 /** 145 * set the index to the ExtendedFormat 146 * @see org.apache.poi.hssf.record.ExtendedFormatRecord 147 * @param xf index to the XF record 148 */ 149 150 public void setXFIndex(short xf) 151 { 152 field_3_xf = xf; 153 } 154 155 /** 156 * set the value for the cell 157 * 158 * @param value double representing the value 159 */ 160 161 public void setValue(double value) 162 { 163 field_4_value = value; 164 } 165 166 //public short getRow() 167 public int getRow() 168 { 169 return field_1_row; 170 } 171 172 public short getColumn() 173 { 174 return field_2_col; 175 } 176 177 /** 178 * get the index to the ExtendedFormat 179 * @see org.apache.poi.hssf.record.ExtendedFormatRecord 180 * @return index to the XF record 181 */ 182 183 public short getXFIndex() 184 { 185 return field_3_xf; 186 } 187 188 /** 189 * get the value for the cell 190 * 191 * @return double representing the value 192 */ 193 194 public double getValue() 195 { 196 return field_4_value; 197 } 198 199 public String toString() 200 { 201 StringBuffer buffer = new StringBuffer(); 202 203 buffer.append("[NUMBER]\n"); 204 buffer.append(" .row = ") 205 .append(Integer.toHexString(getRow())).append("\n"); 206 buffer.append(" .col = ") 207 .append(Integer.toHexString(getColumn())).append("\n"); 208 buffer.append(" .xfindex = ") 209 .append(Integer.toHexString(getXFIndex())).append("\n"); 210 buffer.append(" .value = ").append(getValue()) 211 .append("\n"); 212 buffer.append("[/NUMBER]\n"); 213 return buffer.toString(); 214 } 215 216 /** 217 * called by the class that is responsible for writing this sucker. 218 * Subclasses should implement this so that their data is passed back in a 219 * byte array. 220 * 221 * @return byte array containing instance data 222 */ 223 224 public int serialize(int offset, byte [] data) 225 { 226 LittleEndian.putShort(data, 0 + offset, sid); 227 LittleEndian.putShort(data, 2 + offset, ( short ) 14); 228 //LittleEndian.putShort(data, 4 + offset, getRow()); 229 LittleEndian.putShort(data, 4 + offset, ( short ) getRow()); 230 LittleEndian.putShort(data, 6 + offset, getColumn()); 231 LittleEndian.putShort(data, 8 + offset, getXFIndex()); 232 LittleEndian.putDouble(data, 10 + offset, getValue()); 233 return getRecordSize(); 234 } 235 236 public int getRecordSize() 237 { 238 return 18; 239 } 240 241 /** 242 * called by constructor, should throw runtime exception in the event of a 243 * record passed with a differing ID. 244 * 245 * @param id alleged id for this record 246 */ 247 248 protected void validateSid(short id) 249 { 250 if (id != sid) 251 { 252 throw new RecordFormatException("NOT A Number RECORD"); 253 } 254 } 255 256 public short getSid() 257 { 258 return this.sid; 259 } 260 261 public boolean isBefore(CellValueRecordInterface i) 262 { 263 if (this.getRow() > i.getRow()) 264 { 265 return false; 266 } 267 if ((this.getRow() == i.getRow()) 268 && (this.getColumn() > i.getColumn())) 269 { 270 return false; 271 } 272 if ((this.getRow() == i.getRow()) 273 && (this.getColumn() == i.getColumn())) 274 { 275 return false; 276 } 277 return true; 278 } 279 280 public boolean isAfter(CellValueRecordInterface i) 281 { 282 if (this.getRow() < i.getRow()) 283 { 284 return false; 285 } 286 if ((this.getRow() == i.getRow()) 287 && (this.getColumn() < i.getColumn())) 288 { 289 return false; 290 } 291 if ((this.getRow() == i.getRow()) 292 && (this.getColumn() == i.getColumn())) 293 { 294 return false; 295 } 296 return true; 297 } 298 299 public boolean isEqual(CellValueRecordInterface i) 300 { 301 return ((this.getRow() == i.getRow()) 302 && (this.getColumn() == i.getColumn())); 303 } 304 305 public boolean isInValueSection() 306 { 307 return true; 308 } 309 310 public boolean isValue() 311 { 312 return true; 313 } 314 315 public int compareTo(Object obj) 316 { 317 CellValueRecordInterface loc = ( CellValueRecordInterface ) obj; 318 319 if ((this.getRow() == loc.getRow()) 320 && (this.getColumn() == loc.getColumn())) 321 { 322 return 0; 323 } 324 if (this.getRow() < loc.getRow()) 325 { 326 return -1; 327 } 328 if (this.getRow() > loc.getRow()) 329 { 330 return 1; 331 } 332 if (this.getColumn() < loc.getColumn()) 333 { 334 return -1; 335 } 336 if (this.getColumn() > loc.getColumn()) 337 { 338 return 1; 339 } 340 return -1; 341 } 342 343 public boolean equals(Object obj) 344 { 345 if (!(obj instanceof CellValueRecordInterface)) 346 { 347 return false; 348 } 349 CellValueRecordInterface loc = ( CellValueRecordInterface ) obj; 350 351 if ((this.getRow() == loc.getRow()) 352 && (this.getColumn() == loc.getColumn())) 353 { 354 return true; 355 } 356 return false; 357 } 358 } 359