mirror of https://github.com/apache/jclouds.git
cleaner impl of LocationPredicate, which doesn't eagerly fetch current location on toString()
This commit is contained in:
parent
cf3261e5e7
commit
2f8b127a32
|
@ -17,7 +17,10 @@
|
||||||
* under the License.
|
* under the License.
|
||||||
*/
|
*/
|
||||||
package org.jclouds.compute.domain.internal;
|
package org.jclouds.compute.domain.internal;
|
||||||
import static com.google.common.base.Preconditions.*;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static com.google.common.base.Predicates.equalTo;
|
||||||
|
import static com.google.common.base.Predicates.or;
|
||||||
|
|
||||||
import org.jclouds.compute.domain.ComputeMetadata;
|
import org.jclouds.compute.domain.ComputeMetadata;
|
||||||
import org.jclouds.domain.Location;
|
import org.jclouds.domain.Location;
|
||||||
|
@ -25,6 +28,8 @@ import org.jclouds.domain.LocationScope;
|
||||||
|
|
||||||
import com.google.common.base.Predicate;
|
import com.google.common.base.Predicate;
|
||||||
import com.google.common.base.Supplier;
|
import com.google.common.base.Supplier;
|
||||||
|
import com.google.common.collect.ImmutableSet;
|
||||||
|
import com.google.common.collect.ImmutableSet.Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If the current location id is null, then we don't care where to launch a node.
|
* If the current location id is null, then we don't care where to launch a node.
|
||||||
|
@ -42,33 +47,43 @@ public class LocationPredicate implements Predicate<ComputeMetadata> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean apply(ComputeMetadata input) {
|
public boolean apply(ComputeMetadata input) {
|
||||||
Location location = locationSupplier.get();
|
Location current = locationSupplier.get();
|
||||||
if (location == null)
|
if (current == null)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if (location.equals(input.getLocation()))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
checkArgument(
|
|
||||||
location.getParent() != null || location.getScope() == LocationScope.PROVIDER,
|
|
||||||
"only locations of scope PROVIDER can have a null parent; arg: %s",
|
|
||||||
location);
|
|
||||||
|
|
||||||
if (input.getLocation() == null)
|
if (input.getLocation() == null)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
Location parent = current.getParent();
|
||||||
|
checkArgument(
|
||||||
|
parent != null || current.getScope() == LocationScope.PROVIDER,
|
||||||
|
"only locations of scope PROVIDER can have a null parent; arg: %s",
|
||||||
|
current);
|
||||||
|
|
||||||
checkState(
|
checkState(
|
||||||
input.getLocation().getParent() != null || input.getLocation().getScope() == LocationScope.PROVIDER,
|
input.getLocation().getParent() != null || input.getLocation().getScope() == LocationScope.PROVIDER,
|
||||||
"only locations of scope PROVIDER can have a null parent; input: %s",
|
"only locations of scope PROVIDER can have a null parent; input: %s",
|
||||||
input.getLocation());
|
input.getLocation());
|
||||||
|
|
||||||
|
Builder<Predicate<Location>> predicates = ImmutableSet.<Predicate<Location>>builder();
|
||||||
|
|
||||||
|
predicates.add(equalTo(current));
|
||||||
|
|
||||||
return location.equals(input.getLocation()) || location.getParent() != null
|
if (parent != null) {
|
||||||
&& location.getParent().equals(input.getLocation()) || location.getParent().getParent() != null
|
predicates.add(equalTo(parent));
|
||||||
&& location.getParent().getParent().equals(input.getLocation());
|
|
||||||
|
Location grandparent = parent.getParent();
|
||||||
|
if (grandparent != null)
|
||||||
|
predicates.add(equalTo(grandparent));
|
||||||
|
}
|
||||||
|
|
||||||
|
return or(predicates.build()).apply(input.getLocation());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return locationSupplier.get() == null ? "anyLocation()" : "locationEqualsParentOrGrandparentOf(" + locationSupplier.get().getId() + ")";
|
// not calling .get() here, as it could accidentally cause eager api fetch
|
||||||
|
return "equalsParentOrGrandparentOfCurrentLocation()";
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -627,7 +627,7 @@ public class TemplateBuilderImplTest {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void testDefaultLocationWithUnmatchedPredicateExceptionMessage() {
|
public void testDefaultLocationWithUnmatchedPredicateExceptionMessageAndLocationNotCalled() {
|
||||||
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
|
||||||
.<Location> of());
|
.<Location> of());
|
||||||
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
|
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
|
||||||
|
@ -639,7 +639,6 @@ public class TemplateBuilderImplTest {
|
||||||
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
|
TemplateOptions defaultOptions = createMock(TemplateOptions.class);
|
||||||
|
|
||||||
expect(optionsProvider.get()).andReturn(defaultOptions);
|
expect(optionsProvider.get()).andReturn(defaultOptions);
|
||||||
expect(defaultLocation.getId()).andReturn("us-east-1");
|
|
||||||
|
|
||||||
replay(defaultOptions);
|
replay(defaultOptions);
|
||||||
replay(defaultLocation);
|
replay(defaultLocation);
|
||||||
|
@ -654,7 +653,7 @@ public class TemplateBuilderImplTest {
|
||||||
assert false;
|
assert false;
|
||||||
} catch (NoSuchElementException e) {
|
} catch (NoSuchElementException e) {
|
||||||
// make sure big data is not in the exception message
|
// make sure big data is not in the exception message
|
||||||
assertEquals(e.getMessage(), "no image matched predicate: And(locationEqualsParentOrGrandparentOf(us-east-1),imageDescription(description))");
|
assertEquals(e.getMessage(), "no image matched predicate: And(equalsParentOrGrandparentOfCurrentLocation(),imageDescription(description))");
|
||||||
}
|
}
|
||||||
|
|
||||||
verify(defaultOptions);
|
verify(defaultOptions);
|
||||||
|
|
Loading…
Reference in New Issue