PageRenderTime 192ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/components/forks/poi/src/loci/poi/hssf/record/aggregates/ValueRecordsAggregate.java

http://github.com/openmicroscopy/bioformats
Java | 367 lines | 240 code | 46 blank | 81 comment | 58 complexity | f3d6bf42f5d9482d498467f2cea55ab5 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. package loci.poi.hssf.record.aggregates;
  38. import loci.poi.hssf.record.*;
  39. import java.util.Iterator;
  40. import java.util.List;
  41. /**
  42. *
  43. * Aggregate value records together. Things are easier to handle that way.
  44. *
  45. * @author andy
  46. * @author Glen Stampoultzis (glens at apache.org)
  47. * @author Jason Height (jheight at chariot dot net dot au)
  48. */
  49. public class ValueRecordsAggregate
  50. extends Record
  51. {
  52. public final static short sid = -1000;
  53. int firstcell = -1;
  54. int lastcell = -1;
  55. CellValueRecordInterface[][] records;
  56. /** Creates a new instance of ValueRecordsAggregate */
  57. public ValueRecordsAggregate()
  58. {
  59. records = new CellValueRecordInterface[30][]; // We start with 30 Rows.
  60. }
  61. public void insertCell(CellValueRecordInterface cell) {
  62. short column = cell.getColumn();
  63. int row = cell.getRow();
  64. if (row >= records.length) {
  65. CellValueRecordInterface[][] oldRecords = records;
  66. int newSize = oldRecords.length * 2;
  67. if(newSize<row+1) newSize=row+1;
  68. records = new CellValueRecordInterface[newSize][];
  69. System.arraycopy(oldRecords, 0, records, 0, oldRecords.length);
  70. }
  71. CellValueRecordInterface[] rowCells = records[row];
  72. if (rowCells == null) {
  73. int newSize = column + 1;
  74. if(newSize<10) newSize=10;
  75. rowCells = new CellValueRecordInterface[newSize];
  76. records[row] = rowCells;
  77. }
  78. if (column >= rowCells.length) {
  79. CellValueRecordInterface[] oldRowCells = rowCells;
  80. int newSize = oldRowCells.length * 2;
  81. if(newSize<column+1) newSize=column+1;
  82. // if(newSize>257) newSize=257; // activate?
  83. rowCells = new CellValueRecordInterface[newSize];
  84. System.arraycopy(oldRowCells, 0, rowCells, 0, oldRowCells.length);
  85. records[row] = rowCells;
  86. }
  87. rowCells[column] = cell;
  88. if ((column < firstcell) || (firstcell == -1)) {
  89. firstcell = column;
  90. }
  91. if ((column > lastcell) || (lastcell == -1)) {
  92. lastcell = column;
  93. }
  94. }
  95. public void removeCell(CellValueRecordInterface cell)
  96. {
  97. if (cell != null) {
  98. short column = cell.getColumn();
  99. int row = cell.getRow();
  100. if(row>=records.length) return;
  101. CellValueRecordInterface[] rowCells=records[row];
  102. if(rowCells==null) return;
  103. if(column>=rowCells.length) return;
  104. rowCells[column]=null;
  105. }
  106. }
  107. public int getPhysicalNumberOfCells()
  108. {
  109. int count=0;
  110. for(int r=0;r<records.length;r++) {
  111. CellValueRecordInterface[] rowCells=records[r];
  112. if (rowCells != null)
  113. for(short c=0;c<rowCells.length;c++) {
  114. if(rowCells[c]!=null) count++;
  115. }
  116. }
  117. return count;
  118. }
  119. public int getFirstCellNum()
  120. {
  121. return firstcell;
  122. }
  123. public int getLastCellNum()
  124. {
  125. return lastcell;
  126. }
  127. public int construct(int offset, List records)
  128. {
  129. int k = 0;
  130. FormulaRecordAggregate lastFormulaAggregate = null;
  131. List sharedFormulas = new java.util.ArrayList();
  132. for (k = offset; k < records.size(); k++)
  133. {
  134. Record rec = ( Record ) records.get(k);
  135. if (rec instanceof StringRecord == false && !rec.isInValueSection() && !(rec instanceof UnknownRecord))
  136. {
  137. break;
  138. } else if (rec instanceof SharedFormulaRecord) {
  139. sharedFormulas.add(rec);
  140. } else if (rec instanceof FormulaRecord)
  141. {
  142. FormulaRecord formula = (FormulaRecord)rec;
  143. if (formula.isSharedFormula()) {
  144. Record nextRecord = (Record) records.get(k + 1);
  145. if (nextRecord instanceof SharedFormulaRecord) {
  146. sharedFormulas.add(nextRecord);
  147. k++;
  148. }
  149. //traverse the list of shared formulas in reverse order, and try to find the correct one
  150. //for us
  151. boolean found = false;
  152. for (int i=sharedFormulas.size()-1;i>=0;i--) {
  153. SharedFormulaRecord shrd = (SharedFormulaRecord)sharedFormulas.get(i);
  154. if (shrd.isFormulaInShared(formula)) {
  155. shrd.convertSharedFormulaRecord(formula);
  156. found = true;
  157. break;
  158. }
  159. }
  160. if (!found) {
  161. //Sometimes the shared formula flag "seems" to be errornously set,
  162. //cant really do much about that.
  163. //throw new RecordFormatException("Could not find appropriate shared formula");
  164. }
  165. }
  166. lastFormulaAggregate = new FormulaRecordAggregate((FormulaRecord)rec, null);
  167. insertCell( lastFormulaAggregate );
  168. }
  169. else if (rec instanceof StringRecord)
  170. {
  171. lastFormulaAggregate.setStringRecord((StringRecord)rec);
  172. }
  173. else if (rec.isValue())
  174. {
  175. insertCell(( CellValueRecordInterface ) rec);
  176. }
  177. }
  178. return k;
  179. }
  180. /**
  181. * called by the class that is responsible for writing this sucker.
  182. * Subclasses should implement this so that their data is passed back in a
  183. * byte array.
  184. *
  185. * @param offset to begin writing at
  186. * @param data byte array containing instance data
  187. * @return number of bytes written
  188. */
  189. public int serialize(int offset, byte [] data)
  190. {
  191. throw new RuntimeException("This method shouldnt be called. ValueRecordsAggregate.serializeCellRow() should be called from RowRecordsAggregate.");
  192. }
  193. /** Tallies a count of the size of the cell records
  194. * that are attached to the rows in the range specified.
  195. */
  196. public int getRowCellBlockSize(int startRow, int endRow) {
  197. MyIterator itr = new MyIterator(startRow, endRow);
  198. int size = 0;
  199. while (itr.hasNext()) {
  200. CellValueRecordInterface cell = (CellValueRecordInterface)itr.next();
  201. int row = cell.getRow();
  202. if (row > endRow)
  203. break;
  204. if ((row >=startRow) && (row <= endRow))
  205. size += ((Record)cell).getRecordSize();
  206. }
  207. return size;
  208. }
  209. /** Returns true if the row has cells attached to it */
  210. public boolean rowHasCells(int row) {
  211. if (row > records.length-1) //previously this said row > records.length which means if
  212. return false; // if records.length == 60 and I pass "60" here I get array out of bounds
  213. CellValueRecordInterface[] rowCells=records[row]; //because a 60 length array has the last index = 59
  214. if(rowCells==null) return false;
  215. for(int col=0;col<rowCells.length;col++) {
  216. if(rowCells[col]!=null) return true;
  217. }
  218. return false;
  219. }
  220. /** Serializes the cells that are allocated to a certain row range*/
  221. public int serializeCellRow(final int row, int offset, byte [] data)
  222. {
  223. MyIterator itr = new MyIterator(row, row);
  224. int pos = offset;
  225. while (itr.hasNext())
  226. {
  227. CellValueRecordInterface cell = (CellValueRecordInterface)itr.next();
  228. if (cell.getRow() != row)
  229. break;
  230. pos += (( Record ) cell).serialize(pos, data);
  231. }
  232. return pos - offset;
  233. }
  234. /**
  235. * You never fill an aggregate
  236. */
  237. protected void fillFields(RecordInputStream in)
  238. {
  239. }
  240. /**
  241. * called by constructor, should throw runtime exception in the event of a
  242. * record passed with a differing ID.
  243. *
  244. * @param id alleged id for this record
  245. */
  246. protected void validateSid(short id)
  247. {
  248. }
  249. /**
  250. * return the non static version of the id for this record.
  251. */
  252. public short getSid()
  253. {
  254. return sid;
  255. }
  256. public int getRecordSize() {
  257. int size = 0;
  258. Iterator irecs = this.getIterator();
  259. while (irecs.hasNext()) {
  260. size += (( Record ) irecs.next()).getRecordSize();
  261. }
  262. return size;
  263. }
  264. public Iterator getIterator()
  265. {
  266. return new MyIterator();
  267. }
  268. /** Performs a deep clone of the record*/
  269. public Object clone() {
  270. ValueRecordsAggregate rec = new ValueRecordsAggregate();
  271. for (Iterator valIter = getIterator(); valIter.hasNext();) {
  272. CellValueRecordInterface val = (CellValueRecordInterface)((CellValueRecordInterface)valIter.next()).clone();
  273. rec.insertCell(val);
  274. }
  275. return rec;
  276. }
  277. public class MyIterator implements Iterator {
  278. short nextColumn=-1;
  279. int nextRow,lastRow;
  280. public MyIterator()
  281. {
  282. this.nextRow=0;
  283. this.lastRow=records.length-1;
  284. findNext();
  285. }
  286. public MyIterator(int firstRow,int lastRow)
  287. {
  288. this.nextRow=firstRow;
  289. this.lastRow=lastRow;
  290. findNext();
  291. }
  292. public boolean hasNext() {
  293. return nextRow<=lastRow;
  294. }
  295. public Object next() {
  296. Object o=records[nextRow][nextColumn];
  297. findNext();
  298. return o;
  299. }
  300. public void remove() {
  301. throw new UnsupportedOperationException("gibt's noch nicht");
  302. }
  303. private void findNext() {
  304. nextColumn++;
  305. for(;nextRow<=lastRow;nextRow++) {
  306. //previously this threw array out of bounds...
  307. CellValueRecordInterface[] rowCells=(nextRow < records.length) ? records[nextRow] : null;
  308. if(rowCells==null) { // This row is empty
  309. nextColumn=0;
  310. continue;
  311. }
  312. for(;nextColumn<rowCells.length;nextColumn++) {
  313. if(rowCells[nextColumn]!=null) return;
  314. }
  315. nextColumn=0;
  316. }
  317. }
  318. }
  319. }