-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CachingIterator.java
261 lines (245 loc) · 7.63 KB
/
CachingIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.helpers.collection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.NoSuchElementException;
/**
* An {@link Iterator} which lazily fetches and caches items from the
* underlying iterator when items are requested. This enables positioning
* as well as going backwards through the iteration.
* @author Mattias Persson
*
* @param <T> the type of items in the iterator.
*/
public class CachingIterator<T> implements ListIterator<T>
{
private final Iterator<T> source;
private final List<T> visited = new ArrayList<T>();
private int position;
private T current;
/**
* Creates a new caching iterator using {@code source} as its underlying
* {@link Iterator} to get items lazily from.
* @param source the underlying {@link Iterator} to lazily get items from.
*/
public CachingIterator( Iterator<T> source )
{
this.source = source;
}
/**
* Returns whether a call to {@link #next()} will be able to return
* an item or not. If the current {@link #position()} is beyond the size
* of the cache (as will be the case if only calls to
* {@link #hasNext()}/{@link #next()} has been made up to this point)
* the underlying iterator is asked, else {@code true} since it can be
* returned from the cache.
*
* @return whether or not there are more items in this iteration given the
* current {@link #position()}.
*/
@Override
public boolean hasNext()
{
return visited.size() > position || source.hasNext();
}
/**
* Returns the next item given the current {@link #position()}.
* If the current {@link #position()} is beyond the size
* of the cache (as will be the case if only calls to
* {@link #hasNext()}/{@link #next()} has been made up to this point) the
* underlying iterator is asked for the next item (and cached if there
* was one), else the item is returned from the cache.
*
* @return the next item given the current {@link #position()}.
*/
@Override
public T next()
{
if ( visited.size() > position )
{
current = visited.get( position );
}
else
{
if ( !source.hasNext() )
{
throw new NoSuchElementException();
}
current = source.next();
visited.add( current );
}
position++;
return current;
}
/**
* Returns the current position of the iterator, initially 0. The position
* represents the index of the item which will be returned by the next call
* to {@link #next()} and also the index of the next item returned by
* {@link #previous()} plus one. An example:
*
* <ul>
* <li>Instantiate an iterator which would iterate over the strings "first", "second" and "third".</li>
* <li>Get the two first items ("first" and "second") from it by using {@link #next()},
* {@link #position()} will now return 2.</li>
* <li>Call {@link #previous()} (which will return "second") and {@link #position()} will now be 1</li>
* </ul>
*
* @return the position of the iterator.
*/
public int position()
{
return position;
}
/**
* Sets the position of the iterator. {@code 0} means all the way back to
* the beginning. It is also possible to set the position to one higher
* than the last item, so that the next call to {@link #previous()} would
* return the last item. Items will be cached along the way if necessary.
*
* @param newPosition the position to set for the iterator, must be
* non-negative.
* @return the position before changing to the new position.
*/
public int position( int newPosition )
{
if ( newPosition < 0 )
{
throw new IllegalArgumentException( "Position must be non-negative, was " + newPosition );
}
int previousPosition = position;
boolean overTheEdge = false;
while ( visited.size() < newPosition )
{
T next = source.hasNext() ? source.next() : null;
if ( next != null )
{
visited.add( next );
}
else
{
if ( !overTheEdge )
{
overTheEdge = true;
}
else
{
throw new NoSuchElementException( "Requested position " + newPosition +
", but didn't get further than to " + visited.size() );
}
}
}
current = null;
position = newPosition;
return previousPosition;
}
/**
* Returns whether or not a call to {@link #previous()} will be able to
* return an item or not. So it will return {@code true} if
* {@link #position()} is bigger than 0.
*
* {@inheritDoc}
*/
@Override
public boolean hasPrevious()
{
return position > 0;
}
/**
* {@inheritDoc}
*/
@Override
public T previous()
{
if ( !hasPrevious() )
{
throw new NoSuchElementException( "Position is " + position );
}
current = visited.get( --position );
return current;
}
/**
* Returns the last item returned by {@link #next()}/{@link #previous()}.
* If no call has been made to {@link #next()} or {@link #previous()} since
* this iterator was created or since a call to {@link #position(int)} has
* been made a {@link NoSuchElementException} will be thrown.
*
* @return the last item returned by {@link #next()}/{@link #previous()}.
* @throws NoSuchElementException if no call has been made to {@link #next()}
* or {@link #previous()} since this iterator was created or since a call to
* {@link #position(int)} has been made.
*/
public T current()
{
if ( current == null )
{
throw new NoSuchElementException();
}
return current;
}
/**
* {@inheritDoc}
*/
@Override
public int nextIndex()
{
return position;
}
/**
* {@inheritDoc}
*/
@Override
public int previousIndex()
{
return position - 1;
}
/**
* Not supported by this implement.
*
* {@inheritDoc}
*/
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
/**
* Not supported by this implement.
*
* {@inheritDoc}
*/
@Override
public void set( T e )
{
throw new UnsupportedOperationException();
}
/**
* Not supported by this implement.
*
* {@inheritDoc}
*/
@Override
public void add( T e )
{
throw new UnsupportedOperationException();
}
}