Issue 86: added base options for enumerated results

git-svn-id: http://jclouds.googlecode.com/svn/trunk@1874 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-09-03 04:25:23 +00:00
parent 4e05fcf529
commit 0333fb7c13
2 changed files with 125 additions and 0 deletions

View File

@ -0,0 +1,77 @@
package org.jclouds.azure.storage.options;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import org.jclouds.http.options.BaseHttpRequestOptions;
/**
* Options used to control paginated results (aka list commands).
*
* @see <a href="http://msdn.microsoft.com/en-us/library/dd179466.aspx" />
* @author Adrian Cole
*/
public class ListOptions extends BaseHttpRequestOptions {
public static final ListOptions NONE = new ListOptions();
/**
* Filters the results to return only objects whose name begins with the specified prefix.
*/
public ListOptions prefix(String prefix) {
this.queryParameters.put("prefix", checkNotNull(prefix, "prefix"));
return this;
}
/**
* A string value that identifies the portion of the list to be returned with the next list
* operation. The operation returns a marker value within the response body if the list returned
* was not complete. The marker value may then be used in a subsequent call to request the next
* set of list items.
* <p/>
* The marker value is opaque to the client.
*/
public ListOptions marker(String marker) {
this.queryParameters.put("marker", checkNotNull(marker, "marker"));
return this;
}
/**
* Specifies the maximum number of containers to return. If maxresults is not specified, the
* server will return up to 5,000 items. If the parameter is set to a value greater than 5,000,
* the server will return a Bad Request (400) error
*/
public ListOptions maxResults(int maxresults) {
checkState(maxresults >= 0, "maxresults must be >= 0");
checkState(maxresults <= 10000, "maxresults must be <= 5000");
queryParameters.put("maxresults", Integer.toString(maxresults));
return this;
}
public static class Builder {
/**
* @see ListOptions#prefix(String)
*/
public static ListOptions prefix(String prefix) {
ListOptions options = new ListOptions();
return options.prefix(prefix);
}
/**
* @see ListOptions#marker(String)
*/
public static ListOptions marker(String marker) {
ListOptions options = new ListOptions();
return options.marker(marker);
}
/**
* @see ListOptions#maxResults(long)
*/
public static ListOptions maxResults(int maxKeys) {
ListOptions options = new ListOptions();
return options.maxResults(maxKeys);
}
}
}

View File

@ -0,0 +1,48 @@
package org.jclouds.azure.storage.options;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
/**
* Tests behavior of {@code ListOptions}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "azurestorage.ListOptionsTest")
public class ListOptionsTest {
public void testPrefix() {
ListOptions options = new ListOptions().prefix("a");
assertEquals(ImmutableList.of("a"), options.buildQueryParameters().get("prefix"));
}
public void testMarker() {
ListOptions options = new ListOptions().marker("a");
assertEquals(ImmutableList.of("a"), options.buildQueryParameters().get("marker"));
}
public void testMaxResults() {
int limit = 1;
ListOptions options = new ListOptions().maxResults(limit);
assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("maxresults"));
}
public void testPrefixStatic() {
ListOptions options = ListOptions.Builder.prefix("a");
assertEquals(ImmutableList.of("a"), options.buildQueryParameters().get("prefix"));
}
public void testMarkerStatic() {
ListOptions options = ListOptions.Builder.marker("a");
assertEquals(ImmutableList.of("a"), options.buildQueryParameters().get("marker"));
}
public void testMaxResultsStatic() {
int limit = 1;
ListOptions options = ListOptions.Builder.maxResults(limit);
assertEquals(ImmutableList.of("1"), options.buildQueryParameters().get("maxresults"));
}
}