utils and better toStrings for location classes

This commit is contained in:
Adrian Cole 2012-07-29 16:27:39 -07:00
parent 7a23930df6
commit ba56e2dce4
9 changed files with 143 additions and 21 deletions

View File

@ -22,6 +22,7 @@ import static com.google.common.base.Objects.equal;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Arrays;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
@ -134,6 +135,7 @@ public class ClassMethodArgs {
} }
protected ToStringHelper string() { 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);
} }
} }

View File

@ -145,7 +145,12 @@ public class LocationModule extends AbstractModule {
public Set<String> apply(Set<String> input) { public Set<String> apply(Set<String> input) {
return Sets.filter(input, filter); return Sets.filter(input, filter);
} }
@Override
public String toString() {
return "filterStrings(" + filter + ")";
}
} }
@Provides @Provides

View File

@ -52,5 +52,10 @@ public class AnyOrConfiguredRegionId implements RegionIdFilter {
return true; return true;
return idsInConfig.contains(input); return idsInConfig.contains(input);
} }
@Override
public String toString() {
return "anyOrConfiguredRegionId(" + idsInConfigSupplier + ")";
}
} }

View File

@ -52,5 +52,10 @@ public class AnyOrConfiguredZoneId implements ZoneIdFilter {
return true; return true;
return idsInConfig.contains(input); return idsInConfig.contains(input);
} }
@Override
public String toString() {
return "anyOrConfiguredZoneId(" + idsInConfigSupplier + ")";
}
} }

View File

@ -20,6 +20,7 @@ package org.jclouds.location.suppliers;
import java.net.URI; import java.net.URI;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
import org.jclouds.javax.annotation.Nullable; import org.jclouds.javax.annotation.Nullable;
import org.jclouds.location.suppliers.fromconfig.RegionIdToURIFromConfigurationOrDefaultToProvider; import org.jclouds.location.suppliers.fromconfig.RegionIdToURIFromConfigurationOrDefaultToProvider;
@ -44,9 +45,10 @@ public interface RegionIdToURISupplier extends Supplier<Map<String, Supplier<URI
* @param apiVersion * @param apiVersion
* version of the api, or null if not available * version of the api, or null if not available
* @return regions mapped to default uri * @return regions mapped to default uri
* @throws NoSuchElementException if the {@code apiType} is not present in the catalog
*/ */
RegionIdToURISupplier createForApiTypeAndVersion(@Assisted("apiType") String apiType, RegionIdToURISupplier createForApiTypeAndVersion(@Assisted("apiType") String apiType,
@Nullable @Assisted("apiVersion") String apiVersion); @Nullable @Assisted("apiVersion") String apiVersion) throws NoSuchElementException;
} }
} }

View File

@ -60,4 +60,9 @@ public class SplitConfigurationKey implements Supplier<Set<String>> {
} }
} }
@Override
public String toString() {
return "splitConfigurationKey(" + configKey + ")";
}
} }

View File

@ -48,6 +48,7 @@ import java.util.Comparator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.NoSuchElementException;
import java.util.Set; import java.util.Set;
import java.util.SortedSet; import java.util.SortedSet;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
@ -435,6 +436,11 @@ public class RestAnnotationProcessor<T> {
requestBuilder.method(getHttpMethodOrConstantOrThrowException(method)); 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) requestBuilder.declaring(declaring)
.javaMethod(method) .javaMethod(method)
.args(args) .args(args)

View File

@ -25,11 +25,13 @@ import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map; import java.util.Map;
import com.google.common.annotations.Beta;
import com.google.common.base.Function; import com.google.common.base.Function;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.io.OutputSupplier; import com.google.common.io.OutputSupplier;
@ -44,7 +46,8 @@ public class Suppliers2 {
@Override @Override
public V apply(Map<K, Supplier<V>> input) { 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 @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! // only here until guava compose gives a toString!
// http://code.google.com/p/guava-libraries/issues/detail?id=1052 // 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) { public static <F, T> Supplier<T> compose(Function<? super F, T> function, Supplier<F> supplier) {

View File

@ -18,25 +18,13 @@
*/ */
package org.jclouds.util; package org.jclouds.util;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.testng.Assert.assertEquals; 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.Map;
import java.util.concurrent.TimeUnit; import java.util.NoSuchElementException;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
@ -45,15 +33,66 @@ public class Suppliers2Test {
@Test @Test
public void testGetLastValueInMap() { public void testGetLastValueInMap() {
assertEquals(Suppliers2 assertEquals(
.<String, String> getLastValueInMap( Suppliers2.<String, String> getLastValueInMap(
Suppliers.<Map<String, Supplier<String>>> ofInstance(ImmutableMap.of("foo", Suppliers Suppliers.<Map<String, Supplier<String>>> ofInstance(ImmutableMap.of("foo",
.ofInstance("bar")))).get(), "bar"); Suppliers.ofInstance("bar")))).get(), "bar");
} }
@Test @Test
public void testOfInstanceFunction() { public void testOfInstanceFunction() {
assertEquals(Suppliers2.ofInstanceFunction().apply("foo").get(), "foo"); 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");
}
} }