| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.ovf; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import com.google.common.collect.ImmutableSet; |
| 26 | import com.google.common.collect.Sets; |
| 27 | |
| 28 | /** |
| 29 | * A DiskSection describes meta-information about virtual disks in the OVF package. Virtual disks |
| 30 | * and their metadata are described outside the virtual hardware to facilitate sharing between |
| 31 | * virtual machines within an OVF package. |
| 32 | * |
| 33 | * @author Adrian Cole |
| 34 | */ |
| 35 | public class DiskSection extends Section<DiskSection> { |
| 36 | |
| 37 | @SuppressWarnings("unchecked") |
| 38 | public static Builder builder() { |
| 39 | return new Builder(); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * {@inheritDoc} |
| 44 | */ |
| 45 | @Override |
| 46 | public Builder toBuilder() { |
| 47 | return new Builder().fromDiskSection(this); |
| 48 | } |
| 49 | |
| 50 | public static class Builder extends Section.Builder<DiskSection> { |
| 51 | protected Set<Disk> disks = Sets.newLinkedHashSet(); |
| 52 | |
| 53 | /** |
| 54 | * @see DiskSection#getDisks |
| 55 | */ |
| 56 | public Builder disk(Disk disk) { |
| 57 | this.disks.add(checkNotNull(disk, "disk")); |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @see DiskSection#getDisks |
| 63 | */ |
| 64 | public Builder disks(Iterable<Disk> disks) { |
| 65 | this.disks = ImmutableSet.<Disk> copyOf(checkNotNull(disks, "disks")); |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * {@inheritDoc} |
| 71 | */ |
| 72 | @Override |
| 73 | public DiskSection build() { |
| 74 | return new DiskSection(info, disks); |
| 75 | } |
| 76 | |
| 77 | public Builder fromDiskSection(DiskSection in) { |
| 78 | return disks(in.getDisks()).info(in.getInfo()); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * {@inheritDoc} |
| 83 | */ |
| 84 | @Override |
| 85 | public Builder fromSection(Section<DiskSection> in) { |
| 86 | return (Builder) super.fromSection(in); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritDoc} |
| 91 | */ |
| 92 | @Override |
| 93 | public Builder info(String info) { |
| 94 | return (Builder) super.info(info); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | |
| 99 | private final Set<Disk> disks; |
| 100 | |
| 101 | public DiskSection(String info, Iterable<Disk> disks) { |
| 102 | super(info); |
| 103 | this.disks = ImmutableSet.<Disk> copyOf(checkNotNull(disks, "disks")); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * All disks referred to from Connection elements in all {@link VirtualHardwareSection} elements |
| 108 | * shall be defined in the DiskSection. |
| 109 | * |
| 110 | * @return |
| 111 | */ |
| 112 | public Set<Disk> getDisks() { |
| 113 | return disks; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public int hashCode() { |
| 118 | final int prime = 31; |
| 119 | int result = 1; |
| 120 | result = prime * result + ((info == null) ? 0 : info.hashCode()); |
| 121 | result = prime * result + ((disks == null) ? 0 : disks.hashCode()); |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object obj) { |
| 127 | if (this == obj) |
| 128 | return true; |
| 129 | if (obj == null) |
| 130 | return false; |
| 131 | if (getClass() != obj.getClass()) |
| 132 | return false; |
| 133 | DiskSection other = (DiskSection) obj; |
| 134 | if (info == null) { |
| 135 | if (other.info != null) |
| 136 | return false; |
| 137 | } else if (!info.equals(other.info)) |
| 138 | return false; |
| 139 | if (disks == null) { |
| 140 | if (other.disks != null) |
| 141 | return false; |
| 142 | } else if (!disks.equals(other.disks)) |
| 143 | return false; |
| 144 | return true; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public String toString() { |
| 149 | return String.format("[info=%s, disks=%s]", info, disks); |
| 150 | } |
| 151 | |
| 152 | } |