View Javadoc

1   /**
2    *
3    * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com>
4    *
5    * ====================================================================
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   * ====================================================================
18   */
19  package org.jclouds.blobstore.options;
20  
21  import static com.google.common.base.Preconditions.checkArgument;
22  import static com.google.common.base.Preconditions.checkNotNull;
23  
24  /**
25   * Contains options supported in the list container operation. <h2>
26   * Usage</h2> The recommended way to instantiate a ListOptions object is to statically import
27   * ListOptions.* and invoke a static creation method followed by an instance mutator (if needed):
28   * <p/>
29   * <code>
30   * import static org.jclouds.blobstore.options.ListOptions.Builder.*
31   * <p/>
32   * BlobStore connection = // get connection
33   * Future<BoundedSortedSet<ResourceMetadata>> list = connection.list(maxResults(1000));
34   * <code>
35   * 
36   * @author Adrian Cole
37   */
38  public class ListOptions implements Cloneable {
39  
40     public static class ImmutableListOptions extends ListOptions {
41        private final ListOptions delegate;
42  
43        @Override
44        public ListContainerOptions afterMarker(String marker) {
45           throw new UnsupportedOperationException();
46        }
47  
48        public ImmutableListOptions(ListOptions delegate) {
49           this.delegate = delegate;
50        }
51  
52        @Override
53        public ListContainerOptions maxResults(int maxKeys) {
54           throw new UnsupportedOperationException();
55        }
56  
57        @Override
58        public String getMarker() {
59           return delegate.getMarker();
60        }
61  
62        @Override
63        public Integer getMaxResults() {
64           return delegate.getMaxResults();
65        }
66  
67        @Override
68        public ListOptions clone() {
69           return delegate.clone();
70        }
71  
72        @Override
73        public String toString() {
74           return delegate.toString();
75        }
76     }
77  
78     public static final ImmutableListOptions NONE = new ImmutableListOptions(new ListOptions());
79  
80     ListOptions(Integer maxKeys, String marker) {
81        this.maxKeys = maxKeys;
82        this.marker = marker;
83     }
84  
85     public ListOptions() {
86     }
87  
88     private Integer maxKeys;
89     private String marker;
90  
91     public Integer getMaxResults() {
92        return maxKeys;
93     }
94  
95     public String getMarker() {
96        return marker;
97     }
98  
99     /**
100     * Place to continue a listing at. This must be the value returned from the last list object, as
101     * not all blobstores use lexigraphic lists.
102     */
103    public ListOptions afterMarker(String marker) {
104       this.marker = checkNotNull(marker, "marker");
105       return this;
106    }
107 
108    /**
109     * The maximum number of values you'd like to see in the response body. The server might return
110     * fewer than this many values, but will not return more.
111     */
112    public ListOptions maxResults(int maxKeys) {
113       checkArgument(maxKeys >= 0, "maxKeys must be >= 0");
114       this.maxKeys = maxKeys;
115       return this;
116    }
117 
118    public static class Builder {
119 
120       /**
121        * @see ListOptions#afterMarker(String)
122        */
123       public static ListOptions afterMarker(String marker) {
124          ListOptions options = new ListOptions();
125          return options.afterMarker(marker);
126       }
127 
128       /**
129        * @see ListOptions#maxResults(int)
130        */
131       public static ListOptions maxResults(int maxKeys) {
132          ListOptions options = new ListOptions();
133          return options.maxResults(maxKeys);
134       }
135 
136    }
137 
138    @Override
139    protected ListOptions clone() {
140       return new ListOptions(maxKeys, marker);
141    }
142 }