/src/main/java/nl/bitbrains/nebu/common/VirtualMachineBuilder.java

https://github.com/deltaforge/nebu-common-java · Java · 122 lines · 64 code · 13 blank · 45 comment · 1 complexity · 4c43c68364881afde5db31b72574ae73 MD5 · raw file

  1. package nl.bitbrains.nebu.common;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.List;
  5. import nl.bitbrains.nebu.common.VirtualMachine.Status;
  6. import nl.bitbrains.nebu.common.interfaces.IBuilder;
  7. import nl.bitbrains.nebu.common.interfaces.Identifiable;
  8. import nl.bitbrains.nebu.common.topology.PhysicalResource;
  9. import nl.bitbrains.nebu.common.util.ErrorChecker;
  10. /**
  11. * Builder class for the {@link VirtualMachine}.
  12. *
  13. * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
  14. *
  15. */
  16. public class VirtualMachineBuilder implements IBuilder<VirtualMachine> {
  17. private String uuid;
  18. private String hostname;
  19. private Status status;
  20. private String host;
  21. private List<String> stores;
  22. /**
  23. * Simple constructor.
  24. */
  25. public VirtualMachineBuilder() {
  26. this.reset();
  27. }
  28. /**
  29. * Resets all.
  30. */
  31. public final void reset() {
  32. this.uuid = null;
  33. this.hostname = null;
  34. this.status = Status.UNKNOWN;
  35. this.host = null;
  36. this.stores = new ArrayList<String>();
  37. }
  38. /**
  39. * @param uuid
  40. * to build with.
  41. * @return this for fluency
  42. */
  43. public VirtualMachineBuilder withUuid(final String uuid) {
  44. ErrorChecker.throwIfNullArgument(uuid, PhysicalResource.UUID_NAME);
  45. this.uuid = uuid;
  46. return this;
  47. }
  48. /**
  49. * @param hostname
  50. * to build with
  51. * @return this for fluency.
  52. */
  53. public VirtualMachineBuilder withHostname(final String hostname) {
  54. ErrorChecker.throwIfNullArgument(hostname, "hostname");
  55. this.hostname = hostname;
  56. return this;
  57. }
  58. /**
  59. * @param status
  60. * to include.
  61. * @return this for fluency.
  62. */
  63. public VirtualMachineBuilder withStatus(final Status status) {
  64. this.status = status;
  65. return this;
  66. }
  67. /**
  68. * @param host
  69. * to include.
  70. * @return this for fluency.
  71. */
  72. public VirtualMachineBuilder withHost(final String host) {
  73. ErrorChecker.throwIfNullArgument(host, "host");
  74. this.host = host;
  75. return this;
  76. }
  77. /**
  78. * @param disk
  79. * to add.
  80. * @return this for fluency.
  81. */
  82. public VirtualMachineBuilder withDisk(final String disk) {
  83. ErrorChecker.throwIfNullArgument(disk, "disk");
  84. this.stores.add(disk);
  85. return this;
  86. }
  87. /**
  88. * @param disks
  89. * to add.
  90. * @return this for fluency.
  91. */
  92. public VirtualMachineBuilder withDisks(final Collection<String> disks) {
  93. ErrorChecker.throwIfNullArgument(disks, "disks");
  94. for (final String store : disks) {
  95. this.withDisk(store);
  96. }
  97. return this;
  98. }
  99. /**
  100. * @return the build {@link VirtualMachine} object.
  101. */
  102. public VirtualMachine build() {
  103. ErrorChecker.throwIfNotSet(this.uuid, Identifiable.UUID_NAME);
  104. final VirtualMachine vm = new VirtualMachine(this.uuid, this.hostname, this.status,
  105. this.host, this.stores);
  106. this.reset();
  107. return vm;
  108. }
  109. }