diff --git a/compute/src/main/java/org/jclouds/compute/config/ComputeServiceAdapterContextModule.java b/compute/src/main/java/org/jclouds/compute/config/ComputeServiceAdapterContextModule.java index 8bed58903c..6537d3501d 100644 --- a/compute/src/main/java/org/jclouds/compute/config/ComputeServiceAdapterContextModule.java +++ b/compute/src/main/java/org/jclouds/compute/config/ComputeServiceAdapterContextModule.java @@ -19,16 +19,14 @@ package org.jclouds.compute.config; import static com.google.common.base.Functions.compose; +import static com.google.common.base.Objects.toStringHelper; import static com.google.common.base.Predicates.notNull; -import static com.google.common.collect.Iterables.filter; -import static com.google.common.collect.Iterables.transform; import java.util.Set; import javax.inject.Inject; import javax.inject.Singleton; -import org.jclouds.collect.TransformingSetSupplier; import org.jclouds.compute.ComputeServiceAdapter; import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.Image; @@ -48,9 +46,8 @@ import org.jclouds.domain.LoginCredentials; import org.jclouds.location.suppliers.LocationsSupplier; import com.google.common.base.Function; -import com.google.common.base.Objects; import com.google.common.base.Supplier; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.FluentIterable; import com.google.inject.AbstractModule; import com.google.inject.Provides; @@ -61,16 +58,20 @@ import com.google.inject.Provides; public class ComputeServiceAdapterContextModule extends BaseComputeServiceContextModule { /** - * install this, if you want to use your computeservice adapter to handle locations. Note that if - * you do this, you'll want to instantiate a subclass to prevent type erasure. + * install this, if you want to use your computeservice adapter to handle locations. Note that if you do this, you'll + * want to instantiate a subclass to prevent type erasure. * * ex. + * *
-    *       install(new LocationsFromComputeServiceAdapterModule(){});
+    * install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>() {
+    * });
     * 
+ * * not + * *
-    *       install(new LocationsFromComputeServiceAdapterModule());
+    * install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>());
     * 
*/ public static class LocationsFromComputeServiceAdapterModule extends AbstractModule { @@ -82,17 +83,15 @@ public class ComputeServiceAdapterContextModule extends BaseComputeS @Provides @Singleton protected LocationsSupplier supplyLocationsFromComputeServiceAdapter( - final ComputeServiceAdapter adapter, final Function transformer) { + final ComputeServiceAdapter adapter, final Function transformer) { return new LocationsSupplier() { @Override public Set get() { - Iterable locations = filter(adapter.listLocations(), notNull()); - return ImmutableSet. copyOf(transform(locations, transformer)); + return transformGuardingNull(adapter.listLocations(), transformer); } - @Override public String toString() { - return Objects.toStringHelper(adapter).add("method", "listLocations").toString(); + return toStringHelper(adapter).add("method", "listLocations").toString(); } }; } @@ -101,39 +100,37 @@ public class ComputeServiceAdapterContextModule extends BaseComputeS @Provides @Singleton protected Supplier> provideHardware(final ComputeServiceAdapter adapter, - Function transformer) { - return new TransformingSetSupplier(new Supplier>() { + final Function transformer) { + return new Supplier>() { + @Override + public Set get() { + return transformGuardingNull(adapter.listHardwareProfiles(), transformer); + } - @Override - public Iterable get() { - return adapter.listHardwareProfiles(); - } - - @Override public String toString() { - return Objects.toStringHelper(adapter).add("method", "listHardwareProfiles").toString(); + return toStringHelper(adapter).add("method", "listHardwareProfiles").toString(); } - - }, transformer); + }; + } + + private static Set transformGuardingNull(Iterable from, Function transformer) { + return FluentIterable.from(from).filter(notNull()).transform(transformer).filter(notNull()).toSet(); } @Provides @Singleton protected Supplier> provideImages(final ComputeServiceAdapter adapter, - Function transformer, AddDefaultCredentialsToImage addDefaultCredentialsToImage) { - return new TransformingSetSupplier(new Supplier>() { + final Function transformer, final AddDefaultCredentialsToImage addDefaultCredentialsToImage) { + return new Supplier>() { + @Override + public Set get() { + return transformGuardingNull(adapter.listImages(), compose(addDefaultCredentialsToImage, transformer)); + } - @Override - public Iterable get() { - return filter(adapter.listImages(), notNull()); - } - - @Override public String toString() { - return Objects.toStringHelper(adapter).add("method", "listImages").toString(); + return toStringHelper(adapter).add("method", "listImages").toString(); } - - }, compose(addDefaultCredentialsToImage, transformer)); + }; } @Singleton @@ -155,14 +152,14 @@ public class ComputeServiceAdapterContextModule extends BaseComputeS @Override public String toString() { - return Objects.toStringHelper(this).add("credsForImage", credsForImage).toString(); + return toStringHelper(this).add("credsForImage", credsForImage).toString(); } } @Provides @Singleton protected CreateNodeWithGroupEncodedIntoName defineAddNodeWithTagStrategy( - AdaptingComputeServiceStrategies in) { + AdaptingComputeServiceStrategies in) { return in; } diff --git a/core/src/main/java/org/jclouds/collect/TransformingSetSupplier.java b/core/src/main/java/org/jclouds/collect/TransformingSetSupplier.java deleted file mode 100644 index 83e428e77e..0000000000 --- a/core/src/main/java/org/jclouds/collect/TransformingSetSupplier.java +++ /dev/null @@ -1,76 +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.collect; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Predicates.notNull; - -import java.util.Set; - -import com.google.common.base.Function; -import com.google.common.base.Objects; -import com.google.common.base.Supplier; -import com.google.common.collect.FluentIterable; - -/** - * - * @author Adrian Cole - */ -public class TransformingSetSupplier implements Supplier> { - - private final Supplier> backingSupplier; - private final Function converter; - - public TransformingSetSupplier(Supplier> backingSupplier, Function converter) { - this.backingSupplier = checkNotNull(backingSupplier, "backingSupplier"); - this.converter = checkNotNull(converter, "converter"); - } - - @Override - public Set get() { - Iterable original = backingSupplier.get(); - return FluentIterable.from(original) - .filter(notNull()) - .transform(converter) - .filter(notNull()) - .toSet(); - } - - @Override - public int hashCode() { - return Objects.hashCode(backingSupplier, converter); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - TransformingSetSupplier that = TransformingSetSupplier.class.cast(obj); - return Objects.equal(backingSupplier, that.backingSupplier) && Objects.equal(converter, that.converter); - } - - @Override - public String toString() { - return Objects.toStringHelper(this).add("backingSupplier", backingSupplier).add("converter", converter).toString(); - } -} diff --git a/core/src/test/java/org/jclouds/collect/TransformingSetSupplierTest.java b/core/src/test/java/org/jclouds/collect/TransformingSetSupplierTest.java deleted file mode 100644 index 3b01670d3f..0000000000 --- a/core/src/test/java/org/jclouds/collect/TransformingSetSupplierTest.java +++ /dev/null @@ -1,65 +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.collect; - -import static org.testng.Assert.assertEquals; - -import java.util.List; - -import org.testng.annotations.Test; - -import com.google.common.base.Function; -import com.google.common.base.Functions; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; - -/** - * - * @author Adrian Cole - */ -@Test(groups = "unit", singleThreaded = true, testName = "TransformingSetSupplierTest") -public class TransformingSetSupplierTest { - @Test - public void testTransform() { - TransformingSetSupplier supplier = new TransformingSetSupplier(Suppliers - .> ofInstance(ImmutableSet.of("foo")), Functions.forMap(ImmutableMap.of("foo", "bar"))); - assertEquals(supplier.get(), ImmutableSet. of("bar")); - } - - @Test - public void testNullsNotReturnedFromSourceSupplier() { - List ofNull = Lists.newArrayList(); - ofNull.add(null); - - TransformingSetSupplier supplier = new TransformingSetSupplier(Suppliers - .> ofInstance(ofNull), Functions.forMap(ImmutableMap.of("foo", "bar"))); - assertEquals(supplier.get(), ImmutableSet. of()); - } - - @SuppressWarnings("unchecked") - @Test - public void testNullsNotReturnedFromFunction() { - TransformingSetSupplier supplier = new TransformingSetSupplier(Suppliers - .> ofInstance(ImmutableSet.of("foo")), (Function) Functions.constant(null)); - assertEquals(supplier.get(), ImmutableSet. of()); - } - -}