You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RecordStream.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.model;
  16. import java.util.List;
  17. import org.apache.poi.hssf.record.Record;
  18. /**
  19. * Simplifies iteration over a sequence of <tt>Record</tt> objects.
  20. */
  21. public final class RecordStream {
  22. private final List<org.apache.poi.hssf.record.Record> _list;
  23. private int _nextIndex;
  24. private int _countRead;
  25. private final int _endIx;
  26. /**
  27. * Creates a RecordStream bounded by startIndex and endIndex
  28. *
  29. * @param inputList the list to iterate over
  30. * @param startIndex the start index within the list
  31. * @param endIx the end index within the list, which is the index of the end element + 1
  32. */
  33. public RecordStream(List<org.apache.poi.hssf.record.Record> inputList, int startIndex, int endIx) {
  34. _list = inputList;
  35. _nextIndex = startIndex;
  36. _endIx = endIx;
  37. _countRead = 0;
  38. }
  39. public RecordStream(List<org.apache.poi.hssf.record.Record> records, int startIx) {
  40. this(records, startIx, records.size());
  41. }
  42. public boolean hasNext() {
  43. return _nextIndex < _endIx;
  44. }
  45. public Record getNext() {
  46. if(!hasNext()) {
  47. throw new RuntimeException("Attempt to read past end of record stream");
  48. }
  49. _countRead ++;
  50. return _list.get(_nextIndex++);
  51. }
  52. /**
  53. * @return the {@link Class} of the next Record. <code>null</code> if this stream is exhausted.
  54. */
  55. public Class<? extends Record> peekNextClass() {
  56. if(!hasNext()) {
  57. return null;
  58. }
  59. return _list.get(_nextIndex).getClass();
  60. }
  61. /**
  62. * @return the next Record. <code>null</code> if this stream is exhausted.
  63. */
  64. public Record peekNextRecord() {
  65. return (hasNext()) ? _list.get(_nextIndex) : null;
  66. }
  67. /**
  68. * @return -1 if at end of records
  69. */
  70. public int peekNextSid() {
  71. if(!hasNext()) {
  72. return -1;
  73. }
  74. return _list.get(_nextIndex).getSid();
  75. }
  76. public int getCountRead() {
  77. return _countRead;
  78. }
  79. }