View Javadoc

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      @Override
69      public AbstractIterator<T> iterator() {
70          return getIterator();
71      }
72  
73      @Override
74      public ItemIterable<T> skipTo(long position) {
75          return new CollectionIterable<T>(position, pageFetcher);
76      }
77  
78      @Override
79      public ItemIterable<T> getPage() {
80          return new CollectionPageIterable<T>(skipCount, pageFetcher);
81      }
82  
83      @Override
84      public ItemIterable<T> getPage(int maxNumItems) {
85          this.pageFetcher.setMaxNumItems(maxNumItems);
86          return new CollectionPageIterable<T>(skipCount, pageFetcher);
87      }
88  
89      @Override
90      public long getPageNumItems() {
91          return getIterator().getPageNumItems();
92      }
93  
94      @Override
95      public boolean getHasMoreItems() {
96          return getIterator().getHasMoreItems();
97      }
98  
99      @Override
100     public long getTotalNumItems() {
101         return getIterator().getTotalNumItems();
102     }
103 
104     private AbstractIterator<T> getIterator() {
105         if (this.iterator == null) {
106             this.iterator = createIterator();
107         }
108         return this.iterator;
109     }
110 }
111