PageRenderTime 33ms CodeModel.GetById 7ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

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