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.

CombinedIterable.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.xdgf.usermodel.section;
  16. import java.util.Collections;
  17. import java.util.Iterator;
  18. import java.util.Map.Entry;
  19. import java.util.NoSuchElementException;
  20. import java.util.SortedMap;
  21. /**
  22. * An iterator used to iterate over the base and master items
  23. */
  24. public class CombinedIterable<T> implements Iterable<T> {
  25. final SortedMap<Long, T> _baseItems;
  26. final SortedMap<Long, T> _masterItems;
  27. public CombinedIterable(SortedMap<Long, T> baseItems,
  28. SortedMap<Long, T> masterItems) {
  29. _baseItems = baseItems;
  30. _masterItems = masterItems;
  31. }
  32. @Override
  33. public Iterator<T> iterator() {
  34. final Iterator<Entry<Long, T>> vmasterI = (_masterItems == null)
  35. ? Collections.emptyIterator() : _masterItems.entrySet().iterator();
  36. return new Iterator<T>() {
  37. Long lastI = Long.MIN_VALUE;
  38. Entry<Long, T> currentBase;
  39. Entry<Long, T> currentMaster;
  40. // grab the iterator for both
  41. final Iterator<Entry<Long, T>> baseI = _baseItems.entrySet().iterator();
  42. final Iterator<Entry<Long, T>> masterI = vmasterI;
  43. @Override
  44. public boolean hasNext() {
  45. return currentBase != null || currentMaster != null
  46. || baseI.hasNext() || masterI.hasNext();
  47. }
  48. @Override
  49. public T next() {
  50. // TODO: This seems far more complex than it needs to be
  51. long baseIdx = Long.MAX_VALUE;
  52. long masterIdx = Long.MAX_VALUE;
  53. if (currentBase == null) {
  54. while (baseI.hasNext()) {
  55. currentBase = baseI.next();
  56. if (currentBase.getKey() > lastI) {
  57. baseIdx = currentBase.getKey();
  58. break;
  59. }
  60. }
  61. } else {
  62. baseIdx = currentBase.getKey();
  63. }
  64. if (currentMaster == null) {
  65. while (masterI.hasNext()) {
  66. currentMaster = masterI.next();
  67. if (currentMaster.getKey() > lastI) {
  68. masterIdx = currentMaster.getKey();
  69. break;
  70. }
  71. }
  72. } else {
  73. masterIdx = currentMaster.getKey();
  74. }
  75. T val;
  76. if (currentBase != null) {
  77. if (baseIdx <= masterIdx) {
  78. lastI = baseIdx;
  79. val = currentBase.getValue();
  80. // discard master if same as base
  81. if (masterIdx == baseIdx) {
  82. currentMaster = null;
  83. }
  84. currentBase = null;
  85. } else {
  86. lastI = masterIdx;
  87. val = (currentMaster != null) ? currentMaster.getValue() : null;
  88. currentMaster = null;
  89. }
  90. } else if (currentMaster != null) {
  91. lastI = currentMaster.getKey();
  92. val = currentMaster.getValue();
  93. currentMaster = null;
  94. } else {
  95. throw new NoSuchElementException();
  96. }
  97. return val;
  98. }
  99. @Override
  100. public void remove() {
  101. throw new UnsupportedOperationException();
  102. }
  103. };
  104. }
  105. }