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 java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.chemistry.opencmis.client.runtime.util.AbstractPageFetcher.Page;
25  
26  /**
27   * Abstract {@code Iterator} implementation.
28   *
29   * @param <T>
30   *            the type returned by the iterator
31   */
32  public abstract class AbstractIterator<T> implements Iterator<T> {
33  
34      private long skipCount;
35      private int skipOffset;
36      private final AbstractPageFetcher<T> pageFetcher;
37  
38      private Page<T> page;
39      private Long totalNumItems;
40      private Boolean hasMoreItems;
41  
42      /**
43       * Constructor.
44       */
45      protected AbstractIterator(long skipCount, AbstractPageFetcher<T> pageFetcher) {
46          this.skipCount = skipCount;
47          this.pageFetcher = pageFetcher;
48      }
49  
50      public long getPosition() {
51          return skipCount + skipOffset;
52      }
53  
54      public long getPageNumItems() {
55          Page<T> currentPage = getCurrentPage();
56          if (currentPage != null) {
57              List<T> items = currentPage.getItems();
58              if (items != null) {
59                  return items.size();
60              }
61          }
62          return 0L;
63      }
64  
65      public long getTotalNumItems() {
66          if (totalNumItems == null) {
67              totalNumItems = Long.valueOf(-1);
68              Page<T> currentPage = getCurrentPage();
69              if (currentPage != null) {
70                  // set number of items
71                  if (currentPage.getTotalNumItems() != null) {
72                      totalNumItems = currentPage.getTotalNumItems();
73                  }
74              }
75          }
76          return totalNumItems.longValue();
77      }
78  
79      public boolean getHasMoreItems() {
80          if (hasMoreItems == null) {
81              hasMoreItems = Boolean.FALSE;
82              Page<T> currentPage = getCurrentPage();
83              if (currentPage != null) {
84                  if (currentPage.getHasMoreItems() != null) {
85                      hasMoreItems = currentPage.getHasMoreItems();
86                  }
87              }
88          }
89          return hasMoreItems.booleanValue();
90      }
91  
92      @Override
93      public void remove() {
94          throw new UnsupportedOperationException();
95      }
96  
97      /**
98       * Gets current skip count
99       *
100      * @return skip count
101      */
102     protected long getSkipCount() {
103         return skipCount;
104     }
105 
106     /**
107      * Gets current skip offset (from skip count)
108      *
109      * @return skip offset
110      */
111     protected int getSkipOffset() {
112         return skipOffset;
113     }
114 
115     /**
116      * Increment the skip offset by one
117      *
118      * @return incremented skip offset
119      */
120     protected int incrementSkipOffset() {
121         return skipOffset++;
122     }
123 
124     /**
125      * Gets the current page of items within collection
126      *
127      * @return current page
128      */
129     protected Page<T> getCurrentPage() {
130         if (page == null) {
131             page = pageFetcher.fetchPage(skipCount);
132         }
133         return page;
134     }
135 
136     /**
137      * Skip to the next page of items within collection
138      *
139      * @return next page
140      */
141     protected Page<T> incrementPage() {
142         skipCount += skipOffset;
143         skipOffset = 0;
144         totalNumItems = null;
145         hasMoreItems = null;
146         page = pageFetcher.fetchPage(skipCount);
147         return page;
148     }
149 
150 }