Issue 550: Added support to filter location-based filters further by type.

* core/src/main/java/org/jclouds/providers/Providers.java
  (boundedByIso3166Code): Added another implementation that takes a type to
   further filter the results.
  (collocatedWith): Added another implementation that takes a type to
   further filter the results.

* core/src/test/java/org/jclouds/providers/ProvidersTest.java
  (testBoundedByIso3166Code, testCollocatedWith): Updated to test the versions
   that now take a type as an argument.
This commit is contained in:
Jeremy Whitlock 2011-05-26 10:56:43 -06:00
parent 86817b7612
commit d4ca5c1960
2 changed files with 66 additions and 45 deletions

View File

@ -21,6 +21,8 @@ package org.jclouds.providers;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find; import static com.google.common.collect.Iterables.find;
import com.google.common.base.Predicates;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.ServiceLoader; import java.util.ServiceLoader;
@ -127,20 +129,34 @@ public class Providers {
} }
/** /**
* Returns the providers that are bound to the same location as the given ISO 3166 code. * Returns the providers that are bound to the same location as the given ISO 3166 code regardless of type.
* *
* @param isoCode * @param isoCode
* the ISO 3166 code to filter providers by * the ISO 3166 code to filter providers by
* *
* @return the providers with the given ISO 3166 code * @return the providers bound by the given ISO 3166 code
*/ */
public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code) { public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code) {
return filter(all(), ProviderPredicates.boundedByIso3166Code(iso3166Code)); return filter(all(), ProviderPredicates.boundedByIso3166Code(iso3166Code));
} }
/** /**
* Returns the providers that have at least one common ISO 3166 code in common * Returns the providers that are bound to the same location as the given ISO 3166 code and of the given type.
* regardless of type. *
* @param iso3166Code
* the ISO 3166 code to filter providers by
* @param type
* the type to filter providers by
*
* @return the providers bound by the given ISO 3166 code and of the proper type
*/
public static Iterable<ProviderMetadata> boundedByIso3166Code(String iso3166Code, String type) {
return filter(all(), Predicates.and(ProviderPredicates.boundedByIso3166Code(iso3166Code),
ProviderPredicates.type(type)));
}
/**
* Returns the providers that have at least one common ISO 3166 code in common regardless of type.
* *
* @param providerMetadata * @param providerMetadata
* the provider metadata to use to filter providers by * the provider metadata to use to filter providers by
@ -150,4 +166,19 @@ public class Providers {
public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata) { public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata) {
return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata)); return filter(all(), ProviderPredicates.intersectingIso3166Code(providerMetadata));
} }
/**
* Returns the providers that have at least one common ISO 3166 code and are of the given type.
*
* @param providerMetadata
* the provider metadata to use to filter providers by
* @param type
* the type to filter providers by
*
* @return the providers that share at least one common ISO 3166 code and of the given type
*/
public static Iterable<ProviderMetadata> collocatedWith(ProviderMetadata providerMetadata, String type) {
return filter(all(), Predicates.and(ProviderPredicates.intersectingIso3166Code(providerMetadata),
ProviderPredicates.type(type)));
}
} }

View File

@ -21,8 +21,8 @@ package org.jclouds.providers;
import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail; import static org.testng.Assert.fail;
import java.util.HashMap; import com.google.common.collect.Iterables;
import java.util.Map;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -96,50 +96,40 @@ public class ProvidersTest {
@Test @Test
public void testBoundedByIso3166Code() { public void testBoundedByIso3166Code() {
@SuppressWarnings("serial") // Test filtering by ISO 3166 code alone
Map<String, Integer> expectedResults = new HashMap<String, Integer>() {{ assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA")), 2);
put("US-CA", 2); assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL")), 1);
put("US-FL", 1); assertEquals(Iterables.size(Providers.boundedByIso3166Code("US")), 2);
put("US", 2); assertEquals(Iterables.size(Providers.boundedByIso3166Code("JP-13")), 1);
put("JP-13", 1); assertEquals(Iterables.size(Providers.boundedByIso3166Code("JP")), 1);
put("JP", 1); assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE")), 0);
put("SOME-FAKE-CODE", 0);
}};
for (Map.Entry<String, Integer> result : expectedResults.entrySet()) { // Test filtering by ISO 3166 code and type
Iterable<ProviderMetadata> providersMetadata = Providers.boundedByIso3166Code(result.getKey()); assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA", ProviderMetadata.BLOBSTORE_TYPE)), 1);
int providersFound = 0; assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-CA", ProviderMetadata.COMPUTE_TYPE)), 1);
assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL", ProviderMetadata.BLOBSTORE_TYPE)), 1);
for (ProviderMetadata providerMetadata : providersMetadata) { assertEquals(Iterables.size(Providers.boundedByIso3166Code("US-FL", ProviderMetadata.COMPUTE_TYPE)), 0);
if (providerMetadata != null) { assertEquals(Iterables.size(Providers.boundedByIso3166Code("US", ProviderMetadata.BLOBSTORE_TYPE)), 1);
providersFound++; assertEquals(Iterables.size(Providers.boundedByIso3166Code("US", ProviderMetadata.COMPUTE_TYPE)), 1);
} assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE", ProviderMetadata.BLOBSTORE_TYPE)), 0);
} assertEquals(Iterables.size(Providers.boundedByIso3166Code("FAKE-CODE", ProviderMetadata.COMPUTE_TYPE)), 0);
assertEquals(providersFound, result.getValue().intValue());
}
} }
@Test @Test
public void testCollocatedWith() { public void testCollocatedWith() {
@SuppressWarnings("serial") // Test filtering by collocation alone
Map<ProviderMetadata, Integer> expectedResults = new HashMap<ProviderMetadata, Integer>() {{ assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider)), 1);
put(testBlobstoreProvider, 1); assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider)), 1);
put(testComputeProvider, 1); assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider)), 0);
put(testYetAnotherComputeProvider, 0);
}};
for (Map.Entry<ProviderMetadata, Integer> result : expectedResults.entrySet()) { // Test filtering by collocation and type
Iterable<ProviderMetadata> providersMetadata = Providers.collocatedWith(result.getKey()); assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider, ProviderMetadata.BLOBSTORE_TYPE)), 0);
int providersFound = 0; assertEquals(Iterables.size(Providers.collocatedWith(testBlobstoreProvider, ProviderMetadata.COMPUTE_TYPE)), 1);
assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider, ProviderMetadata.COMPUTE_TYPE)), 0);
for (ProviderMetadata providerMetadata : providersMetadata) { assertEquals(Iterables.size(Providers.collocatedWith(testComputeProvider, ProviderMetadata.BLOBSTORE_TYPE)), 1);
if (providerMetadata != null) { assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider,
providersFound++; ProviderMetadata.COMPUTE_TYPE)), 0);
} assertEquals(Iterables.size(Providers.collocatedWith(testYetAnotherComputeProvider,
} ProviderMetadata.BLOBSTORE_TYPE)), 0);
assertEquals(providersFound, result.getValue().intValue());
}
} }
} }