View Javadoc

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