Inspired by bug #44958 - Record level support for Data Tables. (No formula parser...
[poi.git] / src / java / org / apache / poi / hssf / record / NumberRecord.java
blobb21e488ed73c30417dc1809adf86ca2f1cb23a4c
2 /* ====================================================================
3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 ==================================================================== */
21 * NumberRecord.java
23 * Created on October 1, 2001, 8:01 PM
25 package org.apache.poi.hssf.record;
27 import org.apache.poi.util.LittleEndian;
28 import org.apache.poi.hssf.record.Record;
30 /**
31 * Contains a numeric cell value. <P>
32 * REFERENCE: PG 334 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
33 * @author Andrew C. Oliver (acoliver at apache dot org)
34 * @author Jason Height (jheight at chariot dot net dot au)
35 * @version 2.0-pre
38 public class NumberRecord
39 extends Record
40 implements CellValueRecordInterface, Comparable
42 public static final short sid = 0x203;
43 //private short field_1_row;
44 private int field_1_row;
45 private short field_2_col;
46 private short field_3_xf;
47 private double field_4_value;
49 /** Creates new NumberRecord */
50 public NumberRecord()
54 /**
55 * Constructs a Number record and sets its fields appropriately.
57 * @param in the RecordInputstream to read the record from
60 public NumberRecord(RecordInputStream in)
62 super(in);
65 /**
66 * @param in the RecordInputstream to read the record from
69 protected void fillFields(RecordInputStream in)
71 //field_1_row = LittleEndian.getShort(data, 0 + offset);
72 field_1_row = in.readUShort();
73 field_2_col = in.readShort();
74 field_3_xf = in.readShort();
75 field_4_value = in.readDouble();
78 //public void setRow(short row)
79 public void setRow(int row)
81 field_1_row = row;
84 public void setColumn(short col)
86 field_2_col = col;
89 /**
90 * set the index to the ExtendedFormat
91 * @see org.apache.poi.hssf.record.ExtendedFormatRecord
92 * @param xf index to the XF record
95 public void setXFIndex(short xf)
97 field_3_xf = xf;
101 * set the value for the cell
103 * @param value double representing the value
106 public void setValue(double value)
108 field_4_value = value;
111 //public short getRow()
112 public int getRow()
114 return field_1_row;
117 public short getColumn()
119 return field_2_col;
123 * get the index to the ExtendedFormat
124 * @see org.apache.poi.hssf.record.ExtendedFormatRecord
125 * @return index to the XF record
128 public short getXFIndex()
130 return field_3_xf;
134 * get the value for the cell
136 * @return double representing the value
139 public double getValue()
141 return field_4_value;
144 public String toString()
146 StringBuffer buffer = new StringBuffer();
148 buffer.append("[NUMBER]\n");
149 buffer.append(" .row = ")
150 .append(Integer.toHexString(getRow())).append("\n");
151 buffer.append(" .col = ")
152 .append(Integer.toHexString(getColumn())).append("\n");
153 buffer.append(" .xfindex = ")
154 .append(Integer.toHexString(getXFIndex())).append("\n");
155 buffer.append(" .value = ").append(getValue())
156 .append("\n");
157 buffer.append("[/NUMBER]\n");
158 return buffer.toString();
162 * called by the class that is responsible for writing this sucker.
163 * Subclasses should implement this so that their data is passed back in a
164 * byte array.
166 * @return byte array containing instance data
169 public int serialize(int offset, byte [] data)
171 LittleEndian.putShort(data, 0 + offset, sid);
172 LittleEndian.putShort(data, 2 + offset, ( short ) 14);
173 //LittleEndian.putShort(data, 4 + offset, getRow());
174 LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
175 LittleEndian.putShort(data, 6 + offset, getColumn());
176 LittleEndian.putShort(data, 8 + offset, getXFIndex());
177 LittleEndian.putDouble(data, 10 + offset, getValue());
178 return getRecordSize();
181 public int getRecordSize()
183 return 18;
187 * called by constructor, should throw runtime exception in the event of a
188 * record passed with a differing ID.
190 * @param id alleged id for this record
193 protected void validateSid(short id)
195 if (id != sid)
197 throw new RecordFormatException("NOT A Number RECORD");
201 public short getSid()
203 return sid;
206 public boolean isBefore(CellValueRecordInterface i)
208 if (this.getRow() > i.getRow())
210 return false;
212 if ((this.getRow() == i.getRow())
213 && (this.getColumn() > i.getColumn()))
215 return false;
217 if ((this.getRow() == i.getRow())
218 && (this.getColumn() == i.getColumn()))
220 return false;
222 return true;
225 public boolean isAfter(CellValueRecordInterface i)
227 if (this.getRow() < i.getRow())
229 return false;
231 if ((this.getRow() == i.getRow())
232 && (this.getColumn() < i.getColumn()))
234 return false;
236 if ((this.getRow() == i.getRow())
237 && (this.getColumn() == i.getColumn()))
239 return false;
241 return true;
244 public boolean isEqual(CellValueRecordInterface i)
246 return ((this.getRow() == i.getRow())
247 && (this.getColumn() == i.getColumn()));
250 public boolean isInValueSection()
252 return true;
255 public boolean isValue()
257 return true;
260 public int compareTo(Object obj)
262 CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
264 if ((this.getRow() == loc.getRow())
265 && (this.getColumn() == loc.getColumn()))
267 return 0;
269 if (this.getRow() < loc.getRow())
271 return -1;
273 if (this.getRow() > loc.getRow())
275 return 1;
277 if (this.getColumn() < loc.getColumn())
279 return -1;
281 if (this.getColumn() > loc.getColumn())
283 return 1;
285 return -1;
288 public boolean equals(Object obj)
290 if (!(obj instanceof CellValueRecordInterface))
292 return false;
294 CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
296 if ((this.getRow() == loc.getRow())
297 && (this.getColumn() == loc.getColumn()))
299 return true;
301 return false;
304 public Object clone() {
305 NumberRecord rec = new NumberRecord();
306 rec.field_1_row = field_1_row;
307 rec.field_2_col = field_2_col;
308 rec.field_3_xf = field_3_xf;
309 rec.field_4_value = field_4_value;
310 return rec;