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