PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/atlassian-plugins-webresource/src/main/java/com/atlassian/plugin/webresource/impl/discovery/Found.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 141 lines | 71 code | 17 blank | 53 comment | 0 complexity | 4d04568c3b588df5ede83d90db52838f MD5 | raw file
  1. package com.atlassian.plugin.webresource.impl.discovery;
  2. import com.google.common.collect.ImmutableList;
  3. import com.google.common.collect.ImmutableSet;
  4. import javax.annotation.Nonnull;
  5. import java.util.List;
  6. import java.util.Objects;
  7. import java.util.Set;
  8. import java.util.stream.Collectors;
  9. import static java.util.Collections.emptyList;
  10. import static java.util.Collections.emptySet;
  11. /**
  12. * Represents the result of walking the dependency graph via {@link BundleWalker}, where every
  13. * item is in <a href="https://mathworld.wolfram.com/TopologicalSort.html">is in topological sort order</a>.
  14. * The items captured will vary based on how the graph walk was configured in
  15. * {@link BundleFinder} or {@link BundleWalker}.
  16. */
  17. public class Found {
  18. public static final Found EMPTY = new Found(emptyList(), emptyList(), emptySet());
  19. private final List<Item> items;
  20. private final List<String> reducedInclusions;
  21. private final Set<String> reducedExclusions;
  22. @Deprecated
  23. public Found(final List<Item> items) {
  24. this(items, emptyList(), emptySet());
  25. }
  26. public Found(final List<Item> items, final List<String> reducedInclusions, final Set<String> reducedExclusions) {
  27. this.items = items;
  28. this.reducedInclusions = ImmutableList.copyOf(reducedInclusions);
  29. this.reducedExclusions = ImmutableSet.copyOf(reducedExclusions);
  30. }
  31. /**
  32. * @return a list of {@link com.atlassian.plugin.webresource.models.Requestable} keys
  33. * whose content should be served to the client.
  34. */
  35. public List<String> getFound() {
  36. return items.stream()
  37. .filter(item -> State.INCLUDED.equals(item.getState()))
  38. .map(Item::getKey)
  39. .distinct()
  40. .collect(Collectors.toList());
  41. }
  42. /**
  43. * @return a list of {@link com.atlassian.plugin.webresource.models.Requestable} keys that failed a deep filter
  44. * test while traversing the graph (such as a {@link com.atlassian.plugin.webresource.condition.UrlReadingCondition}
  45. * returning false) and should not be served to the client.
  46. *
  47. * Note that these are the exact items that failed a predicate test. Any child items would be ignored by the
  48. * graph walk. If you need a complete list that includes subtrees of skipped items, configure
  49. * {@link BundleFinder#onDeepFilterFail(PredicateFailStrategy)} to continue when a deep filter fails.
  50. */
  51. public List<String> getSkipped() {
  52. return items.stream()
  53. .filter(item -> State.SKIPPED.equals(item.getState()))
  54. .map(Item::getKey)
  55. .distinct()
  56. .collect(Collectors.toList());
  57. }
  58. /**
  59. * @return all {@link com.atlassian.plugin.webresource.models.Requestable} keys
  60. * discovered while performing this graph traversal. The list will include all skipped items,
  61. * and may include ignored subtrees depending on how {@link BundleFinder#onDeepFilterFail(PredicateFailStrategy)}
  62. * was configured.
  63. */
  64. public List<String> getAll() {
  65. return items.stream()
  66. .map(Item::getKey)
  67. .distinct()
  68. .collect(Collectors.toList());
  69. }
  70. /**
  71. * @return the minimal set of graph entry points required to discover all the {@link #getFound()} items
  72. * after omitting subtrees discoverable via {@link #getReducedExclusions()}.
  73. * Can be used to minimise encoding cost when expressing the graph query and avoid redundant graph walks.
  74. */
  75. public List<String> getReducedInclusions() {
  76. return reducedInclusions;
  77. }
  78. /**
  79. * @return the minimal set of graph entry points required to avoid serving content the client should not be given.
  80. * Can be used to minimise encoding cost when expressing the graph query and avoid redundant graph walks.
  81. */
  82. public Set<String> getReducedExclusions() {
  83. return reducedExclusions;
  84. }
  85. enum State {
  86. /**
  87. * Items with this state contribute to response contents, URLs, caches, etc.
  88. */
  89. INCLUDED,
  90. /**
  91. * Used to mark items that fail filter predicates.
  92. * Items with this state DO NOT contribute to response contents but DO contribute to URLs, caches, etc.
  93. * Specifically, they will be used to determine what UrlReadingCondition should affect the URL when
  94. * batches and sub-batches are calculated.
  95. */
  96. SKIPPED,
  97. /**
  98. * Used to mark items that would have formed a part of this dependency graph, but were
  99. * omitted due to it or a parent being explicitly excluded (i.e., it has been served
  100. * in a previous request, or its parent's condition failed, etc).
  101. * Items with this state DO NOT contribute to response contents, caches, etc. but DO contribute to URLs.
  102. */
  103. IGNORED
  104. }
  105. /**
  106. * Tuple of a {@link com.atlassian.plugin.webresource.models.Requestable} key and its contribution {@link State}
  107. * to the response for a client request.
  108. */
  109. public static class Item {
  110. private final String key;
  111. private final State state;
  112. Item(final @Nonnull String key, @Nonnull final State state) {
  113. Objects.requireNonNull(key);
  114. this.key = key;
  115. this.state = state;
  116. }
  117. public String getKey() {
  118. return key;
  119. }
  120. private State getState() {
  121. return state;
  122. }
  123. }
  124. }