PageRenderTime 19ms CodeModel.GetById 8ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

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