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 | * The NetworkSection element shall list all logical networks used in the OVF package. |
30 | * |
31 | * @author Adrian Cole |
32 | */ |
33 | public class NetworkSection extends Section<NetworkSection> { |
34 | |
35 | @SuppressWarnings("unchecked") |
36 | public static Builder builder() { |
37 | return new Builder(); |
38 | } |
39 | |
40 | /** |
41 | * {@inheritDoc} |
42 | */ |
43 | @Override |
44 | public Builder toBuilder() { |
45 | return builder().fromNetworkSection(this); |
46 | } |
47 | |
48 | public static class Builder extends Section.Builder<NetworkSection> { |
49 | protected Set<Network> networks = Sets.newLinkedHashSet(); |
50 | |
51 | /** |
52 | * @see NetworkSection#getNetworks |
53 | */ |
54 | public Builder network(Network network) { |
55 | this.networks.add(checkNotNull(network, "network")); |
56 | return this; |
57 | } |
58 | |
59 | /** |
60 | * @see NetworkSection#getNetworks |
61 | */ |
62 | public Builder networks(Iterable<Network> networks) { |
63 | this.networks = ImmutableSet.<Network> copyOf(checkNotNull(networks, "networks")); |
64 | return this; |
65 | } |
66 | |
67 | /** |
68 | * {@inheritDoc} |
69 | */ |
70 | @Override |
71 | public NetworkSection build() { |
72 | return new NetworkSection(info, networks); |
73 | } |
74 | |
75 | public Builder fromNetworkSection(NetworkSection in) { |
76 | return networks(in.getNetworks()).info(in.getInfo()); |
77 | } |
78 | |
79 | /** |
80 | * {@inheritDoc} |
81 | */ |
82 | @Override |
83 | public Builder fromSection(Section<NetworkSection> in) { |
84 | return Builder.class.cast(super.fromSection(in)); |
85 | } |
86 | |
87 | /** |
88 | * {@inheritDoc} |
89 | */ |
90 | @Override |
91 | public Builder info(String info) { |
92 | return Builder.class.cast(super.info(info)); |
93 | } |
94 | |
95 | } |
96 | |
97 | private final Set<Network> networks; |
98 | |
99 | public NetworkSection(String info, Iterable<Network> networks) { |
100 | super(info); |
101 | this.networks = ImmutableSet.<Network> copyOf(checkNotNull(networks, "networks")); |
102 | } |
103 | |
104 | /** |
105 | * All networks referred to from Connection elements in all {@link VirtualHardwareSection} |
106 | * elements shall be defined in the NetworkSection. |
107 | * |
108 | * @return |
109 | */ |
110 | public Set<Network> getNetworks() { |
111 | return networks; |
112 | } |
113 | |
114 | @Override |
115 | public int hashCode() { |
116 | final int prime = 31; |
117 | int result = 1; |
118 | result = prime * result + ((info == null) ? 0 : info.hashCode()); |
119 | result = prime * result + ((networks == null) ? 0 : networks.hashCode()); |
120 | return result; |
121 | } |
122 | |
123 | @Override |
124 | public boolean equals(Object obj) { |
125 | if (this == obj) |
126 | return true; |
127 | if (obj == null) |
128 | return false; |
129 | if (getClass() != obj.getClass()) |
130 | return false; |
131 | NetworkSection other = (NetworkSection) obj; |
132 | if (info == null) { |
133 | if (other.info != null) |
134 | return false; |
135 | } else if (!info.equals(other.info)) |
136 | return false; |
137 | if (networks == null) { |
138 | if (other.networks != null) |
139 | return false; |
140 | } else if (!networks.equals(other.networks)) |
141 | return false; |
142 | return true; |
143 | } |
144 | |
145 | @Override |
146 | public String toString() { |
147 | return String.format("[info=%s, networks=%s]", info, networks); |
148 | } |
149 | |
150 | } |