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.

ColumnNumberManager.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fo.flow.table;
  19. import java.util.BitSet;
  20. import java.util.List;
  21. /**
  22. * Helper class maintaining a record of occupied columns and an index to the next
  23. * non-occupied column.
  24. */
  25. public class ColumnNumberManager {
  26. private int columnNumber = 1;
  27. /**
  28. * We use the term "index" instead of "number" because, unlike column numbers, it's
  29. * 0-based.
  30. */
  31. private BitSet usedColumnIndices = new BitSet();
  32. /**
  33. * Returns the number of the column that shall receive the next parsed cell.
  34. *
  35. * @return a column number, 1-based
  36. */
  37. int getCurrentColumnNumber() {
  38. return columnNumber;
  39. }
  40. /**
  41. * Flags columns <code>start</code> to <code>end</code> as occupied,
  42. * and updates the number of the next available column.
  43. *
  44. * @param start start number, inclusive, 1-based
  45. * @param end end number, inclusive
  46. */
  47. void signalUsedColumnNumbers(int start, int end) {
  48. for (int i = start - 1; i < end; i++) {
  49. usedColumnIndices.set(i);
  50. }
  51. columnNumber = end + 1;
  52. while (usedColumnIndices.get(columnNumber - 1)) {
  53. columnNumber++;
  54. }
  55. }
  56. /**
  57. * Resets the record of occupied columns, taking into account columns already occupied
  58. * by previous spanning cells, and computes the number of the first free column.
  59. *
  60. * @param pendingSpans List&lt;PendingSpan&gt; of possible spans over the next row
  61. */
  62. void prepareForNextRow(List pendingSpans) {
  63. usedColumnIndices.clear();
  64. PendingSpan pSpan;
  65. for (int i = 0; i < pendingSpans.size(); i++) {
  66. pSpan = (PendingSpan) pendingSpans.get(i);
  67. if (pSpan != null) {
  68. pSpan.rowsLeft--;
  69. if (pSpan.rowsLeft == 0) {
  70. pendingSpans.set(i, null);
  71. } else {
  72. usedColumnIndices.set(i);
  73. }
  74. }
  75. }
  76. // Set columnNumber to the first available column
  77. columnNumber = 1;
  78. while (usedColumnIndices.get(columnNumber - 1)) {
  79. columnNumber++;
  80. }
  81. }
  82. /**
  83. * Checks whether a given column-number is already in use
  84. * for the current row.
  85. *
  86. * @param colNr the column-number to check
  87. * @return true if column-number is already occupied
  88. */
  89. public boolean isColumnNumberUsed(int colNr) {
  90. return usedColumnIndices.get(colNr - 1);
  91. }
  92. }