PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/components/forks/poi/src/loci/poi/hssf/record/NumberRecord.java

http://github.com/openmicroscopy/bioformats
Java | 334 lines | 196 code | 41 blank | 97 comment | 34 complexity | 1a93e7361ce626f1e004433c3a10b935 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. /*
  38. * NumberRecord.java
  39. *
  40. * Created on October 1, 2001, 8:01 PM
  41. */
  42. package loci.poi.hssf.record;
  43. import loci.poi.util.LittleEndian;
  44. import loci.poi.hssf.record.Record;
  45. /**
  46. * Contains a numeric cell value. <P>
  47. * REFERENCE: PG 334 Microsoft Excel 97 Developer's Kit (ISBN: 1-57231-498-2)<P>
  48. * @author Andrew C. Oliver (acoliver at apache dot org)
  49. * @author Jason Height (jheight at chariot dot net dot au)
  50. * @version 2.0-pre
  51. */
  52. public class NumberRecord
  53. extends Record
  54. implements CellValueRecordInterface, Comparable
  55. {
  56. public static final short sid = 0x203;
  57. //private short field_1_row;
  58. private int field_1_row;
  59. private short field_2_col;
  60. private short field_3_xf;
  61. private double field_4_value;
  62. /** Creates new NumberRecord */
  63. public NumberRecord()
  64. {
  65. }
  66. /**
  67. * Constructs a Number record and sets its fields appropriately.
  68. *
  69. * @param in the RecordInputstream to read the record from
  70. */
  71. public NumberRecord(RecordInputStream in)
  72. {
  73. super(in);
  74. }
  75. /**
  76. * @param in the RecordInputstream to read the record from
  77. */
  78. protected void fillFields(RecordInputStream in)
  79. {
  80. //field_1_row = LittleEndian.getShort(data, 0 + offset);
  81. field_1_row = in.readUShort();
  82. field_2_col = in.readShort();
  83. field_3_xf = in.readShort();
  84. field_4_value = in.readDouble();
  85. }
  86. //public void setRow(short row)
  87. public void setRow(int row)
  88. {
  89. field_1_row = row;
  90. }
  91. public void setColumn(short col)
  92. {
  93. field_2_col = col;
  94. }
  95. /**
  96. * set the index to the ExtendedFormat
  97. * @see loci.poi.hssf.record.ExtendedFormatRecord
  98. * @param xf index to the XF record
  99. */
  100. public void setXFIndex(short xf)
  101. {
  102. field_3_xf = xf;
  103. }
  104. /**
  105. * set the value for the cell
  106. *
  107. * @param value double representing the value
  108. */
  109. public void setValue(double value)
  110. {
  111. field_4_value = value;
  112. }
  113. //public short getRow()
  114. public int getRow()
  115. {
  116. return field_1_row;
  117. }
  118. public short getColumn()
  119. {
  120. return field_2_col;
  121. }
  122. /**
  123. * get the index to the ExtendedFormat
  124. * @see loci.poi.hssf.record.ExtendedFormatRecord
  125. * @return index to the XF record
  126. */
  127. public short getXFIndex()
  128. {
  129. return field_3_xf;
  130. }
  131. /**
  132. * get the value for the cell
  133. *
  134. * @return double representing the value
  135. */
  136. public double getValue()
  137. {
  138. return field_4_value;
  139. }
  140. public String toString()
  141. {
  142. StringBuffer buffer = new StringBuffer();
  143. buffer.append("[NUMBER]\n");
  144. buffer.append(" .row = ")
  145. .append(Integer.toHexString(getRow())).append("\n");
  146. buffer.append(" .col = ")
  147. .append(Integer.toHexString(getColumn())).append("\n");
  148. buffer.append(" .xfindex = ")
  149. .append(Integer.toHexString(getXFIndex())).append("\n");
  150. buffer.append(" .value = ").append(getValue())
  151. .append("\n");
  152. buffer.append("[/NUMBER]\n");
  153. return buffer.toString();
  154. }
  155. /**
  156. * called by the class that is responsible for writing this sucker.
  157. * Subclasses should implement this so that their data is passed back in a
  158. * byte array.
  159. *
  160. * @return byte array containing instance data
  161. */
  162. public int serialize(int offset, byte [] data)
  163. {
  164. LittleEndian.putShort(data, 0 + offset, sid);
  165. LittleEndian.putShort(data, 2 + offset, ( short ) 14);
  166. //LittleEndian.putShort(data, 4 + offset, getRow());
  167. LittleEndian.putShort(data, 4 + offset, ( short ) getRow());
  168. LittleEndian.putShort(data, 6 + offset, getColumn());
  169. LittleEndian.putShort(data, 8 + offset, getXFIndex());
  170. LittleEndian.putDouble(data, 10 + offset, getValue());
  171. return getRecordSize();
  172. }
  173. public int getRecordSize()
  174. {
  175. return 18;
  176. }
  177. /**
  178. * called by constructor, should throw runtime exception in the event of a
  179. * record passed with a differing ID.
  180. *
  181. * @param id alleged id for this record
  182. */
  183. protected void validateSid(short id)
  184. {
  185. if (id != sid)
  186. {
  187. throw new RecordFormatException("NOT A Number RECORD");
  188. }
  189. }
  190. public short getSid()
  191. {
  192. return sid;
  193. }
  194. public boolean isBefore(CellValueRecordInterface i)
  195. {
  196. if (this.getRow() > i.getRow())
  197. {
  198. return false;
  199. }
  200. if ((this.getRow() == i.getRow())
  201. && (this.getColumn() > i.getColumn()))
  202. {
  203. return false;
  204. }
  205. if ((this.getRow() == i.getRow())
  206. && (this.getColumn() == i.getColumn()))
  207. {
  208. return false;
  209. }
  210. return true;
  211. }
  212. public boolean isAfter(CellValueRecordInterface i)
  213. {
  214. if (this.getRow() < i.getRow())
  215. {
  216. return false;
  217. }
  218. if ((this.getRow() == i.getRow())
  219. && (this.getColumn() < i.getColumn()))
  220. {
  221. return false;
  222. }
  223. if ((this.getRow() == i.getRow())
  224. && (this.getColumn() == i.getColumn()))
  225. {
  226. return false;
  227. }
  228. return true;
  229. }
  230. public boolean isEqual(CellValueRecordInterface i)
  231. {
  232. return ((this.getRow() == i.getRow())
  233. && (this.getColumn() == i.getColumn()));
  234. }
  235. public boolean isInValueSection()
  236. {
  237. return true;
  238. }
  239. public boolean isValue()
  240. {
  241. return true;
  242. }
  243. public int compareTo(Object obj)
  244. {
  245. CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
  246. if ((this.getRow() == loc.getRow())
  247. && (this.getColumn() == loc.getColumn()))
  248. {
  249. return 0;
  250. }
  251. if (this.getRow() < loc.getRow())
  252. {
  253. return -1;
  254. }
  255. if (this.getRow() > loc.getRow())
  256. {
  257. return 1;
  258. }
  259. if (this.getColumn() < loc.getColumn())
  260. {
  261. return -1;
  262. }
  263. if (this.getColumn() > loc.getColumn())
  264. {
  265. return 1;
  266. }
  267. return -1;
  268. }
  269. public boolean equals(Object obj)
  270. {
  271. if (!(obj instanceof CellValueRecordInterface))
  272. {
  273. return false;
  274. }
  275. CellValueRecordInterface loc = ( CellValueRecordInterface ) obj;
  276. if ((this.getRow() == loc.getRow())
  277. && (this.getColumn() == loc.getColumn()))
  278. {
  279. return true;
  280. }
  281. return false;
  282. }
  283. public Object clone() {
  284. NumberRecord rec = new NumberRecord();
  285. rec.field_1_row = field_1_row;
  286. rec.field_2_col = field_2_col;
  287. rec.field_3_xf = field_3_xf;
  288. rec.field_4_value = field_4_value;
  289. return rec;
  290. }
  291. }