| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.openstack.options; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | import static com.google.common.base.Preconditions.checkState; |
| 23 | |
| 24 | import java.util.Date; |
| 25 | |
| 26 | import org.jclouds.http.options.BaseHttpRequestOptions; |
| 27 | |
| 28 | /** |
| 29 | * Options used to control paginated results (aka list commands). |
| 30 | * |
| 31 | * @see <a href="http://docs.rackspacecloud.com/servers/api/cs-devguide-latest.pdf" /> |
| 32 | * @author Adrian Cole |
| 33 | */ |
| 34 | public class BaseListOptions extends BaseHttpRequestOptions { |
| 35 | public static final BaseListOptions NONE = new BaseListOptions(); |
| 36 | |
| 37 | /** |
| 38 | * Only return objects changed since this time. |
| 39 | */ |
| 40 | public BaseListOptions changesSince(Date ifModifiedSince) { |
| 41 | this.queryParameters.put("changes-since", checkNotNull(ifModifiedSince, "ifModifiedSince") |
| 42 | .getTime() |
| 43 | / 1000 + ""); |
| 44 | return this; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Indicates where to begin listing. The list will only include objects that occur after the |
| 49 | * offset. This is convenient for pagination: To get the next page of results use the last result |
| 50 | * number of the current page + current page offset as the offset. |
| 51 | */ |
| 52 | public BaseListOptions startAt(long offset) { |
| 53 | checkState(offset >= 0, "offset must be >= 0"); |
| 54 | queryParameters.put("offset", Long.toString(checkNotNull(offset, "offset"))); |
| 55 | return this; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * To reduce load on the service, list operations will return a maximum of 1,000 items at a time. |
| 60 | * To navigate the collection, the parameters limit and offset can be set in the URI |
| 61 | * (e.g.?limit=0&offset=0). If an offset is given beyond the end of a list an empty list will be |
| 62 | * returned. |
| 63 | * <p/> |
| 64 | * Note that list operations never return itemNotFound (404) faults. |
| 65 | */ |
| 66 | public BaseListOptions maxResults(int limit) { |
| 67 | checkState(limit >= 0, "limit must be >= 0"); |
| 68 | checkState(limit <= 10000, "limit must be <= 10000"); |
| 69 | queryParameters.put("limit", Integer.toString(limit)); |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | public static class Builder { |
| 74 | |
| 75 | /** |
| 76 | * @see BaseListOptions#startAt(long) |
| 77 | */ |
| 78 | public static BaseListOptions startAt(long prefix) { |
| 79 | BaseListOptions options = new BaseListOptions(); |
| 80 | return options.startAt(prefix); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @see BaseListOptions#maxResults(long) |
| 85 | */ |
| 86 | public static BaseListOptions maxResults(int maxKeys) { |
| 87 | BaseListOptions options = new BaseListOptions(); |
| 88 | return options.maxResults(maxKeys); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @see BaseListOptions#changesSince(Date) |
| 93 | */ |
| 94 | public static BaseListOptions changesSince(Date since) { |
| 95 | BaseListOptions options = new BaseListOptions(); |
| 96 | return options.changesSince(since); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | } |