| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | package org.jclouds.aws.ec2.domain; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.Date; |
| 24 | |
| 25 | import javax.annotation.Nullable; |
| 26 | |
| 27 | import com.google.common.base.CaseFormat; |
| 28 | |
| 29 | /** |
| 30 | * |
| 31 | * @author Adrian Cole |
| 32 | */ |
| 33 | public class SpotInstanceRequest implements Comparable<SpotInstanceRequest> { |
| 34 | public static Builder builder() { |
| 35 | return new Builder(); |
| 36 | } |
| 37 | |
| 38 | public static class Builder { |
| 39 | private String region; |
| 40 | private String availabilityZoneGroup; |
| 41 | private String launchedAvailabilityZone; |
| 42 | private Date createTime; |
| 43 | private String faultCode; |
| 44 | private String faultMessage; |
| 45 | private String instanceId; |
| 46 | private String launchGroup; |
| 47 | private LaunchSpecification launchSpecification; |
| 48 | private String productDescription; |
| 49 | private String id; |
| 50 | private float spotPrice; |
| 51 | private State state; |
| 52 | private Type type; |
| 53 | private Date validFrom; |
| 54 | private Date validUntil; |
| 55 | |
| 56 | public Builder clear() { |
| 57 | this.region = null; |
| 58 | this.availabilityZoneGroup = null; |
| 59 | this.launchedAvailabilityZone = null; |
| 60 | this.createTime = null; |
| 61 | this.faultCode = null; |
| 62 | this.faultMessage = null; |
| 63 | this.instanceId = null; |
| 64 | this.launchGroup = null; |
| 65 | this.launchSpecification = null; |
| 66 | this.productDescription = null; |
| 67 | this.id = null; |
| 68 | this.spotPrice = 0; |
| 69 | this.state = null; |
| 70 | this.type = null; |
| 71 | this.validFrom = null; |
| 72 | this.validUntil = null; |
| 73 | return this; |
| 74 | } |
| 75 | |
| 76 | public Builder region(String region) { |
| 77 | this.region = region; |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | public Builder availabilityZoneGroup(String availabilityZoneGroup) { |
| 82 | this.availabilityZoneGroup = availabilityZoneGroup; |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | public Builder launchedAvailabilityZone(String launchedAvailabilityZone) { |
| 87 | this.launchedAvailabilityZone = launchedAvailabilityZone; |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | public Builder createTime(Date createTime) { |
| 92 | this.createTime = createTime; |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | public Builder faultCode(String faultCode) { |
| 97 | this.faultCode = faultCode; |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | public Builder faultMessage(String faultMessage) { |
| 102 | this.faultMessage = faultMessage; |
| 103 | return this; |
| 104 | } |
| 105 | |
| 106 | public Builder instanceId(String instanceId) { |
| 107 | this.instanceId = instanceId; |
| 108 | return this; |
| 109 | } |
| 110 | |
| 111 | public Builder launchGroup(String launchGroup) { |
| 112 | this.launchGroup = launchGroup; |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | public Builder launchSpecification(LaunchSpecification launchSpecification) { |
| 117 | this.launchSpecification = launchSpecification; |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | public Builder productDescription(String productDescription) { |
| 122 | this.productDescription = productDescription; |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | public Builder id(String id) { |
| 127 | this.id = id; |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | public Builder spotPrice(float spotPrice) { |
| 132 | this.spotPrice = spotPrice; |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | public Builder state(State state) { |
| 137 | this.state = state; |
| 138 | return this; |
| 139 | } |
| 140 | |
| 141 | public Builder type(Type type) { |
| 142 | this.type = type; |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | public Builder validFrom(Date validFrom) { |
| 147 | this.validFrom = validFrom; |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | public Builder validUntil(Date validUntil) { |
| 152 | this.validUntil = validUntil; |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | public SpotInstanceRequest build() { |
| 157 | return new SpotInstanceRequest(region, availabilityZoneGroup, launchedAvailabilityZone, createTime, faultCode, |
| 158 | faultMessage, instanceId, launchGroup, launchSpecification, productDescription, id, spotPrice, state, |
| 159 | type, validFrom, validUntil); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public enum Type { |
| 164 | ONE_TIME, PERSISTENT, UNRECOGNIZED; |
| 165 | |
| 166 | public String value() { |
| 167 | return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, name())); |
| 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public String toString() { |
| 172 | return value(); |
| 173 | } |
| 174 | |
| 175 | public static Type fromValue(String type) { |
| 176 | try { |
| 177 | return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(type, "type"))); |
| 178 | } catch (IllegalArgumentException e) { |
| 179 | return UNRECOGNIZED; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | public enum State { |
| 185 | OPEN, ACTIVE, CANCELLED, CLOSED, UNRECOGNIZED; |
| 186 | |
| 187 | public String value() { |
| 188 | return (CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, name())); |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | public String toString() { |
| 193 | return value(); |
| 194 | } |
| 195 | |
| 196 | public static State fromValue(String state) { |
| 197 | try { |
| 198 | return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, checkNotNull(state, "type"))); |
| 199 | } catch (IllegalArgumentException e) { |
| 200 | return UNRECOGNIZED; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private final String region; |
| 206 | private final String availabilityZoneGroup; |
| 207 | private final String launchedAvailabilityZone; |
| 208 | private final Date createTime; |
| 209 | private final String faultCode; |
| 210 | private final String faultMessage; |
| 211 | private final String instanceId; |
| 212 | private final String launchGroup; |
| 213 | private final LaunchSpecification launchSpecification; |
| 214 | private final String productDescription; |
| 215 | private final String id; |
| 216 | private final float spotPrice; |
| 217 | private final State state; |
| 218 | private final Type type; |
| 219 | private final Date validFrom; |
| 220 | private final Date validUntil; |
| 221 | |
| 222 | public SpotInstanceRequest(String region, String availabilityZoneGroup, @Nullable String launchedAvailabilityZone, |
| 223 | Date createTime, String faultCode, String faultMessage, String instanceId, String launchGroup, |
| 224 | LaunchSpecification launchSpecification, String productDescription, String id, float spotPrice, State state, |
| 225 | Type type, Date validFrom, Date validUntil) { |
| 226 | this.region = checkNotNull(region, "region"); |
| 227 | this.availabilityZoneGroup = availabilityZoneGroup; |
| 228 | this.launchedAvailabilityZone = launchedAvailabilityZone; |
| 229 | this.createTime = createTime; |
| 230 | this.faultCode = faultCode; |
| 231 | this.faultMessage = faultMessage; |
| 232 | this.instanceId = instanceId; |
| 233 | this.launchGroup = launchGroup; |
| 234 | this.launchSpecification = launchSpecification; |
| 235 | this.productDescription = productDescription; |
| 236 | this.id = checkNotNull(id, "id"); |
| 237 | this.spotPrice = spotPrice; |
| 238 | this.state = checkNotNull(state, "state"); |
| 239 | this.type = checkNotNull(type, "type"); |
| 240 | this.validFrom = validFrom; |
| 241 | this.validUntil = validUntil; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @return spot instance requests are in a region |
| 246 | */ |
| 247 | public String getRegion() { |
| 248 | return region; |
| 249 | } |
| 250 | |
| 251 | public String getAvailabilityZoneGroup() { |
| 252 | return availabilityZoneGroup; |
| 253 | } |
| 254 | |
| 255 | public String getLaunchedAvailabilityZone() { |
| 256 | return launchedAvailabilityZone; |
| 257 | } |
| 258 | |
| 259 | public Date getCreateTime() { |
| 260 | return createTime; |
| 261 | } |
| 262 | |
| 263 | public String getFaultCode() { |
| 264 | return faultCode; |
| 265 | } |
| 266 | |
| 267 | public String getFaultMessage() { |
| 268 | return faultMessage; |
| 269 | } |
| 270 | |
| 271 | public String getInstanceId() { |
| 272 | return instanceId; |
| 273 | } |
| 274 | |
| 275 | public String getLaunchGroup() { |
| 276 | return launchGroup; |
| 277 | } |
| 278 | |
| 279 | public LaunchSpecification getLaunchSpecification() { |
| 280 | return launchSpecification; |
| 281 | } |
| 282 | |
| 283 | public String getProductDescription() { |
| 284 | return productDescription; |
| 285 | } |
| 286 | |
| 287 | public String getId() { |
| 288 | return id; |
| 289 | } |
| 290 | |
| 291 | public float getSpotPrice() { |
| 292 | return spotPrice; |
| 293 | } |
| 294 | |
| 295 | public State getState() { |
| 296 | return state; |
| 297 | } |
| 298 | |
| 299 | public Type getType() { |
| 300 | return type; |
| 301 | } |
| 302 | |
| 303 | public Date getValidFrom() { |
| 304 | return validFrom; |
| 305 | } |
| 306 | |
| 307 | public Date getValidUntil() { |
| 308 | return validUntil; |
| 309 | } |
| 310 | |
| 311 | @Override |
| 312 | public int hashCode() { |
| 313 | final int prime = 31; |
| 314 | int result = 1; |
| 315 | result = prime * result + ((availabilityZoneGroup == null) ? 0 : availabilityZoneGroup.hashCode()); |
| 316 | result = prime * result + ((createTime == null) ? 0 : createTime.hashCode()); |
| 317 | result = prime * result + ((faultCode == null) ? 0 : faultCode.hashCode()); |
| 318 | result = prime * result + ((faultMessage == null) ? 0 : faultMessage.hashCode()); |
| 319 | result = prime * result + ((id == null) ? 0 : id.hashCode()); |
| 320 | result = prime * result + ((instanceId == null) ? 0 : instanceId.hashCode()); |
| 321 | result = prime * result + ((launchGroup == null) ? 0 : launchGroup.hashCode()); |
| 322 | result = prime * result + ((launchSpecification == null) ? 0 : launchSpecification.hashCode()); |
| 323 | result = prime * result + ((launchedAvailabilityZone == null) ? 0 : launchedAvailabilityZone.hashCode()); |
| 324 | result = prime * result + ((productDescription == null) ? 0 : productDescription.hashCode()); |
| 325 | result = prime * result + ((region == null) ? 0 : region.hashCode()); |
| 326 | result = prime * result + Float.floatToIntBits(spotPrice); |
| 327 | result = prime * result + ((state == null) ? 0 : state.hashCode()); |
| 328 | result = prime * result + ((type == null) ? 0 : type.hashCode()); |
| 329 | result = prime * result + ((validFrom == null) ? 0 : validFrom.hashCode()); |
| 330 | result = prime * result + ((validUntil == null) ? 0 : validUntil.hashCode()); |
| 331 | return result; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public boolean equals(Object obj) { |
| 336 | if (this == obj) |
| 337 | return true; |
| 338 | if (obj == null) |
| 339 | return false; |
| 340 | if (getClass() != obj.getClass()) |
| 341 | return false; |
| 342 | SpotInstanceRequest other = (SpotInstanceRequest) obj; |
| 343 | if (availabilityZoneGroup == null) { |
| 344 | if (other.availabilityZoneGroup != null) |
| 345 | return false; |
| 346 | } else if (!availabilityZoneGroup.equals(other.availabilityZoneGroup)) |
| 347 | return false; |
| 348 | if (createTime == null) { |
| 349 | if (other.createTime != null) |
| 350 | return false; |
| 351 | } else if (!createTime.equals(other.createTime)) |
| 352 | return false; |
| 353 | if (faultCode == null) { |
| 354 | if (other.faultCode != null) |
| 355 | return false; |
| 356 | } else if (!faultCode.equals(other.faultCode)) |
| 357 | return false; |
| 358 | if (faultMessage == null) { |
| 359 | if (other.faultMessage != null) |
| 360 | return false; |
| 361 | } else if (!faultMessage.equals(other.faultMessage)) |
| 362 | return false; |
| 363 | if (id == null) { |
| 364 | if (other.id != null) |
| 365 | return false; |
| 366 | } else if (!id.equals(other.id)) |
| 367 | return false; |
| 368 | if (instanceId == null) { |
| 369 | if (other.instanceId != null) |
| 370 | return false; |
| 371 | } else if (!instanceId.equals(other.instanceId)) |
| 372 | return false; |
| 373 | if (launchGroup == null) { |
| 374 | if (other.launchGroup != null) |
| 375 | return false; |
| 376 | } else if (!launchGroup.equals(other.launchGroup)) |
| 377 | return false; |
| 378 | if (launchSpecification == null) { |
| 379 | if (other.launchSpecification != null) |
| 380 | return false; |
| 381 | } else if (!launchSpecification.equals(other.launchSpecification)) |
| 382 | return false; |
| 383 | if (launchedAvailabilityZone == null) { |
| 384 | if (other.launchedAvailabilityZone != null) |
| 385 | return false; |
| 386 | } else if (!launchedAvailabilityZone.equals(other.launchedAvailabilityZone)) |
| 387 | return false; |
| 388 | if (productDescription == null) { |
| 389 | if (other.productDescription != null) |
| 390 | return false; |
| 391 | } else if (!productDescription.equals(other.productDescription)) |
| 392 | return false; |
| 393 | if (region == null) { |
| 394 | if (other.region != null) |
| 395 | return false; |
| 396 | } else if (!region.equals(other.region)) |
| 397 | return false; |
| 398 | if (Float.floatToIntBits(spotPrice) != Float.floatToIntBits(other.spotPrice)) |
| 399 | return false; |
| 400 | if (state != other.state) |
| 401 | return false; |
| 402 | if (type != other.type) |
| 403 | return false; |
| 404 | if (validFrom == null) { |
| 405 | if (other.validFrom != null) |
| 406 | return false; |
| 407 | } else if (!validFrom.equals(other.validFrom)) |
| 408 | return false; |
| 409 | if (validUntil == null) { |
| 410 | if (other.validUntil != null) |
| 411 | return false; |
| 412 | } else if (!validUntil.equals(other.validUntil)) |
| 413 | return false; |
| 414 | return true; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public String toString() { |
| 419 | return "[region=" + region + ", availabilityZoneGroup=" + availabilityZoneGroup + ", launchedAvailabilityZone=" |
| 420 | + launchedAvailabilityZone + ", createTime=" + createTime + ", faultCode=" + faultCode + ", faultMessage=" |
| 421 | + faultMessage + ", instanceId=" + instanceId + ", launchGroup=" + launchGroup + ", launchSpecification=" |
| 422 | + launchSpecification + ", productDescription=" + productDescription + ", id=" + id + ", spotPrice=" |
| 423 | + spotPrice + ", state=" + state + ", type=" + type + ", validFrom=" + validFrom + ", validUntil=" |
| 424 | + validUntil + "]"; |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public int compareTo(SpotInstanceRequest arg0) { |
| 429 | return createTime.compareTo(arg0.createTime); |
| 430 | } |
| 431 | |
| 432 | } |