This project has retired. For details please refer to its Attic page.
AbstractIterable xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF 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.apache.chemistry.opencmis.client.runtime.util;
20  
21  import org.apache.chemistry.opencmis.client.api.ItemIterable;
22  
23  /**
24   * Abstract <code>ItemIterable</code> implementation.
25   *
26   * @param <T> the type returned by the iterable's iterator
27   */
28  public abstract class AbstractIterable<T> implements ItemIterable<T> {
29  
30      private final AbstractPageFetcher<T> pageFetcher;
31      private final long skipCount;
32      private AbstractIterator<T> iterator;
33  
34      protected AbstractIterable(AbstractPageFetcher<T> pageFetcher) {
35          this(0, pageFetcher);
36      }
37  
38      protected AbstractIterable(long position, AbstractPageFetcher<T> pageFetcher) {
39          this.pageFetcher = pageFetcher;
40          this.skipCount = position;
41      }
42  
43      /**
44       * Gets the skip count
45       *
46       * @return  skip count
47       */
48      protected long getSkipCount() {
49          return skipCount;
50      }
51  
52      /**
53       * Gets the page fetcher
54       *
55       * @return  page fetcher
56       */
57      protected AbstractPageFetcher<T> getPageFetcher() {
58          return pageFetcher;
59      }
60  
61      /**
62       * Construct the iterator
63       *
64       * @return  iterator
65       */
66      protected abstract AbstractIterator<T> createIterator();
67  
68      public AbstractIterator<T> iterator() {
69          return getIterator();
70      }
71  
72      public ItemIterable<T> skipTo(long position) {
73          return new CollectionIterable<T>(position, pageFetcher);
74      }
75  
76      public ItemIterable<T> getPage() {
77          return new CollectionPageIterable<T>(skipCount, pageFetcher);
78      }
79  
80      public ItemIterable<T> getPage(int maxNumItems) {
81          this.pageFetcher.setMaxNumItems(maxNumItems);
82          return new CollectionPageIterable<T>(skipCount, pageFetcher);
83      }
84  
85      public long getPageNumItems() {
86          return getIterator().getPageNumItems();
87      }
88  
89      public boolean getHasMoreItems() {
90          return getIterator().getHasMoreItems();
91      }
92  
93      public long getTotalNumItems() {
94          return getIterator().getTotalNumItems();
95      }
96  
97      private AbstractIterator<T> getIterator() {
98          if (this.iterator == null) {
99              this.iterator = createIterator();
100         }
101         return this.iterator;
102     }
103 }
104