mirror of https://github.com/apache/jclouds.git
utils and better toStrings for location classes
This commit is contained in:
parent
7a23930df6
commit
ba56e2dce4
|
@ -22,6 +22,7 @@ import static com.google.common.base.Objects.equal;
|
|||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.jclouds.javax.annotation.Nullable;
|
||||
|
||||
|
@ -134,6 +135,7 @@ public class ClassMethodArgs {
|
|||
}
|
||||
|
||||
protected ToStringHelper string() {
|
||||
return Objects.toStringHelper("").add("clazz", clazz).add("method", method).add("args", args);
|
||||
return Objects.toStringHelper("").omitNullValues().add("clazz", clazz).add("method", method)
|
||||
.add("args", args != null ? Arrays.asList(args) : null);
|
||||
}
|
||||
}
|
|
@ -146,6 +146,11 @@ public class LocationModule extends AbstractModule {
|
|||
return Sets.filter(input, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "filterStrings(" + filter + ")";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
|
|
@ -53,4 +53,9 @@ public class AnyOrConfiguredRegionId implements RegionIdFilter {
|
|||
return idsInConfig.contains(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "anyOrConfiguredRegionId(" + idsInConfigSupplier + ")";
|
||||
}
|
||||
|
||||
}
|
|
@ -53,4 +53,9 @@ public class AnyOrConfiguredZoneId implements ZoneIdFilter {
|
|||
return idsInConfig.contains(input);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "anyOrConfiguredZoneId(" + idsInConfigSupplier + ")";
|
||||
}
|
||||
|
||||
}
|
|
@ -20,6 +20,7 @@ package org.jclouds.location.suppliers;
|
|||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.jclouds.javax.annotation.Nullable;
|
||||
import org.jclouds.location.suppliers.fromconfig.RegionIdToURIFromConfigurationOrDefaultToProvider;
|
||||
|
@ -44,9 +45,10 @@ public interface RegionIdToURISupplier extends Supplier<Map<String, Supplier<URI
|
|||
* @param apiVersion
|
||||
* version of the api, or null if not available
|
||||
* @return regions mapped to default uri
|
||||
* @throws NoSuchElementException if the {@code apiType} is not present in the catalog
|
||||
*/
|
||||
RegionIdToURISupplier createForApiTypeAndVersion(@Assisted("apiType") String apiType,
|
||||
@Nullable @Assisted("apiVersion") String apiVersion);
|
||||
@Nullable @Assisted("apiVersion") String apiVersion) throws NoSuchElementException;
|
||||
}
|
||||
|
||||
}
|
|
@ -60,4 +60,9 @@ public class SplitConfigurationKey implements Supplier<Set<String>> {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "splitConfigurationKey(" + configKey + ")";
|
||||
}
|
||||
|
||||
}
|
|
@ -48,6 +48,7 @@ import java.util.Comparator;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.SortedSet;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
@ -435,6 +436,11 @@ public class RestAnnotationProcessor<T> {
|
|||
requestBuilder.method(getHttpMethodOrConstantOrThrowException(method));
|
||||
}
|
||||
|
||||
if (endpoint == null) {
|
||||
throw new NoSuchElementException(String.format("no endpoint found for %s",
|
||||
new ClassMethodArgs(method.getDeclaringClass(), method, args)));
|
||||
}
|
||||
|
||||
requestBuilder.declaring(declaring)
|
||||
.javaMethod(method)
|
||||
.args(args)
|
||||
|
|
|
@ -25,11 +25,13 @@ import java.io.OutputStream;
|
|||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
|
||||
import com.google.common.annotations.Beta;
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Objects;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.base.Throwables;
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.io.OutputSupplier;
|
||||
|
||||
|
@ -44,7 +46,8 @@ public class Suppliers2 {
|
|||
|
||||
@Override
|
||||
public V apply(Map<K, Supplier<V>> input) {
|
||||
return Iterables.getLast(input.values()).get();
|
||||
Supplier<V> last = Iterables.getLast(input.values());
|
||||
return last.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -82,6 +85,56 @@ public class Suppliers2 {
|
|||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the value of the first supplier, or the value of the fallback, if the unlessNull is
|
||||
* null.
|
||||
*/
|
||||
@Beta
|
||||
public static <T> Supplier<T> or(final Supplier<T> unlessNull, final Supplier<T> fallback) {
|
||||
return new Supplier<T>() {
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
T val = unlessNull.get();
|
||||
if (val != null)
|
||||
return val;
|
||||
return fallback.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(this).add("unlessNull", unlessNull).add("fallback", fallback).toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* if a throwable of certain type is encountered on getting the first value, use the fallback.
|
||||
*/
|
||||
@Beta
|
||||
public static <T, X extends Throwable> Supplier<T> onThrowable(final Supplier<T> unlessThrowable,
|
||||
final Class<X> throwable, final Supplier<T> fallback) {
|
||||
return new Supplier<T>() {
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
try {
|
||||
return unlessThrowable.get();
|
||||
} catch (Throwable t) {
|
||||
if (Throwables2.getFirstThrowableOfType(t, throwable) != null)
|
||||
return fallback.get();
|
||||
throw Throwables.propagate(t);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(this).add("unlessThrowable", unlessThrowable)
|
||||
.add("throwable", throwable.getSimpleName()).add("fallback", fallback).toString();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// only here until guava compose gives a toString!
|
||||
// http://code.google.com/p/guava-libraries/issues/detail?id=1052
|
||||
public static <F, T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier) {
|
||||
|
|
|
@ -18,25 +18,13 @@
|
|||
*/
|
||||
package org.jclouds.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertSame;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Supplier;
|
||||
import com.google.common.base.Suppliers;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
|
@ -45,10 +33,10 @@ public class Suppliers2Test {
|
|||
|
||||
@Test
|
||||
public void testGetLastValueInMap() {
|
||||
assertEquals(Suppliers2
|
||||
.<String, String> getLastValueInMap(
|
||||
Suppliers.<Map<String, Supplier<String>>> ofInstance(ImmutableMap.of("foo", Suppliers
|
||||
.ofInstance("bar")))).get(), "bar");
|
||||
assertEquals(
|
||||
Suppliers2.<String, String> getLastValueInMap(
|
||||
Suppliers.<Map<String, Supplier<String>>> ofInstance(ImmutableMap.of("foo",
|
||||
Suppliers.ofInstance("bar")))).get(), "bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -56,4 +44,55 @@ public class Suppliers2Test {
|
|||
assertEquals(Suppliers2.ofInstanceFunction().apply("foo").get(), "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrWhenFirstNull() {
|
||||
assertEquals(Suppliers2.or(Suppliers.<String> ofInstance(null), Suppliers.ofInstance("foo")).get(), "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOrWhenFirstNotNull() {
|
||||
assertEquals(Suppliers2.or(Suppliers.<String> ofInstance("foo"), Suppliers.ofInstance("bar")).get(), "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnThrowableWhenFirstThrowsMatchingException() {
|
||||
assertEquals(Suppliers2.onThrowable(new Supplier<String>() {
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
}, NoSuchElementException.class, Suppliers.ofInstance("foo")).get(), "foo");
|
||||
}
|
||||
|
||||
@Test(expectedExceptions = RuntimeException.class)
|
||||
public void testOnThrowableWhenFirstThrowsUnmatchingException() {
|
||||
Suppliers2.onThrowable(new Supplier<String>() {
|
||||
|
||||
@Override
|
||||
public String get() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
}, NoSuchElementException.class, Suppliers.ofInstance("foo")).get();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnThrowableWhenFirstIsFine() {
|
||||
assertEquals(
|
||||
Suppliers2.onThrowable(Suppliers.<String> ofInstance("foo"), NoSuchElementException.class,
|
||||
Suppliers.ofInstance("bar")).get(), "foo");
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCombination() {
|
||||
Supplier<String> alternate = Suppliers.ofInstance("bar");
|
||||
Supplier<String> or = Suppliers2.or(Suppliers.<String> ofInstance("foo"), alternate);
|
||||
Supplier<String> combined = Suppliers2.onThrowable(or, NoSuchElementException.class, alternate);
|
||||
|
||||
assertEquals(combined.get(), "foo");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue