1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
3 | * contributor license agreements. See the NOTICE file distributed with |
4 | * this work for additional information regarding copyright ownership. |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
6 | * (the "License"); you may not use this file except in compliance with |
7 | * the License. You may obtain a copy of the License at |
8 | * |
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
10 | * |
11 | * Unless required by applicable law or agreed to in writing, software |
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
14 | * See the License for the specific language governing permissions and |
15 | * limitations under the License. |
16 | */ |
17 | package org.jclouds.glesys.domain; |
18 | |
19 | import static com.google.common.base.Preconditions.checkNotNull; |
20 | |
21 | import java.beans.ConstructorProperties; |
22 | |
23 | import com.google.common.base.Objects; |
24 | import com.google.common.base.Objects.ToStringHelper; |
25 | |
26 | /** |
27 | * Class ServerSpec |
28 | * |
29 | * @author Adrian Cole |
30 | */ |
31 | public class ServerSpec { |
32 | |
33 | public static Builder<?> builder() { |
34 | return new ConcreteBuilder(); |
35 | } |
36 | |
37 | public Builder<?> toBuilder() { |
38 | return new ConcreteBuilder().fromServerSpec(this); |
39 | } |
40 | |
41 | public abstract static class Builder<T extends Builder<T>> { |
42 | protected abstract T self(); |
43 | |
44 | protected String platform; |
45 | protected String datacenter; |
46 | protected int memorySizeMB; |
47 | protected int diskSizeGB; |
48 | protected String templateName; |
49 | protected int cpuCores; |
50 | protected int transferGB; |
51 | |
52 | /** |
53 | * @see ServerSpec#getPlatform() |
54 | */ |
55 | public T platform(String platform) { |
56 | this.platform = checkNotNull(platform, "platform"); |
57 | return self(); |
58 | } |
59 | |
60 | /** |
61 | * @see ServerSpec#getDatacenter() |
62 | */ |
63 | public T datacenter(String datacenter) { |
64 | this.datacenter = checkNotNull(datacenter, "datacenter"); |
65 | return self(); |
66 | } |
67 | |
68 | /** |
69 | * @see ServerSpec#getMemorySizeMB() |
70 | */ |
71 | public T memorySizeMB(int memorySizeMB) { |
72 | this.memorySizeMB = memorySizeMB; |
73 | return self(); |
74 | } |
75 | |
76 | /** |
77 | * @see ServerSpec#getDiskSizeGB() |
78 | */ |
79 | public T diskSizeGB(int diskSizeGB) { |
80 | this.diskSizeGB = diskSizeGB; |
81 | return self(); |
82 | } |
83 | |
84 | /** |
85 | * @see ServerSpec#getTemplateName() |
86 | */ |
87 | public T templateName(String templateName) { |
88 | this.templateName = checkNotNull(templateName, "templateName"); |
89 | return self(); |
90 | } |
91 | |
92 | /** |
93 | * @see ServerSpec#getCpuCores() |
94 | */ |
95 | public T cpuCores(int cpuCores) { |
96 | this.cpuCores = cpuCores; |
97 | return self(); |
98 | } |
99 | |
100 | /** |
101 | * @see ServerSpec#getTransferGB() |
102 | */ |
103 | public T transferGB(int transferGB) { |
104 | this.transferGB = transferGB; |
105 | return self(); |
106 | } |
107 | |
108 | public ServerSpec build() { |
109 | return new ServerSpec(platform, datacenter, memorySizeMB, diskSizeGB, templateName, cpuCores, transferGB); |
110 | } |
111 | |
112 | public T fromServerSpec(ServerSpec in) { |
113 | return this.platform(in.getPlatform()) |
114 | .datacenter(in.getDatacenter()) |
115 | .memorySizeMB(in.getMemorySizeMB()) |
116 | .diskSizeGB(in.getDiskSizeGB()) |
117 | .templateName(in.getTemplateName()) |
118 | .cpuCores(in.getCpuCores()) |
119 | .transferGB(in.getTransferGB()); |
120 | } |
121 | } |
122 | |
123 | private static class ConcreteBuilder extends Builder<ConcreteBuilder> { |
124 | @Override |
125 | protected ConcreteBuilder self() { |
126 | return this; |
127 | } |
128 | } |
129 | |
130 | private final String platform; |
131 | private final String datacenter; |
132 | private final int memorySizeMB; |
133 | private final int diskSizeGB; |
134 | private final String templateName; |
135 | private final int cpuCores; |
136 | private final int transferGB; |
137 | |
138 | @ConstructorProperties({ |
139 | "platform", "datacenter", "memorySizeMB", "diskSizeGB", "templateName", "cpuCores", "transferGB" |
140 | }) |
141 | protected ServerSpec(String platform, String datacenter, int memorySizeMB, int diskSizeGB, String templateName, int cpuCores, int transferGB) { |
142 | this.platform = checkNotNull(platform, "platform"); |
143 | this.datacenter = checkNotNull(datacenter, "datacenter"); |
144 | this.memorySizeMB = memorySizeMB; |
145 | this.diskSizeGB = diskSizeGB; |
146 | this.templateName = checkNotNull(templateName, "templateName"); |
147 | this.cpuCores = cpuCores; |
148 | this.transferGB = transferGB; |
149 | } |
150 | |
151 | /** |
152 | * @return the data center to create the new server in |
153 | */ |
154 | public String getPlatform() { |
155 | return this.platform; |
156 | } |
157 | |
158 | /** |
159 | * @return the platform to use (i.e. "Xen" or "OpenVZ") |
160 | */ |
161 | public String getDatacenter() { |
162 | return this.datacenter; |
163 | } |
164 | |
165 | /** |
166 | * @return the os template to use to create the new server |
167 | */ |
168 | public int getMemorySizeMB() { |
169 | return this.memorySizeMB; |
170 | } |
171 | |
172 | /** |
173 | * @return the amount of disk space, in GB, to allocate |
174 | */ |
175 | public int getDiskSizeGB() { |
176 | return this.diskSizeGB; |
177 | } |
178 | |
179 | /** |
180 | * @return the memory, in MB, to allocate |
181 | */ |
182 | public String getTemplateName() { |
183 | return this.templateName; |
184 | } |
185 | |
186 | /** |
187 | * @return the number of CPU cores to allocate |
188 | */ |
189 | public int getCpuCores() { |
190 | return this.cpuCores; |
191 | } |
192 | |
193 | /** |
194 | * @return bandwidth of in GB |
195 | */ |
196 | public int getTransferGB() { |
197 | return this.transferGB; |
198 | } |
199 | |
200 | @Override |
201 | public int hashCode() { |
202 | return Objects.hashCode(platform, datacenter, memorySizeMB, diskSizeGB, templateName, cpuCores, transferGB); |
203 | } |
204 | |
205 | @Override |
206 | public boolean equals(Object obj) { |
207 | if (this == obj) return true; |
208 | if (obj == null || getClass() != obj.getClass()) return false; |
209 | ServerSpec that = ServerSpec.class.cast(obj); |
210 | return Objects.equal(this.platform, that.platform) |
211 | && Objects.equal(this.datacenter, that.datacenter) |
212 | && Objects.equal(this.memorySizeMB, that.memorySizeMB) |
213 | && Objects.equal(this.diskSizeGB, that.diskSizeGB) |
214 | && Objects.equal(this.templateName, that.templateName) |
215 | && Objects.equal(this.cpuCores, that.cpuCores) |
216 | && Objects.equal(this.transferGB, that.transferGB); |
217 | } |
218 | |
219 | protected ToStringHelper string() { |
220 | return Objects.toStringHelper("").add("platform", platform).add("datacenter", datacenter) |
221 | .add("memorySizeMB", memorySizeMB).add("diskSizeGB", diskSizeGB).add("templateName", templateName) |
222 | .add("cpuCores", cpuCores).add("transferGB", transferGB); |
223 | } |
224 | |
225 | @Override |
226 | public String toString() { |
227 | return string().toString(); |
228 | } |
229 | } |