mirror of https://github.com/apache/jclouds.git
removed TransformingSetSupplier as it is replaced with FluentIterable
This commit is contained in:
parent
c98eec8241
commit
b19e4a7a0f
|
@ -19,16 +19,14 @@
|
||||||
package org.jclouds.compute.config;
|
package org.jclouds.compute.config;
|
||||||
|
|
||||||
import static com.google.common.base.Functions.compose;
|
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.base.Predicates.notNull;
|
||||||
import static com.google.common.collect.Iterables.filter;
|
|
||||||
import static com.google.common.collect.Iterables.transform;
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Singleton;
|
import javax.inject.Singleton;
|
||||||
|
|
||||||
import org.jclouds.collect.TransformingSetSupplier;
|
|
||||||
import org.jclouds.compute.ComputeServiceAdapter;
|
import org.jclouds.compute.ComputeServiceAdapter;
|
||||||
import org.jclouds.compute.domain.Hardware;
|
import org.jclouds.compute.domain.Hardware;
|
||||||
import org.jclouds.compute.domain.Image;
|
import org.jclouds.compute.domain.Image;
|
||||||
|
@ -48,9 +46,8 @@ import org.jclouds.domain.LoginCredentials;
|
||||||
import org.jclouds.location.suppliers.LocationsSupplier;
|
import org.jclouds.location.suppliers.LocationsSupplier;
|
||||||
|
|
||||||
import com.google.common.base.Function;
|
import com.google.common.base.Function;
|
||||||
import com.google.common.base.Objects;
|
|
||||||
import com.google.common.base.Supplier;
|
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.AbstractModule;
|
||||||
import com.google.inject.Provides;
|
import com.google.inject.Provides;
|
||||||
|
|
||||||
|
@ -61,16 +58,20 @@ import com.google.inject.Provides;
|
||||||
public class ComputeServiceAdapterContextModule<N, H, I, L> extends BaseComputeServiceContextModule {
|
public class ComputeServiceAdapterContextModule<N, H, I, L> extends BaseComputeServiceContextModule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* install this, if you want to use your computeservice adapter to handle locations. Note that if
|
* install this, if you want to use your computeservice adapter to handle locations. Note that if you do this, you'll
|
||||||
* you do this, you'll want to instantiate a subclass to prevent type erasure.
|
* want to instantiate a subclass to prevent type erasure.
|
||||||
*
|
*
|
||||||
* ex.
|
* ex.
|
||||||
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>(){});
|
* install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>() {
|
||||||
|
* });
|
||||||
* </pre>
|
* </pre>
|
||||||
|
*
|
||||||
* not
|
* not
|
||||||
|
*
|
||||||
* <pre>
|
* <pre>
|
||||||
* install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>());
|
* install(new LocationsFromComputeServiceAdapterModule<NodeMetadata, Hardware, Image, Location>());
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public static class LocationsFromComputeServiceAdapterModule<N, H, I, L> extends AbstractModule {
|
public static class LocationsFromComputeServiceAdapterModule<N, H, I, L> extends AbstractModule {
|
||||||
|
@ -82,17 +83,15 @@ public class ComputeServiceAdapterContextModule<N, H, I, L> extends BaseComputeS
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
protected LocationsSupplier supplyLocationsFromComputeServiceAdapter(
|
protected LocationsSupplier supplyLocationsFromComputeServiceAdapter(
|
||||||
final ComputeServiceAdapter<N, H, I, L> adapter, final Function<L, Location> transformer) {
|
final ComputeServiceAdapter<N, H, I, L> adapter, final Function<L, Location> transformer) {
|
||||||
return new LocationsSupplier() {
|
return new LocationsSupplier() {
|
||||||
@Override
|
@Override
|
||||||
public Set<? extends Location> get() {
|
public Set<? extends Location> get() {
|
||||||
Iterable<L> locations = filter(adapter.listLocations(), notNull());
|
return transformGuardingNull(adapter.listLocations(), transformer);
|
||||||
return ImmutableSet.<Location> copyOf(transform(locations, transformer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
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<N, H, I, L> extends BaseComputeS
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
protected Supplier<Set<? extends Hardware>> provideHardware(final ComputeServiceAdapter<N, H, I, L> adapter,
|
protected Supplier<Set<? extends Hardware>> provideHardware(final ComputeServiceAdapter<N, H, I, L> adapter,
|
||||||
Function<H, Hardware> transformer) {
|
final Function<H, Hardware> transformer) {
|
||||||
return new TransformingSetSupplier<H, Hardware>(new Supplier<Iterable<H>>() {
|
return new Supplier<Set<? extends Hardware>>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<H> get() {
|
public Set<? extends Hardware> get() {
|
||||||
return adapter.listHardwareProfiles();
|
return transformGuardingNull(adapter.listHardwareProfiles(), transformer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Objects.toStringHelper(adapter).add("method", "listHardwareProfiles").toString();
|
return toStringHelper(adapter).add("method", "listHardwareProfiles").toString();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
}, transformer);
|
private static <F, T> Set<? extends T> transformGuardingNull(Iterable<F> from, Function<F, T> transformer) {
|
||||||
|
return FluentIterable.from(from).filter(notNull()).transform(transformer).filter(notNull()).toSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
protected Supplier<Set<? extends Image>> provideImages(final ComputeServiceAdapter<N, H, I, L> adapter,
|
protected Supplier<Set<? extends Image>> provideImages(final ComputeServiceAdapter<N, H, I, L> adapter,
|
||||||
Function<I, Image> transformer, AddDefaultCredentialsToImage addDefaultCredentialsToImage) {
|
final Function<I, Image> transformer, final AddDefaultCredentialsToImage addDefaultCredentialsToImage) {
|
||||||
return new TransformingSetSupplier<I, Image>(new Supplier<Iterable<I>>() {
|
return new Supplier<Set<? extends Image>>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Iterable<I> get() {
|
public Set<? extends Image> get() {
|
||||||
return filter(adapter.listImages(), notNull());
|
return transformGuardingNull(adapter.listImages(), compose(addDefaultCredentialsToImage, transformer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Objects.toStringHelper(adapter).add("method", "listImages").toString();
|
return toStringHelper(adapter).add("method", "listImages").toString();
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}, compose(addDefaultCredentialsToImage, transformer));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
|
@ -155,14 +152,14 @@ public class ComputeServiceAdapterContextModule<N, H, I, L> extends BaseComputeS
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return Objects.toStringHelper(this).add("credsForImage", credsForImage).toString();
|
return toStringHelper(this).add("credsForImage", credsForImage).toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
protected CreateNodeWithGroupEncodedIntoName defineAddNodeWithTagStrategy(
|
protected CreateNodeWithGroupEncodedIntoName defineAddNodeWithTagStrategy(
|
||||||
AdaptingComputeServiceStrategies<N, H, I, L> in) {
|
AdaptingComputeServiceStrategies<N, H, I, L> in) {
|
||||||
return in;
|
return in;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<F, T> implements Supplier<Set<? extends T>> {
|
|
||||||
|
|
||||||
private final Supplier<Iterable<F>> backingSupplier;
|
|
||||||
private final Function<F, T> converter;
|
|
||||||
|
|
||||||
public TransformingSetSupplier(Supplier<Iterable<F>> backingSupplier, Function<F, T> converter) {
|
|
||||||
this.backingSupplier = checkNotNull(backingSupplier, "backingSupplier");
|
|
||||||
this.converter = checkNotNull(converter, "converter");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<? extends T> get() {
|
|
||||||
Iterable<F> 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();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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<String, String> supplier = new TransformingSetSupplier<String, String>(Suppliers
|
|
||||||
.<Iterable<String>> ofInstance(ImmutableSet.of("foo")), Functions.forMap(ImmutableMap.of("foo", "bar")));
|
|
||||||
assertEquals(supplier.get(), ImmutableSet.<String> of("bar"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testNullsNotReturnedFromSourceSupplier() {
|
|
||||||
List<String> ofNull = Lists.newArrayList();
|
|
||||||
ofNull.add(null);
|
|
||||||
|
|
||||||
TransformingSetSupplier<String, String> supplier = new TransformingSetSupplier<String, String>(Suppliers
|
|
||||||
.<Iterable<String>> ofInstance(ofNull), Functions.forMap(ImmutableMap.of("foo", "bar")));
|
|
||||||
assertEquals(supplier.get(), ImmutableSet.<String> of());
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Test
|
|
||||||
public void testNullsNotReturnedFromFunction() {
|
|
||||||
TransformingSetSupplier<String, String> supplier = new TransformingSetSupplier<String, String>(Suppliers
|
|
||||||
.<Iterable<String>> ofInstance(ImmutableSet.of("foo")), (Function) Functions.constant(null));
|
|
||||||
assertEquals(supplier.get(), ImmutableSet.<String> of());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue