added function to grab first arg during pagination

This commit is contained in:
Adrian Cole 2013-02-24 12:34:00 -08:00
parent bbc35cb7e1
commit 2f78f30a47
5 changed files with 291 additions and 22 deletions

View File

@ -52,17 +52,22 @@ public class PagedIterables {
*
* @return iterable with only the one page
*/
public static <T> PagedIterable<T> of(final IterableWithMarker<T> only) {
public static <T> PagedIterable<T> onlyPage(final IterableWithMarker<T> only) {
return new PagedIterable<T>() {
@Override
public Iterator<IterableWithMarker<T>> iterator() {
return ImmutableSet.of(only).iterator();
}
};
}
/**
* @see #onlyPage(IterableWithMarker)
*/
@Deprecated
public static <T> PagedIterable<T> of(IterableWithMarker<T> only) {
return onlyPage(only);
}
/**
*
*
@ -77,12 +82,9 @@ public class PagedIterables {
public static <T> PagedIterable<T> advance(final IterableWithMarker<T> initial,
final Function<Object, IterableWithMarker<T>> markerToNext) {
return new PagedIterable<T>() {
@Override
public Iterator<IterableWithMarker<T>> iterator() {
return advancingIterator(initial, markerToNext);
}
};
}
@ -97,9 +99,6 @@ public class PagedIterables {
this.markerToNext = checkNotNull(markerToNext, "marker to next iterable");
}
/**
* {@inheritDoc}
*/
@Override
protected IterableWithMarker<T> computeNext() {
if (unread)
@ -114,17 +113,11 @@ public class PagedIterables {
return endOfData();
}
/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Objects.hashCode(current, unread);
}
/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
@ -135,9 +128,6 @@ public class PagedIterables {
return Objects.equal(this.current, other.current) && Objects.equal(this.unread, other.unread);
}
/**
* {@inheritDoc}
*/
@Override
public String toString() {
return Objects.toStringHelper("").add("current", current).add("unread", unread).toString();
@ -161,5 +151,4 @@ public class PagedIterables {
}
return new AdvancingIterator<T>(initial, markerToNext);
}
}

View File

@ -0,0 +1,109 @@
/**
* 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.internal;
/**
* 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.
*/
import static org.jclouds.collect.PagedIterables.advance;
import static org.jclouds.collect.PagedIterables.onlyPage;
import java.util.Arrays;
import java.util.List;
import org.jclouds.collect.IterableWithMarker;
import org.jclouds.collect.PagedIterable;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.InvocationContext;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import com.google.common.annotations.Beta;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
/**
* Used to propagate {@code arg0} during an advance in a {@link PagedIterable}. For example, in the call
* {@code api.getUserApi().listPathPrefix("/users")}, the arg0 is the value {@code "/users"}.
*
* @author Adrian Cole
*/
@Beta
public abstract class Arg0ToPagedIterable<T, I extends Arg0ToPagedIterable<T, I>> implements
Function<IterableWithMarker<T>, PagedIterable<T>>, InvocationContext<I> {
private GeneratedHttpRequest request;
@Override
public PagedIterable<T> apply(IterableWithMarker<T> input) {
if (!input.nextMarker().isPresent())
return onlyPage(input);
List<Object> args = getArgs(request);
Optional<Object> arg0 = Optional.fromNullable(args.size() > 0 ? args.get(0) : null);
return advance(input, markerToNextForArg0(arg0));
}
protected List<Object> getArgs(GeneratedHttpRequest request) {
return request.getArgs();
}
/**
* @param arg0
* present when there was an arg0
*/
protected abstract Function<Object, IterableWithMarker<T>> markerToNextForArg0(Optional<Object> arg0);
@SuppressWarnings("unchecked")
@Override
public I setContext(HttpRequest request) {
this.request = GeneratedHttpRequest.class.cast(request);
return (I) this;
}
/**
* Used to propagate caller {@code arg0} to a callee during an advance in a {@link PagedIterable}. For example, in
* the call {@code api.getUserApiForZone(zone).list()}, the caller arg0 is the value of {@code zone}, and the callee
* is {@code UserApi.list()}
*
* @author Adrian Cole
*/
public static abstract class FromCaller<T, I extends FromCaller<T, I>> extends Arg0ToPagedIterable<T, I> {
@Override
protected List<Object> getArgs(GeneratedHttpRequest request) {
Object[] args = request.getCaller().get().getArgs();
return args != null ? Arrays.asList(args) : ImmutableList.of();
}
}
}

View File

@ -50,10 +50,12 @@ import com.google.common.base.Optional;
/**
* @author Adrian Cole
* @deprecated Arg0ToPagedIterable.FromCaller
*/
@Beta
@Deprecated
public abstract class CallerArg0ToPagedIterable<T, I extends CallerArg0ToPagedIterable<T, I>> implements
Function<IterableWithMarker<T>, PagedIterable<T>>, InvocationContext<I> {
Function<IterableWithMarker<T>, PagedIterable<T>>, InvocationContext<I> {
private GeneratedHttpRequest request;

View File

@ -74,5 +74,4 @@ public class PagedIterablesTest {
EasyMock.verify(markerToNext);
}
}

View File

@ -0,0 +1,170 @@
/**
* 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 current 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.internal;
import static com.google.common.base.Throwables.propagate;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import org.jclouds.collect.IterableWithMarker;
import org.jclouds.collect.IterableWithMarkers;
import org.jclouds.internal.ClassMethodArgs;
import org.jclouds.rest.internal.GeneratedHttpRequest;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
/**
* @author Adrian Cole
*/
@Test(testName = "Arg0ToPagedIterableTest")
public class Arg0ToPagedIterableTest {
private static abstract class TestArg0 extends Arg0ToPagedIterable<String, TestArg0> {
private TestArg0(GeneratedHttpRequest in) {
this.setContext(in);
}
}
@Test
public void testWhenNextMarkerAbsentDoesntAdvance() {
GeneratedHttpRequest request = args(ImmutableList.of());
TestArg0 converter = new TestArg0(request) {
@Override
protected Function<Object, IterableWithMarker<String>> markerToNextForArg0(Optional<Object> arg0) {
fail();
return null;
}
};
assertEquals(converter.apply(IterableWithMarkers.from(ImmutableSet.of("foo", "bar"))).concat().toImmutableSet(),
ImmutableSet.of("foo", "bar"));
}
@Test
public void testWhenNextMarkerPresentButNoArgsMarkerToNextForArg0ParamIsAbsent() {
GeneratedHttpRequest request = args(ImmutableList.of());
final IterableWithMarker<String> next = IterableWithMarkers.from(ImmutableSet.of("baz"));
TestArg0 converter = new TestArg0(request) {
@Override
protected Function<Object, IterableWithMarker<String>> markerToNextForArg0(Optional<Object> arg0) {
assertEquals(arg0, Optional.absent());
return Functions.constant(next);
}
};
assertEquals(converter.apply(IterableWithMarkers.from(ImmutableSet.of("foo", "bar"), "marker")).concat().toImmutableSet(),
ImmutableSet.of("foo", "bar", "baz"));
}
@Test
public void testWhenNextMarkerPresentWithArgsMarkerToNextForArg0ParamIsPresent() {
GeneratedHttpRequest request = args(ImmutableList.<Object> of("path"));
final IterableWithMarker<String> next = IterableWithMarkers.from(ImmutableSet.of("baz"));
TestArg0 converter = new TestArg0(request) {
@Override
protected Function<Object, IterableWithMarker<String>> markerToNextForArg0(Optional<Object> arg0) {
assertEquals(arg0, Optional.of("path"));
return Functions.constant(next);
}
};
assertEquals(converter.apply(IterableWithMarkers.from(ImmutableSet.of("foo", "bar"), "marker")).concat().toImmutableSet(),
ImmutableSet.of("foo", "bar", "baz"));
}
private GeneratedHttpRequest args(ImmutableList<Object> args) {
try {
return GeneratedHttpRequest.builder().method("GET").endpoint("http://localhost")
.declaring(String.class).javaMethod(String.class.getMethod("toString")).args(args).build();
} catch (Exception e) {
throw propagate(e);
}
}
private static abstract class TestCallerArg0 extends Arg0ToPagedIterable.FromCaller<String, TestCallerArg0> {
private TestCallerArg0(GeneratedHttpRequest in) {
this.setContext(in);
}
}
@Test
public void testFromCallerWhenNextMarkerPresentButNoArgsMarkerToNextForArg0ParamIsAbsent() {
GeneratedHttpRequest request = callerArgs(ImmutableList.of());
final IterableWithMarker<String> next = IterableWithMarkers.from(ImmutableSet.of("baz"));
TestCallerArg0 converter = new TestCallerArg0(request) {
@Override
protected Function<Object, IterableWithMarker<String>> markerToNextForArg0(Optional<Object> arg0) {
assertEquals(arg0, Optional.absent());
return Functions.constant(next);
}
};
assertEquals(converter.apply(IterableWithMarkers.from(ImmutableSet.of("foo", "bar"), "marker")).concat().toImmutableSet(),
ImmutableSet.of("foo", "bar", "baz"));
}
@Test
public void testFromCallerWhenNextMarkerPresentWithArgsMarkerToNextForArg0ParamIsPresent() {
GeneratedHttpRequest request = callerArgs(ImmutableList.<Object> of("path"));
final IterableWithMarker<String> next = IterableWithMarkers.from(ImmutableSet.of("baz"));
TestCallerArg0 converter = new TestCallerArg0(request) {
@Override
protected Function<Object, IterableWithMarker<String>> markerToNextForArg0(Optional<Object> arg0) {
assertEquals(arg0, Optional.of("path"));
return Functions.constant(next);
}
};
assertEquals(converter.apply(IterableWithMarkers.from(ImmutableSet.of("foo", "bar"), "marker")).concat().toImmutableSet(),
ImmutableSet.of("foo", "bar", "baz"));
}
private GeneratedHttpRequest callerArgs(ImmutableList<Object> args) {
try {
return GeneratedHttpRequest.builder().method("GET").endpoint("http://localhost")
.declaring(String.class).javaMethod(String.class.getMethod("toString"))
.caller(ClassMethodArgs.builder()
.clazz(String.class)
.method(String.class.getMethod("toString"))
.args(args.toArray()).build()).build();
} catch (Exception e) {
throw propagate(e);
}
}
}