001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: /*
019: * NumberRecord.java
020: *
021: * Created on October 1, 2001, 8:01 PM
022: */
023: package org.apache.poi.hssf.record;
024:
025: import org.apache.poi.util.LittleEndian;
026: import org.apache.poi.hssf.record.Record;
027:
028: /**
029: * Contains a numeric cell value. <P>
030: * REFERENCE: PG 334 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
031: * @author Andrew C. Oliver (acoliver at apache dot org)
032: * @author Jason Height (jheight at chariot dot net dot au)
033: * @version 2.0-pre
034: */
035:
036: public class NumberRecord extends Record implements
037: CellValueRecordInterface, Comparable {
038: public static final short sid = 0x203;
039: //private short field_1_row;
040: private int field_1_row;
041: private short field_2_col;
042: private short field_3_xf;
043: private double field_4_value;
044:
045: /** Creates new NumberRecord */
046: public NumberRecord() {
047: }
048:
049: /**
050: * Constructs a Number record and sets its fields appropriately.
051: *
052: * @param in the RecordInputstream to read the record from
053: */
054:
055: public NumberRecord(RecordInputStream in) {
056: super (in);
057: }
058:
059: /**
060: * @param in the RecordInputstream to read the record from
061: */
062:
063: protected void fillFields(RecordInputStream in) {
064: //field_1_row = LittleEndian.getShort(data, 0 + offset);
065: field_1_row = in.readUShort();
066: field_2_col = in.readShort();
067: field_3_xf = in.readShort();
068: field_4_value = in.readDouble();
069: }
070:
071: //public void setRow(short row)
072: public void setRow(int row) {
073: field_1_row = row;
074: }
075:
076: public void setColumn(short col) {
077: field_2_col = col;
078: }
079:
080: /**
081: * set the index to the ExtendedFormat
082: * @see org.apache.poi.hssf.record.ExtendedFormatRecord
083: * @param xf index to the XF record
084: */
085:
086: public void setXFIndex(short xf) {
087: field_3_xf = xf;
088: }
089:
090: /**
091: * set the value for the cell
092: *
093: * @param value double representing the value
094: */
095:
096: public void setValue(double value) {
097: field_4_value = value;
098: }
099:
100: //public short getRow()
101: public int getRow() {
102: return field_1_row;
103: }
104:
105: public short getColumn() {
106: return field_2_col;
107: }
108:
109: /**
110: * get the index to the ExtendedFormat
111: * @see org.apache.poi.hssf.record.ExtendedFormatRecord
112: * @return index to the XF record
113: */
114:
115: public short getXFIndex() {
116: return field_3_xf;
117: }
118:
119: /**
120: * get the value for the cell
121: *
122: * @return double representing the value
123: */
124:
125: public double getValue() {
126: return field_4_value;
127: }
128:
129: public String toString() {
130: StringBuffer buffer = new StringBuffer();
131:
132: buffer.append("[NUMBER]\n");
133: buffer.append(" .row = ").append(
134: Integer.toHexString(getRow())).append("\n");
135: buffer.append(" .col = ").append(
136: Integer.toHexString(getColumn())).append("\n");
137: buffer.append(" .xfindex = ").append(
138: Integer.toHexString(getXFIndex())).append("\n");
139: buffer.append(" .value = ").append(getValue())
140: .append("\n");
141: buffer.append("[/NUMBER]\n");
142: return buffer.toString();
143: }
144:
145: /**
146: * called by the class that is responsible for writing this sucker.
147: * Subclasses should implement this so that their data is passed back in a
148: * byte array.
149: *
150: * @return byte array containing instance data
151: */
152:
153: public int serialize(int offset, byte[] data) {
154: LittleEndian.putShort(data, 0 + offset, sid);
155: LittleEndian.putShort(data, 2 + offset, (short) 14);
156: //LittleEndian.putShort(data, 4 + offset, getRow());
157: LittleEndian.putShort(data, 4 + offset, (short) getRow());
158: LittleEndian.putShort(data, 6 + offset, getColumn());
159: LittleEndian.putShort(data, 8 + offset, getXFIndex());
160: LittleEndian.putDouble(data, 10 + offset, getValue());
161: return getRecordSize();
162: }
163:
164: public int getRecordSize() {
165: return 18;
166: }
167:
168: /**
169: * called by constructor, should throw runtime exception in the event of a
170: * record passed with a differing ID.
171: *
172: * @param id alleged id for this record
173: */
174:
175: protected void validateSid(short id) {
176: if (id != sid) {
177: throw new RecordFormatException("NOT A Number RECORD");
178: }
179: }
180:
181: public short getSid() {
182: return sid;
183: }
184:
185: public boolean isBefore(CellValueRecordInterface i) {
186: if (this .getRow() > i.getRow()) {
187: return false;
188: }
189: if ((this .getRow() == i.getRow())
190: && (this .getColumn() > i.getColumn())) {
191: return false;
192: }
193: if ((this .getRow() == i.getRow())
194: && (this .getColumn() == i.getColumn())) {
195: return false;
196: }
197: return true;
198: }
199:
200: public boolean isAfter(CellValueRecordInterface i) {
201: if (this .getRow() < i.getRow()) {
202: return false;
203: }
204: if ((this .getRow() == i.getRow())
205: && (this .getColumn() < i.getColumn())) {
206: return false;
207: }
208: if ((this .getRow() == i.getRow())
209: && (this .getColumn() == i.getColumn())) {
210: return false;
211: }
212: return true;
213: }
214:
215: public boolean isEqual(CellValueRecordInterface i) {
216: return ((this .getRow() == i.getRow()) && (this .getColumn() == i
217: .getColumn()));
218: }
219:
220: public boolean isInValueSection() {
221: return true;
222: }
223:
224: public boolean isValue() {
225: return true;
226: }
227:
228: public int compareTo(Object obj) {
229: CellValueRecordInterface loc = (CellValueRecordInterface) obj;
230:
231: if ((this .getRow() == loc.getRow())
232: && (this .getColumn() == loc.getColumn())) {
233: return 0;
234: }
235: if (this .getRow() < loc.getRow()) {
236: return -1;
237: }
238: if (this .getRow() > loc.getRow()) {
239: return 1;
240: }
241: if (this .getColumn() < loc.getColumn()) {
242: return -1;
243: }
244: if (this .getColumn() > loc.getColumn()) {
245: return 1;
246: }
247: return -1;
248: }
249:
250: public boolean equals(Object obj) {
251: if (!(obj instanceof CellValueRecordInterface)) {
252: return false;
253: }
254: CellValueRecordInterface loc = (CellValueRecordInterface) obj;
255:
256: if ((this .getRow() == loc.getRow())
257: && (this .getColumn() == loc.getColumn())) {
258: return true;
259: }
260: return false;
261: }
262:
263: public Object clone() {
264: NumberRecord rec = new NumberRecord();
265: rec.field_1_row = field_1_row;
266: rec.field_2_col = field_2_col;
267: rec.field_3_xf = field_3_xf;
268: rec.field_4_value = field_4_value;
269: return rec;
270: }
271: }
|