| 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 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 | * |
| 30 | * @author Jason King |
| 31 | * @see <a href= "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Container_Product_Order_Virtual_Guest" |
| 32 | * /> |
| 33 | */ |
| 34 | public class ProductOrder { |
| 35 | public static Builder builder() { |
| 36 | return new Builder(); |
| 37 | } |
| 38 | |
| 39 | public static class Builder { |
| 40 | private int packageId = -1; |
| 41 | private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); |
| 42 | private Set<VirtualGuest> virtualGuests = Sets.newLinkedHashSet(); |
| 43 | private String location; |
| 44 | private int quantity; |
| 45 | private boolean useHourlyPricing; |
| 46 | |
| 47 | public Builder packageId(int packageId) { |
| 48 | this.packageId = packageId; |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Adds a price to the order |
| 54 | * All that is required to send in is the price ID of each item desired to be ordered. |
| 55 | * @param prices |
| 56 | * The Prices of the item desired to be ordered |
| 57 | */ |
| 58 | public Builder price(ProductItemPrice prices) { |
| 59 | this.prices.add(checkNotNull(prices, "prices")); |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adds multiple prices to the order, overwriting any existing ones |
| 65 | * All that is required to send in is the price ID of each item desired to be ordered. |
| 66 | * @param prices |
| 67 | * The Prices of the items desired to be ordered |
| 68 | */ |
| 69 | public Builder prices(Iterable<ProductItemPrice> prices) { |
| 70 | this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices")); |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Adds a virtualGuest to the order |
| 76 | * @param virtualGuest |
| 77 | * The virtualGuest to add. Needs domain and hostname. |
| 78 | */ |
| 79 | public Builder virtualGuest(VirtualGuest virtualGuest) { |
| 80 | this.virtualGuests.add(checkNotNull(virtualGuest, "virtualGuest")); |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | public Builder virtualGuests(Iterable<VirtualGuest> virtualGuests) { |
| 85 | this.virtualGuests = ImmutableSet.<VirtualGuest> copyOf(checkNotNull(virtualGuests, "virtualGuests")); |
| 86 | return this; |
| 87 | } |
| 88 | |
| 89 | public Builder location(String location) { |
| 90 | this.location = location; |
| 91 | return this; |
| 92 | } |
| 93 | |
| 94 | public Builder quantity(int quantity) { |
| 95 | this.quantity = quantity; |
| 96 | return this; |
| 97 | } |
| 98 | |
| 99 | public Builder useHourlyPricing(Boolean useHourlyPricing) { |
| 100 | this.useHourlyPricing = useHourlyPricing; |
| 101 | return this; |
| 102 | } |
| 103 | |
| 104 | public ProductOrder build() { |
| 105 | return new ProductOrder(packageId, location,prices, virtualGuests, quantity, useHourlyPricing); |
| 106 | } |
| 107 | |
| 108 | public static Builder fromProductOrder(ProductOrder in) { |
| 109 | return ProductOrder.builder().packageId(in.getPackageId()) |
| 110 | .location(in.getLocation()) |
| 111 | .prices(in.getPrices()) |
| 112 | .virtualGuests(in.getVirtualGuests()) |
| 113 | .quantity(in.getQuantity()) |
| 114 | .useHourlyPricing(in.getUseHourlyPricing()); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | private int packageId = -1; |
| 119 | private String location; |
| 120 | private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); |
| 121 | private Set<VirtualGuest> virtualGuests = Sets.newLinkedHashSet(); |
| 122 | private int quantity; |
| 123 | private boolean useHourlyPricing; |
| 124 | |
| 125 | // for deserializer |
| 126 | ProductOrder() { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | public ProductOrder(int packageId, String location, Iterable<ProductItemPrice> prices, Iterable<VirtualGuest> virtualGuest, int quantity, boolean useHourlyPricing) { |
| 131 | this.packageId = packageId; |
| 132 | this.location = location; |
| 133 | this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices")); |
| 134 | this.virtualGuests = ImmutableSet.<VirtualGuest> copyOf(checkNotNull(virtualGuest, "virtualGuest")); |
| 135 | this.quantity = quantity; |
| 136 | this.useHourlyPricing = useHourlyPricing; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return The package id of an order. This is required. |
| 141 | */ |
| 142 | public int getPackageId() { |
| 143 | return packageId; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @return The region keyname or specific location keyname where the order should be provisioned. |
| 148 | */ |
| 149 | public String getLocation() { |
| 150 | return location; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Gets the item prices in this order. |
| 155 | * All that is required to be present is the price ID |
| 156 | * @return the prices. |
| 157 | */ |
| 158 | public Set<ProductItemPrice> getPrices() { |
| 159 | return prices; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Gets the virtual guests in this order. |
| 164 | * @return the the virtual guests. |
| 165 | */ |
| 166 | public Set<VirtualGuest> getVirtualGuests() { |
| 167 | return virtualGuests; |
| 168 | } |
| 169 | |
| 170 | public int getQuantity() { |
| 171 | return quantity; |
| 172 | } |
| 173 | |
| 174 | public boolean getUseHourlyPricing() { |
| 175 | return useHourlyPricing; |
| 176 | } |
| 177 | |
| 178 | public Builder toBuilder() { |
| 179 | return Builder.fromProductOrder(this); |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public boolean equals(Object o) { |
| 184 | if (this == o) return true; |
| 185 | if (o == null || getClass() != o.getClass()) return false; |
| 186 | |
| 187 | ProductOrder that = (ProductOrder) o; |
| 188 | |
| 189 | if (packageId != that.packageId) return false; |
| 190 | if (quantity != that.quantity) return false; |
| 191 | if (useHourlyPricing != that.useHourlyPricing) return false; |
| 192 | if (location != null ? !location.equals(that.location) : that.location != null) |
| 193 | return false; |
| 194 | if (prices != null ? !prices.equals(that.prices) : that.prices != null) |
| 195 | return false; |
| 196 | if (virtualGuests != null ? !virtualGuests.equals(that.virtualGuests) : that.virtualGuests != null) |
| 197 | return false; |
| 198 | |
| 199 | return true; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public int hashCode() { |
| 204 | int result = (packageId ^ (packageId >>> 32)); |
| 205 | result = 31 * result + (location != null ? location.hashCode() : 0); |
| 206 | result = 31 * result + (prices != null ? prices.hashCode() : 0); |
| 207 | result = 31 * result + (virtualGuests != null ? virtualGuests.hashCode() : 0); |
| 208 | result = 31 * result + (quantity ^ (quantity >>> 32)); |
| 209 | result = 31 * result + (useHourlyPricing ? 1 : 0); |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | |
| 214 | public String toString() { |
| 215 | return "[packageId=" + packageId + ", location=" + location + ", prices=" + prices |
| 216 | + ", virtualGuests=" + virtualGuests +", quantity=" + quantity + ", useHourlyPricing=" + useHourlyPricing + "]"; |
| 217 | } |
| 218 | } |