001: // ============================================================================
002: // $Id: LookAheadIterator.java,v 1.19 2005/08/02 23:45:22 davidahall Exp $
003: // Copyright (c) 2003-2005 David A. Hall
004: // ============================================================================
005: // The contents of this file are subject to the Common Development and
006: // Distribution License (CDDL), Version 1.0 (the License); you may not use this
007: // file except in compliance with the License. You should have received a copy
008: // of the the License along with this file: if not, a copy of the License is
009: // available from Sun Microsystems, Inc.
010: //
011: // http://www.sun.com/cddl/cddl.html
012: //
013: // From time to time, the license steward (initially Sun Microsystems, Inc.) may
014: // publish revised and/or new versions of the License. You may not use,
015: // distribute, or otherwise make this file available under subsequent versions
016: // of the License.
017: //
018: // Alternatively, the contents of this file may be used under the terms of the
019: // GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
020: // case the provisions of the LGPL are applicable instead of those above. If you
021: // wish to allow use of your version of this file only under the terms of the
022: // LGPL, and not to allow others to use your version of this file under the
023: // terms of the CDDL, indicate your decision by deleting the provisions above
024: // and replace them with the notice and other provisions required by the LGPL.
025: // If you do not delete the provisions above, a recipient may use your version
026: // of this file under the terms of either the CDDL or the LGPL.
027: //
028: // This library is distributed in the hope that it will be useful,
029: // but WITHOUT ANY WARRANTY; without even the implied warranty of
030: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
031: // ============================================================================
032:
033: package net.sf.jga.util;
034:
035: import java.lang.reflect.Array;
036: import java.util.ArrayList;
037: import java.util.NoSuchElementException;
038: import java.util.Iterator;
039:
040: /**
041: * Iterator that allows the program to look at and operate on the next few
042: * elements without consuming them.
043: * <p>
044: * Copyright © 2003-2005 David A. Hall
045: *
046: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
047: */
048:
049: public class LookAheadIterator<T> implements Iterator<T>, Iterable<T> {
050: // The base iterator
051: private Iterator<? extends T> _base;
052:
053: // ring buffer, to hold the elements that have been read ahead of the
054: // current position.
055: private Object[] _buffer;
056:
057: // index of the next element to read from the buffer
058: private int _readptr = 0;
059:
060: // index of the next element to write to the buffer
061: private int _writeptr = 0;
062:
063: // number of elements in the buffer
064: private int _cnt = 0;
065:
066: // size of the buffer
067: private int _size;
068:
069: /**
070: * Builds a LookAheadIterator that can look ahead 1 element.
071: */
072:
073: public LookAheadIterator(Iterator<? extends T> base) {
074: this (base, 1);
075: }
076:
077: /**
078: * Builds a LookAheadIterator that can look ahead the given number of
079: * elements.
080: *
081: * @throws IllegalArgumentException if max <= 0.
082: */
083:
084: public LookAheadIterator(Iterator<? extends T> base, int max) {
085: if (max <= 0)
086: throw new IllegalArgumentException();
087:
088: _base = (base != null) ? base : new EmptyIterator<T>();
089: _size = max;
090:
091: _buffer = new Object[_size];
092: }
093:
094: /**
095: * Returns true if there is an element at the Nth position. Put another
096: * way, returns true if there are enough elements remaining in the iterator
097: * that next() could be called N times without having a
098: * NoSuchElementException thrown.
099: *
100: * @return true if there is an element at the Nth position
101: * @throws IllegalArgumentException if n < 0 or n > max lookahead
102: */
103:
104: public boolean hasNextPlus(int n) {
105: if (n < 0 || n > _size)
106: throw new IllegalArgumentException();
107:
108: if (n == 0)
109: return hasNext();
110:
111: return readAhead(n);
112: }
113:
114: /**
115: * Returns the element at the Nth position. Put another way, returns the
116: * element that the Nth call to next() would return. The current position
117: * of the iteration is not modified.
118: *
119: * @return the element at the Nth position
120: * @throws IllegalArgumentException if n < 0 or n > max lookahead
121: * @throws NoSuchElementException if the Nth position is off the end of
122: * the iteration
123: */
124:
125: public T peek(int n) {
126: if (n < 0 || n > _size)
127: throw new IllegalArgumentException();
128:
129: if (!readAhead(n)) {
130: throw new NoSuchElementException();
131: }
132:
133: int offset = (_readptr + n - 1) % _size;
134:
135: // @SuppressWarnings
136: // This generates an unchecked cast warning: it's ok since the only
137: // thing that can get into the buffer came from an
138: // Iterator<? extends T>, we know that the cast is always valid
139:
140: return (T) _buffer[offset];
141: }
142:
143: /**
144: * Returns the maximum offset that may be peeked.
145: */
146: public int getMaxPeekSize() {
147: return _size;
148: }
149:
150: // - - - - - - - - - - -
151: // Iterable<T> interface
152: // - - - - - - - - - - -
153:
154: public Iterator<T> iterator() {
155: return this ;
156: }
157:
158: // - - - - - - - - - - -
159: // Iterator<T> interface
160: // - - - - - - - - - - -
161:
162: public boolean hasNext() {
163: if (_cnt > 0)
164: return true;
165: else
166: return _base.hasNext();
167: }
168:
169: public T next() {
170: if (_cnt == 0)
171: return _base.next();
172:
173: // @SuppressWarnings
174: // This generates an unchecked cast warning: it's ok since the only
175: // thing that can get into the buffer came from an
176: // Iterator<? extends T>, we know that the cast is always valid
177:
178: T value = (T) _buffer[_readptr];
179: _readptr = advance(_readptr);
180: _cnt--;
181:
182: return value;
183: }
184:
185: // private implementation
186:
187: public void remove() {
188: throw new UnsupportedOperationException();
189: }
190:
191: private boolean readAhead(int n) {
192: if (n <= _cnt)
193: return true;
194:
195: while (n > _cnt && _base.hasNext()) {
196: // this line generates an unchecked cast warning, although it should
197: // be safe in this case: it would be difficult to find a way to
198: // corrupt the private array
199: _buffer[_writeptr] = (T) _base.next();
200:
201: _cnt++;
202: _writeptr = advance(_writeptr);
203: }
204:
205: return n == _cnt;
206: }
207:
208: private int advance(int n) {
209: return ++n % _size;
210: }
211: }
|