| 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.internal; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.jclouds.ovf.DiskSection; |
| 26 | import org.jclouds.ovf.NetworkSection; |
| 27 | import org.jclouds.ovf.Section; |
| 28 | |
| 29 | import com.google.common.collect.ImmutableMultimap; |
| 30 | import com.google.common.collect.ImmutableSet; |
| 31 | import com.google.common.collect.LinkedHashMultimap; |
| 32 | import com.google.common.collect.Multimap; |
| 33 | import com.google.common.collect.Sets; |
| 34 | |
| 35 | /** |
| 36 | * @author Adrian Cole |
| 37 | */ |
| 38 | public class BaseEnvelope<V extends BaseVirtualSystem<V>, E extends BaseEnvelope<V, E>> { |
| 39 | |
| 40 | public static <V extends BaseVirtualSystem<V>, E extends BaseEnvelope<V, E>> Builder<V, E> builder() { |
| 41 | return new Builder<V, E>(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * {@inheritDoc} |
| 46 | */ |
| 47 | public Builder<V, E> toBuilder() { |
| 48 | return new Builder<V, E>().fromEnvelope(this); |
| 49 | } |
| 50 | |
| 51 | public static class Builder<V extends BaseVirtualSystem<V>, E extends BaseEnvelope<V, E>> { |
| 52 | protected Set<DiskSection> diskSections = Sets.newLinkedHashSet(); |
| 53 | protected Set<NetworkSection> networkSections = Sets.newLinkedHashSet(); |
| 54 | @SuppressWarnings("unchecked") |
| 55 | protected Multimap<String, Section> additionalSections = LinkedHashMultimap.create(); |
| 56 | protected V virtualSystem; |
| 57 | |
| 58 | /** |
| 59 | * @see BaseEnvelope#getDiskSections |
| 60 | */ |
| 61 | public Builder<V, E> diskSection(DiskSection diskSection) { |
| 62 | this.diskSections.add(checkNotNull(diskSection, "diskSection")); |
| 63 | return this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @see BaseEnvelope#getDiskSections |
| 68 | */ |
| 69 | public Builder<V, E> diskSections(Iterable<? extends DiskSection> diskSections) { |
| 70 | this.diskSections = ImmutableSet.<DiskSection> copyOf(checkNotNull(diskSections, "diskSections")); |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @see BaseEnvelope#getNetworkSections |
| 76 | */ |
| 77 | public Builder<V, E> networkSection(NetworkSection networkSection) { |
| 78 | this.networkSections.add(checkNotNull(networkSection, "networkSection")); |
| 79 | return this; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @see BaseEnvelope#getNetworkSections |
| 84 | */ |
| 85 | public Builder<V, E> networkSections(Iterable<? extends NetworkSection> networkSections) { |
| 86 | this.networkSections = ImmutableSet.<NetworkSection> copyOf(checkNotNull(networkSections, "networkSections")); |
| 87 | return this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * @see BaseEnvelope#getAdditionalSections |
| 92 | */ |
| 93 | @SuppressWarnings("unchecked") |
| 94 | public Builder<V, E> additionalSection(String name, Section additionalSection) { |
| 95 | this.additionalSections.put(checkNotNull(name, "name"), checkNotNull(additionalSection, "additionalSection")); |
| 96 | return this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @see BaseEnvelope#getAdditionalSections |
| 101 | */ |
| 102 | @SuppressWarnings("unchecked") |
| 103 | public Builder<V, E> additionalSections(Multimap<String, Section> additionalSections) { |
| 104 | this.additionalSections = ImmutableMultimap.<String, Section> copyOf(checkNotNull(additionalSections, |
| 105 | "additionalSections")); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @see BaseEnvelope#getVirtualSystem |
| 111 | */ |
| 112 | public Builder<V, E> virtualSystem(V virtualSystem) { |
| 113 | this.virtualSystem = virtualSystem; |
| 114 | return this; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | @SuppressWarnings("unchecked") |
| 121 | public E build() { |
| 122 | return (E) new BaseEnvelope<V, E>(diskSections, networkSections, additionalSections, virtualSystem); |
| 123 | } |
| 124 | |
| 125 | public Builder<V, E> fromEnvelope(BaseEnvelope<V, E> in) { |
| 126 | return virtualSystem(in.getVirtualSystem()).diskSections(in.getDiskSections()) |
| 127 | .networkSections(networkSections).additionalSections(in.getAdditionalSections()); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
| 132 | private final Set<DiskSection> diskSections; |
| 133 | private final Set<NetworkSection> networkSections; |
| 134 | @SuppressWarnings("unchecked") |
| 135 | private final Multimap<String, Section> additionalSections; |
| 136 | private final V virtualSystem; |
| 137 | |
| 138 | @SuppressWarnings("unchecked") |
| 139 | public BaseEnvelope(Iterable<? extends DiskSection> diskSections, Iterable<? extends NetworkSection> networkSections, |
| 140 | Multimap<String, Section> additionalSections, V virtualSystem) { |
| 141 | this.diskSections = ImmutableSet.copyOf(checkNotNull(diskSections, "diskSections")); |
| 142 | this.networkSections = ImmutableSet.copyOf(checkNotNull(networkSections, "networkSections")); |
| 143 | this.additionalSections = ImmutableMultimap.copyOf(checkNotNull(additionalSections, "additionalSections")); |
| 144 | this.virtualSystem = checkNotNull(virtualSystem, "virtualSystem"); |
| 145 | } |
| 146 | |
| 147 | public V getVirtualSystem() { |
| 148 | return virtualSystem; |
| 149 | } |
| 150 | |
| 151 | public Set<? extends DiskSection> getDiskSections() { |
| 152 | return diskSections; |
| 153 | } |
| 154 | |
| 155 | @SuppressWarnings("unchecked") |
| 156 | public Multimap<String, Section> getAdditionalSections() { |
| 157 | return additionalSections; |
| 158 | } |
| 159 | |
| 160 | @Override |
| 161 | public int hashCode() { |
| 162 | final int prime = 31; |
| 163 | int result = 1; |
| 164 | result = prime * result + ((additionalSections == null) ? 0 : additionalSections.hashCode()); |
| 165 | result = prime * result + ((diskSections == null) ? 0 : diskSections.hashCode()); |
| 166 | result = prime * result + ((networkSections == null) ? 0 : networkSections.hashCode()); |
| 167 | result = prime * result + ((virtualSystem == null) ? 0 : virtualSystem.hashCode()); |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public boolean equals(Object obj) { |
| 173 | if (this == obj) |
| 174 | return true; |
| 175 | if (obj == null) |
| 176 | return false; |
| 177 | if (getClass() != obj.getClass()) |
| 178 | return false; |
| 179 | BaseEnvelope<?, ?> other = (BaseEnvelope<?, ?>) obj; |
| 180 | if (additionalSections == null) { |
| 181 | if (other.additionalSections != null) |
| 182 | return false; |
| 183 | } else if (!additionalSections.equals(other.additionalSections)) |
| 184 | return false; |
| 185 | if (diskSections == null) { |
| 186 | if (other.diskSections != null) |
| 187 | return false; |
| 188 | } else if (!diskSections.equals(other.diskSections)) |
| 189 | return false; |
| 190 | if (networkSections == null) { |
| 191 | if (other.networkSections != null) |
| 192 | return false; |
| 193 | } else if (!networkSections.equals(other.networkSections)) |
| 194 | return false; |
| 195 | if (virtualSystem == null) { |
| 196 | if (other.virtualSystem != null) |
| 197 | return false; |
| 198 | } else if (!virtualSystem.equals(other.virtualSystem)) |
| 199 | return false; |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public String toString() { |
| 205 | return String.format("[diskSections=%s, networkSections=%s, additionalSections=%s, virtualSystem=%s]", |
| 206 | diskSections, networkSections, additionalSections, virtualSystem); |
| 207 | } |
| 208 | |
| 209 | public Set<NetworkSection> getNetworkSections() { |
| 210 | return networkSections; |
| 211 | } |
| 212 | } |