PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource-rest/src/main/java/com/atlassian/webresource/plugin/async/model/ResourcesAndData.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 161 lines | 131 code | 25 blank | 5 comment | 7 complexity | 02d884d2528b536b703e679e62e0b95c MD5 | raw file
  1. package com.atlassian.webresource.plugin.async.model;
  2. import com.atlassian.json.marshal.Jsonable;
  3. import com.atlassian.webresource.api.assembler.WebResource;
  4. import com.atlassian.webresource.api.assembler.resource.PluginUrlResource;
  5. import com.atlassian.webresource.api.assembler.resource.ResourcePhase;
  6. import com.atlassian.webresource.api.data.PluginDataResource;
  7. import javax.annotation.Nonnull;
  8. import java.io.IOException;
  9. import java.io.StringWriter;
  10. import java.util.ArrayList;
  11. import java.util.Collection;
  12. import java.util.HashMap;
  13. import java.util.Map;
  14. import java.util.Objects;
  15. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.INTERACTION;
  16. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.REQUIRE;
  17. import static java.util.Objects.requireNonNull;
  18. import static java.util.stream.Collectors.toCollection;
  19. import static java.util.stream.Collectors.toMap;
  20. import static java.util.stream.StreamSupport.stream;
  21. public class ResourcesAndData {
  22. private final OutputShape require;
  23. private final OutputShape interaction;
  24. public ResourcesAndData(@Nonnull final OutputShape require,
  25. @Nonnull final OutputShape interaction) {
  26. this.require = requireNonNull(require, "The required resources are mandatory.");
  27. this.interaction = requireNonNull(interaction, "The resources for interaction are mandatory.");
  28. }
  29. public ResourcesAndData(@Nonnull final Iterable<WebResource> resources) {
  30. this(
  31. buildOutputShape(resources, REQUIRE),
  32. buildOutputShape(resources, INTERACTION)
  33. );
  34. }
  35. @Nonnull
  36. public OutputShape getRequire() {
  37. return require;
  38. }
  39. @Nonnull
  40. public Collection<ResourceTypeAndUrl> getRequireResources() {
  41. return new ArrayList<>(require.getResources());
  42. }
  43. @Nonnull
  44. public Map<String, String> getRequiredResourcesUnparsedData() {
  45. return new HashMap<>(require.getUnparsedData());
  46. }
  47. @Nonnull
  48. public Map<String, String> getRequireResourcesUnparsedErrors() {
  49. return new HashMap<>(require.getUnparsedErrors());
  50. }
  51. @Nonnull
  52. public Collection<ResourceTypeAndUrl> getResourcesForInteration() {
  53. return new ArrayList<>(interaction.getResources());
  54. }
  55. @Nonnull
  56. public Map<String, String> getResourcesForInterationUnparsedData() {
  57. return new HashMap<>(interaction.getUnparsedData());
  58. }
  59. @Nonnull
  60. public Map<String, String> getResourcesForInterationUnparsedErrors() {
  61. return new HashMap<>(interaction.getUnparsedErrors());
  62. }
  63. @Nonnull
  64. public OutputShape getInteraction() {
  65. return interaction;
  66. }
  67. @Override
  68. public boolean equals(final Object other) {
  69. if (this == other) {
  70. return true;
  71. }
  72. if (other instanceof ResourcesAndData) {
  73. final ResourcesAndData otherResourcesAndData = (ResourcesAndData) other;
  74. return require.equals(otherResourcesAndData.require)
  75. && interaction.equals(otherResourcesAndData.interaction);
  76. }
  77. return false;
  78. }
  79. @Override
  80. public int hashCode() {
  81. return Objects.hash(require, interaction);
  82. }
  83. @Override
  84. public String toString() {
  85. return "ResourcesAndData{ requirePhase=" + require + ", interactionPhase=" + interaction + " }";
  86. }
  87. /**
  88. * Merge all resources information into the current object.
  89. *
  90. * @param resources The resources to be merged into the current object.
  91. */
  92. public void merge(@Nonnull final Iterable<WebResource> resources) {
  93. requireNonNull(resources, "The resorces are mandatory for the merge action.");
  94. merge(new ResourcesAndData(resources));
  95. }
  96. private void merge(final ResourcesAndData resourcesAndData) {
  97. requireNonNull(resourcesAndData, "The resorces and data are mandatory for the merge action.");
  98. require.merge(resourcesAndData.require);
  99. interaction.merge(resourcesAndData.interaction);
  100. }
  101. private static OutputShape buildOutputShape(final Iterable<WebResource> resources,
  102. final ResourcePhase resourcePhase) {
  103. requireNonNull(resources, "The resources are mandatory to perform the conversion to OutputShape.");
  104. final Map<String, String> data =
  105. stream(resources.spliterator(), false)
  106. .filter(resource -> resourcePhase == resource.getResourcePhase())
  107. .filter(PluginDataResource.class::isInstance)
  108. .map(PluginDataResource.class::cast)
  109. .filter(resource -> resource.getData().isPresent())
  110. .collect(toMap(PluginDataResource::getKey, resource -> jsonToString(resource.getJsonable())));
  111. final Map<String, String> errors =
  112. stream(resources.spliterator(), false)
  113. .filter(resource -> resourcePhase == resource.getResourcePhase())
  114. .filter(PluginDataResource.class::isInstance)
  115. .map(PluginDataResource.class::cast)
  116. .filter(resource -> !resource.getData().isPresent())
  117. .collect(toMap(PluginDataResource::getKey, resource -> jsonToString(resource.getJsonable())));
  118. final Collection<ResourceTypeAndUrl> resourcesTypesAndUrls =
  119. stream(resources.spliterator(), false)
  120. .filter(resource -> resourcePhase == resource.getResourcePhase())
  121. .filter(PluginUrlResource.class::isInstance)
  122. .map(PluginUrlResource.class::cast)
  123. .map(ResourceTypeAndUrl::new)
  124. .collect(toCollection(ArrayList::new));
  125. return new OutputShape(resourcesTypesAndUrls, data, errors);
  126. }
  127. private static String jsonToString(final Jsonable jsonable) {
  128. try {
  129. final StringWriter out = new StringWriter();
  130. jsonable.write(out);
  131. return out.toString();
  132. } catch (final IOException exception) {
  133. throw new IllegalStateException(exception);
  134. }
  135. }
  136. }