PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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