Issue 158: Added ProductItemCategory

This commit is contained in:
Jason King 2011-09-17 19:25:07 +01:00
parent a655d8ab25
commit 2dd0b0deeb
5 changed files with 186 additions and 12 deletions

View File

@ -18,14 +18,13 @@
*/ */
package org.jclouds.softlayer.domain; package org.jclouds.softlayer.domain;
import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.jclouds.javax.annotation.Nullable;
import java.util.Set; import java.util.Set;
import org.jclouds.javax.annotation.Nullable; import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
/** /**
* The SoftLayer_Product_Item data type contains general information relating to * The SoftLayer_Product_Item data type contains general information relating to
@ -50,6 +49,7 @@ public class ProductItem implements Comparable<ProductItem> {
private String units; private String units;
private Float capacity; private Float capacity;
private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); private Set<ProductItemPrice> prices = Sets.newLinkedHashSet();
private Set<ProductItemCategory> categories = Sets.newLinkedHashSet();
public Builder id(long id) { public Builder id(long id) {
this.id = id; this.id = id;
@ -81,13 +81,27 @@ public class ProductItem implements Comparable<ProductItem> {
return this; return this;
} }
public Builder category(ProductItemCategory categories) {
this.categories.add(checkNotNull(categories, "categories"));
return this;
}
public Builder categories(Iterable<ProductItemCategory> categories) {
this.categories = ImmutableSet.<ProductItemCategory> copyOf(checkNotNull(categories, "categories"));
return this;
}
public ProductItem build() { public ProductItem build() {
return new ProductItem(id, description, units, capacity, prices); return new ProductItem(id, description, units, capacity, prices, categories);
} }
public static Builder fromProductItem(ProductItem in) { public static Builder fromProductItem(ProductItem in) {
return ProductItem.builder().id(in.getId()).description(in.getDescription()).units(in.getUnits()) return ProductItem.builder().id(in.getId())
.capacity(in.getCapacity()).prices(in.getPrices()); .description(in.getDescription())
.units(in.getUnits())
.capacity(in.getCapacity())
.prices(in.getPrices())
.categories(in.getCategories());
} }
} }
@ -96,18 +110,21 @@ public class ProductItem implements Comparable<ProductItem> {
private String units; private String units;
private Float capacity; private Float capacity;
private Set<ProductItemPrice> prices = Sets.newLinkedHashSet(); private Set<ProductItemPrice> prices = Sets.newLinkedHashSet();
private Set<ProductItemCategory> categories = Sets.newLinkedHashSet();
// for deserializer // for deserializer
ProductItem() { ProductItem() {
} }
public ProductItem(long id, String description, String units, Float capacity, Iterable<ProductItemPrice> prices) { public ProductItem(long id, String description, String units, Float capacity,
Iterable<ProductItemPrice> prices, Iterable<ProductItemCategory> categories) {
this.id = id; this.id = id;
this.description = description; this.description = description;
this.units = units; this.units = units;
this.capacity = capacity; this.capacity = capacity;
this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices")); this.prices = ImmutableSet.<ProductItemPrice> copyOf(checkNotNull(prices, "prices"));
this.categories = ImmutableSet.<ProductItemCategory> copyOf(checkNotNull(categories, "categories"));
} }
@Override @Override
@ -155,6 +172,14 @@ public class ProductItem implements Comparable<ProductItem> {
return prices; return prices;
} }
/**
*
* @return An item's associated item categories.
*/
public Set<ProductItemCategory> getCategories() {
return categories;
}
public Builder toBuilder() { public Builder toBuilder() {
return Builder.fromProductItem(this); return Builder.fromProductItem(this);
} }
@ -184,6 +209,6 @@ public class ProductItem implements Comparable<ProductItem> {
@Override @Override
public String toString() { public String toString() {
return "ProductItem [id=" + id + ", description=" + description + ", units=" + units + ", capacity=" + capacity return "ProductItem [id=" + id + ", description=" + description + ", units=" + units + ", capacity=" + capacity
+ ", prices=" + prices + "]"; + ", prices=" + prices + ", categories=" + categories + "]";
} }
} }

View File

@ -0,0 +1,140 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.softlayer.domain;
/**
* The SoftLayer_Product_Item_Category data type contains
* general category information for prices.
*
* @author Jason King
* @see <a href=
* "http://sldn.softlayer.com/reference/datatypes/SoftLayer_Product_Item_Category"
* />
*/
public class ProductItemCategory implements Comparable<ProductItemCategory> {
// TODO there are more elements than this.
public static Builder builder() {
return new Builder();
}
public static class Builder {
private long id = -1;
private String name;
private String categoryCode;
public Builder id(long id) {
this.id = id;
return this;
}
public Builder name(String name) {
this.name = name;
return this;
}
public Builder categoryCode(String categoryCode) {
this.categoryCode = categoryCode;
return this;
}
public ProductItemCategory build() {
return new ProductItemCategory(id, name, categoryCode);
}
public static Builder fromProductItemCategory(ProductItemCategory in) {
return ProductItemCategory.builder().id(in.getId())
.name(in.getName())
.categoryCode(in.getCategoryCode());
}
}
private long id = -1;
private String name;
private String categoryCode;
// for deserializer
ProductItemCategory() {
}
public ProductItemCategory(long id, String name, String categoryCode) {
this.id = id;
this.name = name;
this.categoryCode = categoryCode;
}
@Override
public int compareTo(ProductItemCategory arg0) {
return new Long(id).compareTo(arg0.getId());
}
/**
* @return The unique identifier of a specific location.
*/
public long getId() {
return id;
}
/**
* @return The friendly, descriptive name of the category as seen on the order forms and on invoices.
*/
public String getName() {
return name;
}
/**
* @return The code used to identify this category.
*/
public String getCategoryCode() {
return categoryCode;
}
public Builder toBuilder() {
return Builder.fromProductItemCategory(this);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductItemCategory other = (ProductItemCategory) obj;
if (id != other.id)
return false;
return true;
}
@Override
public String toString() {
return "ProductItemCategory [id=" + id + ", name=" + name + ", categoryCode=" + categoryCode + "]";
}
}

View File

@ -43,7 +43,7 @@ import javax.ws.rs.core.MediaType;
@RequestFilters(BasicAuthentication.class) @RequestFilters(BasicAuthentication.class)
@Path("/v{jclouds.api-version}") @Path("/v{jclouds.api-version}")
public interface ProductPackageAsyncClient { public interface ProductPackageAsyncClient {
public static String PRODUCT_MASK = "items;locations.locationAddress"; public static String PRODUCT_MASK = "items.prices;items.categories;locations.locationAddress";
/** /**
* @see ProductPackageClient#getProductPackage * @see ProductPackageClient#getProductPackage

View File

@ -43,7 +43,7 @@ public class ProductPackageAsyncClientTest extends BaseSoftLayerAsyncClientTest<
assertRequestLineEquals( assertRequestLineEquals(
httpRequest, httpRequest,
"GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/1234.json?objectMask=items%3Blocations.locationAddress HTTP/1.1"); "GET https://api.softlayer.com/rest/v3/SoftLayer_Product_Package/1234.json?objectMask=items.prices%3Bitems.categories%3Blocations.locationAddress HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n");
assertPayloadEquals(httpRequest, null, null, false); assertPayloadEquals(httpRequest, null, null, false);

View File

@ -105,6 +105,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
private void checkProductItem(ProductItem item) { private void checkProductItem(ProductItem item) {
assert item.getId() > 0 : item; assert item.getId() > 0 : item;
assert item.getDescription() != null : item; assert item.getDescription() != null : item;
checkCategories(item.getCategories());
// units and capacity may be null // units and capacity may be null
assertTrue(item.getPrices().size() >= 0); assertTrue(item.getPrices().size() >= 0);
@ -134,4 +135,12 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
assert address.getCountry() != null : address; assert address.getCountry() != null : address;
assert address.getState() != null : address; assert address.getState() != null : address;
} }
private void checkCategories(Set<ProductItemCategory> categories) {
for( ProductItemCategory category: categories ) {
assert category.getId() >0 : category;
assert category.getName() != null : category;
assert category.getCategoryCode() != null : category;
}
}
} }