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

https://github.com/deltaforge/nebu-core · Java · 113 lines · 59 code · 12 blank · 42 comment · 1 complexity · dd512540796e63f706cf2b5b8833f8fb MD5 · raw file

  1. package nl.bitbrains.nebu.containers;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import nl.bitbrains.nebu.common.VirtualMachine;
  7. import nl.bitbrains.nebu.common.interfaces.IBuilder;
  8. import nl.bitbrains.nebu.common.interfaces.Identifiable;
  9. import nl.bitbrains.nebu.common.topology.PhysicalResource;
  10. import nl.bitbrains.nebu.common.util.ErrorChecker;
  11. /**
  12. * Builder class for the {@link Deployment}.
  13. *
  14. * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
  15. *
  16. */
  17. public class DeploymentBuilder implements IBuilder<Deployment> {
  18. private String uuid;
  19. private boolean launched;
  20. private List<VMDeploymentSpecification> specs;
  21. private Map<VirtualMachine, VMDeploymentSpecification> virtualMachines;
  22. /**
  23. * Simple constructor.
  24. */
  25. public DeploymentBuilder() {
  26. this.reset();
  27. }
  28. /**
  29. * Resets the builder.
  30. */
  31. public final void reset() {
  32. this.uuid = null;
  33. this.launched = false;
  34. this.specs = new ArrayList<VMDeploymentSpecification>();
  35. this.virtualMachines = new HashMap<VirtualMachine, VMDeploymentSpecification>();
  36. }
  37. /**
  38. * @param uuid
  39. * to build with.
  40. * @return this for fluency
  41. */
  42. public final DeploymentBuilder withUuid(final String uuid) {
  43. ErrorChecker.throwIfNullArgument(uuid, Identifiable.UUID_NAME);
  44. this.uuid = uuid;
  45. return this;
  46. }
  47. /**
  48. * @param launched
  49. * to build with
  50. * @return this for fluency.
  51. */
  52. public final DeploymentBuilder withLaunched(final boolean launched) {
  53. this.launched = launched;
  54. return this;
  55. }
  56. /**
  57. * @param spec
  58. * to include.
  59. * @return this for fluency.
  60. */
  61. public final DeploymentBuilder withSpec(final VMDeploymentSpecification spec) {
  62. ErrorChecker.throwIfNullArgument(spec, "spec");
  63. this.specs.add(spec);
  64. return this;
  65. }
  66. /**
  67. * @param specs
  68. * to include.
  69. * @return this for fluency.
  70. */
  71. public final DeploymentBuilder withSpecs(final List<VMDeploymentSpecification> specs) {
  72. ErrorChecker.throwIfNullArgument(specs, "specs");
  73. for (final VMDeploymentSpecification spec : specs) {
  74. this.withSpec(spec);
  75. }
  76. return this;
  77. }
  78. /**
  79. * @param virtualMachine
  80. * to include.
  81. * @param spec
  82. * this VM was launched with.
  83. * @return this for fluency.
  84. */
  85. public final DeploymentBuilder withVM(final VirtualMachine virtualMachine,
  86. final VMDeploymentSpecification spec) {
  87. ErrorChecker.throwIfNullArgument(virtualMachine, "virtualMachine");
  88. this.virtualMachines.put(virtualMachine, spec);
  89. return this;
  90. }
  91. /**
  92. * @return the build {@link Deployment} object.
  93. */
  94. public final Deployment build() {
  95. ErrorChecker.throwIfNotSet(this.uuid, PhysicalResource.UUID_NAME);
  96. final Deployment dep = new Deployment(this.uuid, this.launched, this.specs,
  97. this.virtualMachines);
  98. this.reset();
  99. return dep;
  100. }
  101. }