PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 138 lines | 93 code | 17 blank | 28 comment | 0 complexity | 78ded140c0aaafe6855fa79d7d0fe33b MD5 | raw file
  1. package com.atlassian.plugin.webresource.impl.discovery;
  2. import com.atlassian.plugin.webresource.impl.helpers.BaseHelpers;
  3. import com.atlassian.plugin.webresource.impl.snapshot.Bundle;
  4. import com.atlassian.plugin.webresource.impl.snapshot.Snapshot;
  5. import com.google.common.base.Predicate;
  6. import com.google.common.base.Predicates;
  7. import javax.annotation.Nonnull;
  8. import javax.annotation.Nullable;
  9. import java.util.ArrayList;
  10. import java.util.Collection;
  11. import java.util.HashSet;
  12. import java.util.LinkedHashSet;
  13. import java.util.List;
  14. import java.util.Set;
  15. import static com.atlassian.plugin.webresource.impl.support.Support.efficientAndPredicate;
  16. import static com.google.common.base.Predicates.alwaysFalse;
  17. import static com.google.common.base.Predicates.alwaysTrue;
  18. import static java.util.Optional.ofNullable;
  19. /**
  20. * Query builder for retrieving ordered lists of web-resource keys based on what the user is requesting
  21. * and what has previously been requested.
  22. */
  23. public class BundleFinder {
  24. private final Snapshot snapshot;
  25. /**
  26. * Unique keys to include in the query.
  27. * The order they are added may be important to the order that results are returned in.
  28. */
  29. private final LinkedHashSet<String> included;
  30. private final List<Predicate<Bundle>> deepFilters;
  31. private PredicateFailStrategy deepFilterFailStrategy = PredicateFailStrategy.STOP;
  32. /**
  33. * Unique keys to ignore if found during the query.
  34. */
  35. private final Set<String> excluded;
  36. private final List<Predicate<Bundle>> shallowFilters;
  37. private Predicate<Bundle> conditionsForExcluded;
  38. private boolean deep;
  39. private boolean resolveExcluded;
  40. public BundleFinder(final Snapshot snapshot) {
  41. deep = true;
  42. deepFilters = new ArrayList<>();
  43. excluded = new HashSet<>();
  44. included = new LinkedHashSet<>();
  45. resolveExcluded = true;
  46. this.snapshot = snapshot;
  47. shallowFilters = new ArrayList<>();
  48. }
  49. public BundleFinder included(final Collection<String> keys) {
  50. included.clear();
  51. included.addAll(keys);
  52. return this;
  53. }
  54. public BundleFinder included(final String key) {
  55. included.clear();
  56. included.add(key);
  57. return this;
  58. }
  59. public BundleFinder excluded(@Nonnull final Collection<String> keys, @Nullable final Predicate<Bundle> conditionsForExcluded) {
  60. excluded.clear();
  61. excluded.addAll(keys);
  62. this.conditionsForExcluded = ofNullable(conditionsForExcluded).orElseGet(Predicates::alwaysTrue);
  63. resolveExcluded = true;
  64. return this;
  65. }
  66. public BundleFinder excludedResolved(final Collection<String> keys) {
  67. excluded.clear();
  68. excluded.addAll(keys);
  69. conditionsForExcluded = alwaysTrue();
  70. resolveExcluded = false;
  71. return this;
  72. }
  73. /**
  74. * If it should resolve full tree of dependencies recursively, `true` by default.
  75. */
  76. public BundleFinder deep(final boolean deep) {
  77. this.deep = deep;
  78. return this;
  79. }
  80. /**
  81. * Remove element and all its dependencies if filter fail.
  82. */
  83. public BundleFinder deepFilter(final Predicate<Bundle> filter) {
  84. deepFilters.add(filter);
  85. return this;
  86. }
  87. /**
  88. * What the walker should do when a deep filter fails.
  89. * @param failStrategy
  90. * @return
  91. */
  92. public BundleFinder onDeepFilterFail(PredicateFailStrategy failStrategy) {
  93. this.deepFilterFailStrategy = failStrategy;
  94. return this;
  95. }
  96. /**
  97. * Remove element but leave its dependencies if filter fail.
  98. */
  99. public BundleFinder shallowFilter(final Predicate<Bundle> filter) {
  100. shallowFilters.add(filter);
  101. return this;
  102. }
  103. public Found endAndGetResult() {
  104. final Predicate<Bundle> deepPredicate = deep ? alwaysTrue() : alwaysFalse();
  105. return new BundleWalker(snapshot)
  106. .find(included,
  107. excluded,
  108. conditionsForExcluded,
  109. resolveExcluded,
  110. deepPredicate,
  111. efficientAndPredicate(deepFilters),
  112. deepFilterFailStrategy,
  113. efficientAndPredicate(shallowFilters));
  114. }
  115. /**
  116. * Run query and get found Bundles.
  117. */
  118. public List<String> end() {
  119. return endAndGetResult().getFound();
  120. }
  121. }