Issue 158: Review cleanup: move small classes to static methods

This commit is contained in:
Jason King 2011-09-19 14:39:06 +01:00
parent 4790e588b4
commit 148641c3ce
8 changed files with 95 additions and 182 deletions

View File

@ -1,44 +0,0 @@
/**
* 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.compute.functions;
import com.google.common.base.Function;
import org.jclouds.softlayer.domain.ProductItem;
import java.util.NoSuchElementException;
/**
* Returns the description of the ProductItem.
* @author Jason King
*/
public class DescriptionFromProductItem implements Function<ProductItem,String> {
/**
*
* @param productItem the productItem to use
* @return the description.
* @throws java.util.NoSuchElementException if the description is missing
*/
@Override
public String apply(ProductItem productItem) {
String result = productItem.getDescription();
if(result==null) throw new NoSuchElementException();
return result;
}
}

View File

@ -21,24 +21,29 @@ package org.jclouds.softlayer.compute.functions;
import com.google.common.base.Function; import com.google.common.base.Function;
import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItem;
import java.util.NoSuchElementException; public class ProductItems {
/**
* Returns the capacity of the ProductItem.
* @author Jason King
*/
public class CapacityFromProductItem implements Function<ProductItem,Float> {
/** /**
* * Creates a function to get the capacity from a product item.
* @param productItem the productItem to use
* @return the capacity
* @throws NoSuchElementException if the capacity is missing
*/ */
@Override public static Function<ProductItem,Float> capacity() {
public Float apply(ProductItem productItem) { return new Function<ProductItem,Float>() {
Float result = productItem.getCapacity(); @Override
if(result==null) throw new NoSuchElementException(); public Float apply(ProductItem productItem) {
return result; return productItem.getCapacity();
}
};
}
/**
* Creates a function to get the description from a product item.
*/
public static Function<ProductItem,String> description() {
return new Function<ProductItem,String>() {
@Override
public String apply(ProductItem productItem) {
return productItem.getDescription();
}
};
} }
} }

View File

@ -22,6 +22,8 @@ import com.google.common.base.Predicate;
import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemCategory; import org.jclouds.softlayer.domain.ProductItemCategory;
import static com.google.common.base.Preconditions.checkNotNull;
public class ProductItemPredicates { public class ProductItemPredicates {
/** /**
@ -30,9 +32,11 @@ public class ProductItemPredicates {
* @return true if it does, otherwise false. * @return true if it does, otherwise false.
*/ */
public static Predicate<ProductItem> categoryCode(final String category) { public static Predicate<ProductItem> categoryCode(final String category) {
checkNotNull(category, "category cannot be null");
return new Predicate<ProductItem>() { return new Predicate<ProductItem>() {
@Override @Override
public boolean apply(ProductItem productItem) { public boolean apply(ProductItem productItem) {
checkNotNull(productItem, "productItem cannot ne null");
for(ProductItemCategory productItemCategory: productItem.getCategories()) { for(ProductItemCategory productItemCategory: productItem.getCategories()) {
if(category.equals(productItemCategory.getCategoryCode())) return true; if(category.equals(productItemCategory.getCategoryCode())) return true;
} }
@ -52,12 +56,14 @@ public class ProductItemPredicates {
* @return true if it does, otherwise false. * @return true if it does, otherwise false.
*/ */
public static Predicate<ProductItem> capacity(final Float capacity) { public static Predicate<ProductItem> capacity(final Float capacity) {
checkNotNull(capacity, "capacity cannot be null");
return new Predicate<ProductItem>() { return new Predicate<ProductItem>() {
@Override @Override
public boolean apply(ProductItem productItem) { public boolean apply(ProductItem productItem) {
checkNotNull(productItem, "productItem cannot ne null");
Float productItemCapacity = productItem.getCapacity(); Float productItemCapacity = productItem.getCapacity();
if (productItemCapacity == null) return false; if (productItemCapacity == null) return false;
return productItemCapacity.equals(capacity); return capacity.equals(productItemCapacity);
} }
@Override @Override
@ -74,12 +80,12 @@ public class ProductItemPredicates {
* @return true if it does, otherwise false. * @return true if it does, otherwise false.
*/ */
public static Predicate<ProductItem> units(final String units) { public static Predicate<ProductItem> units(final String units) {
checkNotNull(units, "units cannot be null");
return new Predicate<ProductItem>() { return new Predicate<ProductItem>() {
@Override @Override
public boolean apply(ProductItem productItem) { public boolean apply(ProductItem productItem) {
String productItemUnits = productItem.getUnits(); checkNotNull(productItem, "productItem cannot ne null");
if (productItemUnits == null) return false; return units.equals(productItem.getUnits());
return productItemUnits.equals(units);
} }
@Override @Override

View File

@ -18,9 +18,10 @@
*/ */
package org.jclouds.softlayer.predicates; package org.jclouds.softlayer.predicates;
import com.google.common.base.Predicate;
import org.jclouds.softlayer.domain.ProductPackage; import org.jclouds.softlayer.domain.ProductPackage;
import com.google.common.base.Predicate; import static com.google.common.base.Preconditions.checkNotNull;
public class ProductPackagePredicates { public class ProductPackagePredicates {
@ -32,7 +33,8 @@ public class ProductPackagePredicates {
public static Predicate<ProductPackage> named(final String packageName) { public static Predicate<ProductPackage> named(final String packageName) {
return new Predicate<ProductPackage>() { return new Predicate<ProductPackage>() {
public boolean apply(ProductPackage productPackage) { public boolean apply(ProductPackage productPackage) {
return productPackage.getName().equals(packageName); checkNotNull(productPackage, "productPackage cannot be null");
return productPackage.getName().equals(packageName);
} }
}; };
} }

View File

@ -1,58 +0,0 @@
/**
* 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.compute.functions;
import org.jclouds.softlayer.domain.ProductItem;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.NoSuchElementException;
import static org.testng.Assert.assertEquals;
/**
* Tests {@code DescriptionFromProductItem}
*
* @author Jason King
*/
@Test(groups = "unit")
public class CapacityFromProductItemTest {
private CapacityFromProductItem function;
@BeforeMethod
public void setup() {
function = new CapacityFromProductItem();
}
@Test
public void testCapacity() {
ProductItem item = ProductItem.builder()
.id(1).capacity(2.0f)
.build();
assertEquals(function.apply(item),2.0f);
}
@Test(expectedExceptions = NoSuchElementException.class)
public void testCapacityMissing() {
ProductItem item = ProductItem.builder()
.id(1).build();
function.apply(item);
}
}

View File

@ -22,36 +22,43 @@ import org.jclouds.softlayer.domain.ProductItem;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.util.NoSuchElementException; import static org.jclouds.softlayer.compute.functions.ProductItems.capacity;
import static org.jclouds.softlayer.compute.functions.ProductItems.description;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
/** /**
* Tests {@code DescriptionFromProductItem} * Tests {@code ProductItems}
*
* @author Jason King * @author Jason King
*/ */
@Test(groups = "unit") @Test(groups = "unit")
public class DescriptionFromProductItemTest { public class ProductItemsTest {
private DescriptionFromProductItem function; private ProductItem item;
@BeforeMethod @BeforeMethod
public void setup() { public void setup() {
function = new DescriptionFromProductItem(); item = ProductItem.builder().id(1).capacity(2.0f).description("an item").build();
}
@Test
public void testCapacity() {
assertEquals(capacity().apply(item), 2.0f);
}
@Test
public void testCapacityMissing() {
ProductItem item = ProductItem.builder().id(1).build();
assertNull(capacity().apply(item));
} }
@Test @Test
public void testDescription() { public void testDescription() {
ProductItem item = ProductItem.builder() assertEquals(description().apply(item),"an item");
.id(1).description("an item")
.build();
assertEquals(function.apply(item),"an item");
} }
@Test(expectedExceptions = NoSuchElementException.class)
public void testDescriptionMissing() { public void testDescriptionMissing() {
ProductItem item = ProductItem.builder() ProductItem item = ProductItem.builder().id(1).build();
.id(1).build(); assertNull(description().apply(item));
function.apply(item);
} }
} }

View File

@ -23,9 +23,8 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.jclouds.softlayer.compute.functions.CapacityFromProductItem;
import org.jclouds.softlayer.compute.functions.DescriptionFromProductItem;
import org.jclouds.softlayer.compute.functions.ProductItemPriceFromProductItem; import org.jclouds.softlayer.compute.functions.ProductItemPriceFromProductItem;
import org.jclouds.softlayer.compute.functions.ProductItems;
import org.jclouds.softlayer.domain.*; import org.jclouds.softlayer.domain.*;
import org.jclouds.softlayer.reference.SoftLayerConstants; import org.jclouds.softlayer.reference.SoftLayerConstants;
import org.testng.annotations.BeforeGroups; import org.testng.annotations.BeforeGroups;
@ -118,7 +117,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Predicates.and(categoryCode("ram"), capacity(1.0f))); Predicates.and(categoryCode("ram"), capacity(1.0f)));
// capacity is key in GB (1Gb = 1.0f) // capacity is key in GB (1Gb = 1.0f)
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, new CapacityFromProductItem()); Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f)); ProductItemPrice price = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
assert new Long(1644L).equals(price.getId()); assert new Long(1644L).equals(price.getId());
@ -130,7 +129,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f))); Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
// number of cores is the key // number of cores is the key
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, new CapacityFromProductItem()); Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f)); ProductItemPrice price = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
assert new Long(1963L).equals(price.getId()); assert new Long(1963L).equals(price.getId());
@ -140,7 +139,7 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
public void testGetUbuntuPrice() { public void testGetUbuntuPrice() {
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os")); Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, new DescriptionFromProductItem()); Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice price = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)")); ProductItemPrice price = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
assert new Long(1693L).equals(price.getId()); assert new Long(1693L).equals(price.getId());
@ -151,17 +150,17 @@ public class ProductPackageClientLiveTest extends BaseSoftLayerClientLiveTest {
Iterable<ProductItem> ramItems = Iterables.filter(cloudServerProductPackage.getItems(), Iterable<ProductItem> ramItems = Iterables.filter(cloudServerProductPackage.getItems(),
Predicates.and(categoryCode("ram"), capacity(1.0f))); Predicates.and(categoryCode("ram"), capacity(1.0f)));
Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, new CapacityFromProductItem()); Map<Float, ProductItem> ramToProductItem = Maps.uniqueIndex(ramItems, ProductItems.capacity());
ProductItemPrice ramPrice = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f)); ProductItemPrice ramPrice = new ProductItemPriceFromProductItem().apply(ramToProductItem.get(1.0f));
Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f))); Iterable<ProductItem> cpuItems = Iterables.filter(cloudServerProductPackage.getItems(), Predicates.and(units("PRIVATE_CORE"), capacity(2.0f)));
Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, new CapacityFromProductItem()); Map<Float, ProductItem> coresToProductItem = Maps.uniqueIndex(cpuItems, ProductItems.capacity());
ProductItemPrice cpuPrice = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f)); ProductItemPrice cpuPrice = new ProductItemPriceFromProductItem().apply(coresToProductItem.get(2.0f));
Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os")); Iterable<ProductItem> operatingSystems = Iterables.filter(cloudServerProductPackage.getItems(), categoryCode("os"));
Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, new DescriptionFromProductItem()); Map<String, ProductItem> osToProductItem = Maps.uniqueIndex(operatingSystems, ProductItems.description());
ProductItemPrice osPrice = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)")); ProductItemPrice osPrice = new ProductItemPriceFromProductItem().apply(osToProductItem.get("Ubuntu Linux 8 LTS Hardy Heron - Minimal Install (64 bit)"));
Set<Long> prices = Sets.<Long>newLinkedHashSet(); Set<Long> prices = Sets.<Long>newLinkedHashSet();

View File

@ -21,6 +21,7 @@ package org.jclouds.softlayer.predicates;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import org.jclouds.softlayer.domain.ProductItem; import org.jclouds.softlayer.domain.ProductItem;
import org.jclouds.softlayer.domain.ProductItemCategory; import org.jclouds.softlayer.domain.ProductItemCategory;
import org.testng.annotations.BeforeGroups;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertFalse;
@ -28,31 +29,36 @@ import static org.testng.Assert.assertFalse;
@Test(sequential = true,groups = "unit") @Test(sequential = true,groups = "unit")
public class ProductItemPredicatesTest { public class ProductItemPredicatesTest {
private ProductItemCategory ramCategory;
private ProductItem item;
private ProductItem emptyItem;
@BeforeGroups(groups = { "unit" })
public void setupClient() {
ramCategory = ProductItemCategory.builder().id(1).categoryCode("ram").build();
item = ProductItem.builder().id(1)
.categories(ImmutableSet.of(ramCategory))
.capacity(2.0f)
.units("GB")
.build();
emptyItem = ProductItem.builder().id(1).build();
}
@Test @Test
public void testCategoryCodePresent() { public void testCategoryCodePresent() {
ProductItemCategory category = ProductItemCategory.builder()
.id(1).categoryCode("ram")
.build();
ProductItem item = ProductItem.builder()
.categories(ImmutableSet.of(category))
.build();
assert ProductItemPredicates.categoryCode("ram").apply(item); assert ProductItemPredicates.categoryCode("ram").apply(item);
} }
@Test @Test
public void testCategoryCodePresentTwoCategories() { public void testCategoryCodePresentTwoCategories() {
ProductItemCategory category1 = ProductItemCategory.builder() ProductItemCategory osCategory = ProductItemCategory.builder()
.id(1).categoryCode("os") .id(2).categoryCode("os")
.build();
ProductItemCategory category2 = ProductItemCategory.builder()
.id(2).categoryCode("ram")
.build(); .build();
ProductItem item = ProductItem.builder() ProductItem item = ProductItem.builder()
.categories(ImmutableSet.of(category1, category2)) .categories(ImmutableSet.of(ramCategory, osCategory))
.build(); .build();
assert ProductItemPredicates.categoryCode("ram").apply(item); assert ProductItemPredicates.categoryCode("ram").apply(item);
@ -60,36 +66,26 @@ public class ProductItemPredicatesTest {
@Test @Test
public void testCategoryCodeMissing() { public void testCategoryCodeMissing() {
ProductItem item = ProductItem.builder() assertFalse(ProductItemPredicates.categoryCode("missing").apply(emptyItem));
.categories(ImmutableSet.<ProductItemCategory>of())
.build();
assertFalse(ProductItemPredicates.categoryCode("ram").apply(item));
} }
@Test @Test
public void testCapacity() { public void testCapacityPresent() {
ProductItem item = ProductItem.builder()
.id(1).capacity(2.0f)
.build();
assert ProductItemPredicates.capacity(2.0f).apply(item); assert ProductItemPredicates.capacity(2.0f).apply(item);
} }
@Test @Test
public void testCapacityDifferent() { public void testCapacityMissing() {
ProductItem item = ProductItem.builder()
.id(1).capacity(2.0f)
.build();
assertFalse(ProductItemPredicates.capacity(1.0f).apply(item)); assertFalse(ProductItemPredicates.capacity(1.0f).apply(item));
} }
@Test @Test
public void testCapacityMissing() { public void testUnitsPresent() {
ProductItem item = ProductItem.builder() assert ProductItemPredicates.units("GB").apply(item);
.id(1).build();
assertFalse(ProductItemPredicates.capacity(2.0f).apply(item));
} }
}
@Test
public void testUnitsMissing() {
assertFalse(ProductItemPredicates.units("Kg").apply(item));
}
}