| 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.softlayer.domain; |
| 20 | |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import com.google.common.collect.Sets; |
| 23 | import org.jclouds.javax.annotation.Nullable; |
| 24 | |
| 25 | import java.util.Set; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | |
| 29 | /** |
| 30 | * The SoftLayer_Product_Item data type contains general information relating to |
| 31 | * a single SoftLayer product. |
| 32 | * |
| 33 | * @author Adrian Cole |
| 34 | * @see <a href= |
| 35 | * "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item" |
| 36 | * /> |
| 37 | */ |
| 38 | public class ProductItem implements Comparable<ProductItem> { |
| 39 | |
| 40 | // TODO there are more elements than this. |
| 41 | |
| 42 | public static Builder builder() { |
| 43 | return new Builder(); |
| 44 | } |
| 45 | |
| 46 | public static class Builder { |
| 47 | private int id = -1; |
| 48 | private String description; |
| 49 | private String units; |
| 50 | private Float capacity; |
| 51 | private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); |
| 52 | private Set<ProductItemCategory> categories = Sets.newLinkedHashSet(); |
| 53 | |
| 54 | public Builder id(int id) { |
| 55 | this.id = id; |
| 56 | return this; |
| 57 | } |
| 58 | |
| 59 | public Builder description(String description) { |
| 60 | this.description = description; |
| 61 | return this; |
| 62 | } |
| 63 | |
| 64 | public Builder units(String units) { |
| 65 | this.units = units; |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | public Builder capacity(Float capacity) { |
| 70 | this.capacity = capacity; |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | public Builder price(ProductItemPrice prices) { |
| 75 | this.prices.add(checkNotNull(prices, "prices")); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | public Builder prices(Iterable<ProductItemPrice> prices) { |
| 80 | this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices")); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public Builder category(ProductItemCategory categories) { |
| 85 | this.categories.add(checkNotNull(categories, "categories")); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public Builder categories(Iterable<ProductItemCategory> categories) { |
| 90 | this.categories = ImmutableSet.<ProductItemCategory> copyOf(checkNotNull(categories, "categories")); |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | public ProductItem build() { |
| 95 | return new ProductItem(id, description, units, capacity, prices, categories); |
| 96 | } |
| 97 | |
| 98 | public static Builder fromProductItem(ProductItem in) { |
| 99 | return ProductItem.builder().id(in.getId()) |
| 100 | .description(in.getDescription()) |
| 101 | .units(in.getUnits()) |
| 102 | .capacity(in.getCapacity()) |
| 103 | .prices(in.getPrices()) |
| 104 | .categories(in.getCategories()); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private int id = -1; |
| 109 | private String description; |
| 110 | private String units; |
| 111 | private Float capacity; |
| 112 | private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); |
| 113 | private Set<ProductItemCategory> categories = Sets.newLinkedHashSet(); |
| 114 | |
| 115 | // for deserializer |
| 116 | ProductItem() { |
| 117 | |
| 118 | } |
| 119 | |
| 120 | public ProductItem(int id, String description, String units, Float capacity, |
| 121 | Iterable<ProductItemPrice> prices, Iterable<ProductItemCategory> categories) { |
| 122 | this.id = id; |
| 123 | this.description = description; |
| 124 | this.units = units; |
| 125 | this.capacity = capacity; |
| 126 | this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices")); |
| 127 | this.categories = ImmutableSet.<ProductItemCategory> copyOf(checkNotNull(categories, "categories")); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public int compareTo(ProductItem arg0) { |
| 132 | return new Integer(id).compareTo(arg0.getId()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @return The unique identifier of a specific location. |
| 137 | */ |
| 138 | public int getId() { |
| 139 | return id; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return A product's description |
| 144 | */ |
| 145 | public String getDescription() { |
| 146 | return description; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return The unit of measurement that a product item is measured in. |
| 151 | */ |
| 152 | @Nullable |
| 153 | public String getUnits() { |
| 154 | return units; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return Some Product Items have capacity information such as RAM and |
| 159 | * bandwidth, and others. This provides the numerical representation |
| 160 | * of the capacity given in the description of this product item. |
| 161 | */ |
| 162 | @Nullable |
| 163 | public Float getCapacity() { |
| 164 | return capacity; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * |
| 169 | * @return A product item's prices. |
| 170 | */ |
| 171 | public Set<ProductItemPrice> getPrices() { |
| 172 | return prices; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * |
| 177 | * @return An item's associated item categories. |
| 178 | */ |
| 179 | public Set<ProductItemCategory> getCategories() { |
| 180 | return categories; |
| 181 | } |
| 182 | |
| 183 | public Builder toBuilder() { |
| 184 | return Builder.fromProductItem(this); |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public int hashCode() { |
| 189 | final int prime = 31; |
| 190 | int result = 1; |
| 191 | result = prime * result + (id ^ (id >>> 32)); |
| 192 | return result; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public boolean equals(Object obj) { |
| 197 | if (this == obj) |
| 198 | return true; |
| 199 | if (obj == null) |
| 200 | return false; |
| 201 | if (getClass() != obj.getClass()) |
| 202 | return false; |
| 203 | ProductItem other = (ProductItem) obj; |
| 204 | if (id != other.id) |
| 205 | return false; |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public String toString() { |
| 211 | return "ProductItem [id=" + id + ", description=" + description + ", units=" + units + ", capacity=" + capacity |
| 212 | + ", prices=" + prices + ", categories=" + categories + "]"; |
| 213 | } |
| 214 | } |