View Javadoc
1   package ca.uhn.fhir.jpa.util;
2   
3   /*
4    * #%L
5    * HAPI FHIR JPA Server
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   * http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.apache.commons.lang3.Validate;
24  import org.hibernate.ScrollableResults;
25  
26  import java.util.Iterator;
27  
28  public class ScrollableResultsIterator<T extends Object> extends BaseIterator<T> implements Iterator<T> {
29  	private boolean hasNext;
30  	private T myNext;
31  	private ScrollableResults myScroll;
32  
33  	public ScrollableResultsIterator(ScrollableResults theScroll) {
34  		myScroll = theScroll;
35  	}
36  
37  	@SuppressWarnings("unchecked")
38  	private void ensureHaveNext() {
39  		if (myNext == null) {
40  			if (myScroll.next()) {
41  				hasNext = true;
42  				myNext = (T) myScroll.get(0);
43  			} else {
44  				hasNext = false;
45  			}
46  		}
47  	}
48  
49  	@Override
50  	public boolean hasNext() {
51  		ensureHaveNext();
52  		return hasNext;
53  	}
54  
55  	@Override
56  	public T next() {
57  		ensureHaveNext();
58  		Validate.isTrue(hasNext);
59  		T next = myNext;
60  		myNext = null;
61  		return next;
62  	}
63  }