/src/main/java/nl/bitbrains/nebu/containers/ApplicationBuilder.java

https://github.com/deltaforge/nebu-core · Java · 136 lines · 72 code · 14 blank · 50 comment · 2 complexity · 9c91a3e9946e29a268176d16a04a448a MD5 · raw file

  1. package nl.bitbrains.nebu.containers;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import nl.bitbrains.nebu.common.interfaces.IBuilder;
  6. import nl.bitbrains.nebu.common.interfaces.Identifiable;
  7. import nl.bitbrains.nebu.common.topology.PhysicalResource;
  8. import nl.bitbrains.nebu.common.util.ErrorChecker;
  9. /**
  10. * Builder class for the {@link Application}.
  11. *
  12. * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
  13. *
  14. */
  15. public class ApplicationBuilder implements IBuilder<Application> {
  16. private String name;
  17. private String uuid;
  18. private String deploymentPolicy;
  19. private Map<String, VMTemplate> templates;
  20. private Map<String, Deployment> deployments;
  21. /**
  22. * Simple constructor.
  23. */
  24. public ApplicationBuilder() {
  25. this.reset();
  26. }
  27. /**
  28. * Resets the builder.
  29. */
  30. public final void reset() {
  31. this.uuid = null;
  32. this.name = null;
  33. this.deploymentPolicy = "random";
  34. this.templates = new HashMap<String, VMTemplate>();
  35. this.deployments = new HashMap<String, Deployment>();
  36. }
  37. /**
  38. * @param uuid
  39. * to build with.
  40. * @return this for fluency
  41. */
  42. public final ApplicationBuilder withUuid(final String uuid) {
  43. ErrorChecker.throwIfNullArgument(uuid, Identifiable.UUID_NAME);
  44. this.uuid = uuid;
  45. return this;
  46. }
  47. /**
  48. * @param name
  49. * to build with
  50. * @return this for fluency.
  51. */
  52. public final ApplicationBuilder withName(final String name) {
  53. ErrorChecker.throwIfNullArgument(name, "name");
  54. this.name = name;
  55. return this;
  56. }
  57. /**
  58. * @param policy
  59. * to build with
  60. * @return this for fluency.
  61. */
  62. public final ApplicationBuilder withDeploymentPolicy(final String policy) {
  63. ErrorChecker.throwIfNullArgument(policy, "policy");
  64. this.deploymentPolicy = policy;
  65. return this;
  66. }
  67. /**
  68. * @param template
  69. * to include.
  70. * @return this for fluency.
  71. */
  72. public final ApplicationBuilder withTemplate(final VMTemplate template) {
  73. ErrorChecker.throwIfNullArgument(template, "template");
  74. this.templates.put(template.getUniqueIdentifier(), template);
  75. return this;
  76. }
  77. /**
  78. * @param templates
  79. * to include.
  80. * @return this for fluency.
  81. */
  82. public final ApplicationBuilder withTemplates(final List<VMTemplate> templates) {
  83. ErrorChecker.throwIfNullArgument(templates, "templates");
  84. for (final VMTemplate template : templates) {
  85. this.withTemplate(template);
  86. }
  87. return this;
  88. }
  89. /**
  90. * @param deployment
  91. * to include.
  92. * @return this for fluency.
  93. */
  94. public final ApplicationBuilder withDeployment(final Deployment deployment) {
  95. ErrorChecker.throwIfNullArgument(deployment, "deployment");
  96. this.deployments.put(deployment.getUniqueIdentifier(), deployment);
  97. return this;
  98. }
  99. /**
  100. * @param deployments
  101. * to include.
  102. * @return this for fluency.
  103. */
  104. public final ApplicationBuilder withDeployments(final List<Deployment> deployments) {
  105. ErrorChecker.throwIfNullArgument(deployments, "deployments");
  106. for (final Deployment deployment : deployments) {
  107. this.withDeployment(deployment);
  108. }
  109. return this;
  110. }
  111. /**
  112. * @return the build {@link Application} object.
  113. */
  114. public final Application build() {
  115. ErrorChecker.throwIfNotSet(this.uuid, PhysicalResource.UUID_NAME);
  116. ErrorChecker.throwIfNotSet(this.deploymentPolicy, "deploymentPolicy");
  117. final Application app = new Application(this.uuid, this.name, this.deploymentPolicy,
  118. this.templates, this.deployments);
  119. this.reset();
  120. return app;
  121. }
  122. }