PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/com/atlassian/jira/plugins/bitbucket/spi/bitbucket/impl/BitbucketChangesetIterator.java

https://bitbucket.org/atlassian/jira-bitbucket-connector/
Java | 82 lines | 61 code | 12 blank | 9 comment | 7 complexity | aa303b34db2ce08243c24b21f745ad17 MD5 | raw file
  1. package com.atlassian.jira.plugins.bitbucket.spi.bitbucket.impl;
  2. import com.atlassian.jira.plugins.bitbucket.api.Changeset;
  3. import com.atlassian.jira.plugins.bitbucket.api.SourceControlRepository;
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.NoSuchElementException;
  8. /**
  9. * An iterator that will load pages of changesets from the remote repository in pages transparently.
  10. */
  11. public class BitbucketChangesetIterator implements Iterator<Changeset>
  12. {
  13. public static final int PAGE_SIZE = 15;
  14. private Iterator<Changeset> currentPage = null;
  15. private Changeset followingChangset = null; // next changeset after current page
  16. private final SourceControlRepository repository;
  17. private final BitbucketCommunicator bitbucketCommunicator;
  18. public BitbucketChangesetIterator(BitbucketCommunicator bitbucketCommunicator, SourceControlRepository repository)
  19. {
  20. this.bitbucketCommunicator = bitbucketCommunicator;
  21. this.repository = repository;
  22. }
  23. public boolean hasNext()
  24. {
  25. boolean pageHasMoreChangesets = getCurrentPage().hasNext();
  26. if (!pageHasMoreChangesets && followingChangset != null)
  27. {
  28. currentPage = readPage(followingChangset.getNode());
  29. pageHasMoreChangesets = getCurrentPage().hasNext();
  30. }
  31. return pageHasMoreChangesets;
  32. }
  33. public Changeset next()
  34. {
  35. // we have to call hasNext() here as that will retrieve additional
  36. // changesets from bitbucket if required
  37. if (!hasNext())
  38. {
  39. throw new NoSuchElementException();
  40. }
  41. return getCurrentPage().next();
  42. }
  43. public void remove()
  44. {
  45. throw new UnsupportedOperationException();
  46. }
  47. private Iterator<Changeset> getCurrentPage()
  48. {
  49. if (currentPage == null)
  50. {
  51. currentPage = readPage(null);
  52. }
  53. return currentPage;
  54. }
  55. private Iterator<Changeset> readPage(String startNode)
  56. {
  57. // read PAGE_SIZE + 1 changesets. Last changeset will be used as starting node
  58. // for next page (last changeset is actually returned as first in the list)
  59. List<Changeset> changesets = bitbucketCommunicator.getChangesets(repository, startNode, PAGE_SIZE + 1);
  60. followingChangset = null;
  61. if (changesets.size() > PAGE_SIZE)
  62. {
  63. followingChangset = changesets.remove(0);
  64. }
  65. // get the changesets in the correct order (TODO this is probably not required,
  66. // we sort the changesets before displaying anyway)
  67. Collections.reverse(changesets);
  68. return changesets.iterator();
  69. }
  70. }