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 DeploymentOptionSection specifies a discrete set of intended resource configurations. The |
30 | * author of an OVF package can include sizing metadata for different configurations. A consumer of |
31 | * the OVF shall select a configuration, for example, by prompting the user. The selected |
32 | * configuration is visible in the OVF environment, enabling guest software to adapt to the selected |
33 | * configuration. |
34 | * |
35 | * @author Adrian Cole |
36 | */ |
37 | public class DeploymentOptionSection extends Section<DeploymentOptionSection> { |
38 | |
39 | @SuppressWarnings("unchecked") |
40 | public static Builder builder() { |
41 | return new Builder(); |
42 | } |
43 | |
44 | /** |
45 | * {@inheritDoc} |
46 | */ |
47 | @Override |
48 | public Builder toBuilder() { |
49 | return builder().fromDeploymentOptionSection(this); |
50 | } |
51 | |
52 | public static class Builder extends Section.Builder<DeploymentOptionSection> { |
53 | protected Set<Configuration> configurations = Sets.newLinkedHashSet(); |
54 | |
55 | /** |
56 | * @see DeploymentOptionSection#getConfigurations |
57 | */ |
58 | public Builder configuration(Configuration configuration) { |
59 | this.configurations.add(checkNotNull(configuration, "configuration")); |
60 | return this; |
61 | } |
62 | |
63 | /** |
64 | * @see DeploymentOptionSection#getConfigurations |
65 | */ |
66 | public Builder configurations(Iterable<Configuration> configurations) { |
67 | this.configurations = ImmutableSet.<Configuration> copyOf(checkNotNull(configurations, "configurations")); |
68 | return this; |
69 | } |
70 | |
71 | /** |
72 | * {@inheritDoc} |
73 | */ |
74 | @Override |
75 | public DeploymentOptionSection build() { |
76 | return new DeploymentOptionSection(info, configurations); |
77 | } |
78 | |
79 | public Builder fromDeploymentOptionSection(DeploymentOptionSection in) { |
80 | return info(in.getInfo()).configurations(in.getConfigurations()); |
81 | } |
82 | |
83 | /** |
84 | * {@inheritDoc} |
85 | */ |
86 | @Override |
87 | public Builder fromSection(Section<DeploymentOptionSection> in) { |
88 | return Builder.class.cast(super.fromSection(in)); |
89 | } |
90 | |
91 | /** |
92 | * {@inheritDoc} |
93 | */ |
94 | @Override |
95 | public Builder info(String info) { |
96 | return Builder.class.cast(super.info(info)); |
97 | } |
98 | |
99 | } |
100 | |
101 | protected final Set<Configuration> configurations; |
102 | |
103 | public DeploymentOptionSection(String info, Iterable<Configuration> configurations) { |
104 | super(info); |
105 | this.configurations = ImmutableSet.<Configuration> copyOf(checkNotNull(configurations, "configurations")); |
106 | |
107 | } |
108 | |
109 | @Override |
110 | public int hashCode() { |
111 | final int prime = 31; |
112 | int result = super.hashCode(); |
113 | result = prime * result + ((configurations == null) ? 0 : configurations.hashCode()); |
114 | return result; |
115 | } |
116 | |
117 | @Override |
118 | public boolean equals(Object obj) { |
119 | if (this == obj) |
120 | return true; |
121 | if (!super.equals(obj)) |
122 | return false; |
123 | if (getClass() != obj.getClass()) |
124 | return false; |
125 | DeploymentOptionSection other = (DeploymentOptionSection) obj; |
126 | if (configurations == null) { |
127 | if (other.configurations != null) |
128 | return false; |
129 | } else if (!configurations.equals(other.configurations)) |
130 | return false; |
131 | return true; |
132 | } |
133 | |
134 | @Override |
135 | public String toString() { |
136 | return String.format("[info=%s, configurations=%s]", info, configurations); |
137 | } |
138 | |
139 | public Set<Configuration> getConfigurations() { |
140 | return configurations; |
141 | } |
142 | |
143 | } |