001: // ============================================================================
002: // $Id: CachingIterator.java,v 1.9 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 retain the last few elements returned.
042: * <p>
043: * Copyright © 2003-2005 David A. Hall
044: *
045: * @author <a href="mailto:davidahall@users.sf.net">David A. Hall</a>
046: */
047:
048: public class CachingIterator<T> implements Iterator<T>, Iterable<T> {
049: // The base iterator
050: private Iterator<? extends T> _base;
051:
052: // ring buffer, to hold the elements that have been consumed
053: private Object[] _cache;
054:
055: // index of the buffer's base element. Once the ring buffer loops around,
056: // the base pointer indicates the logical
057: private int _baseptr = 0;
058:
059: // number of elements in the buffer
060: private int _cnt = 0;
061:
062: // size of the buffer
063: private int _size;
064:
065: /**
066: * Builds a CachingIterator that can retain 1 element.
067: */
068:
069: public CachingIterator(Iterator<? extends T> base) {
070: this (base, 1);
071: }
072:
073: /**
074: * Builds a CachingIterator that can retain the given number of elements.
075: *
076: * @throws IllegalArgumentException if max <= 0.
077: */
078:
079: public CachingIterator(Iterator<? extends T> base, int max) {
080: if (max <= 0)
081: throw new IllegalArgumentException();
082:
083: _base = (base != null) ? base : new EmptyIterator<T>();
084: _size = max;
085:
086: _cache = new Object[_size];
087: }
088:
089: /**
090: * Returns true if there have been at least N elements consumed from the
091: * underlying iterator.
092: */
093:
094: public boolean hasCached(int n) {
095: return n > 0 && n <= _cnt;
096: }
097:
098: /**
099: * Returns the Nth previous element consumed from the underlying iterator.
100: */
101:
102: public T cached(int n) {
103: if (n <= 0 || n > _cnt || n > _size)
104: throw new NoSuchElementException();
105:
106: // @SuppressWarnings
107: // The buffer array is private, and the only things that we ever put
108: // into it are known to be T objects because they are read from an
109: // Iterator<? extends T>, so we know that the cast is always valid
110:
111: return (T) _cache[(_baseptr + _size - n) % _size];
112: }
113:
114: /**
115: * Returns the cache size
116: */
117: public int getCacheSize() {
118: return _size;
119: }
120:
121: /**
122: * Returns the number of elements in the cache
123: */
124: public int getCacheCount() {
125: return _cnt;
126: }
127:
128: // - - - - - - - - - - -
129: // Iterable<T> interface
130: // - - - - - - - - - - -
131:
132: public Iterator<T> iterator() {
133: return this ;
134: }
135:
136: // - - - - - - - - - - -
137: // Iterator<T> interface
138: // - - - - - - - - - - -
139:
140: public boolean hasNext() {
141: return _base.hasNext();
142: }
143:
144: public T next() {
145: if (_cnt < _size)
146: ++_cnt;
147:
148: int n = _baseptr;
149: _baseptr = ++_baseptr % _size;
150: _cache[n] = _base.next();
151:
152: // @SuppressWarnings
153: // The buffer array is private, and the only things that we ever put
154: // into it are known to be T objects because they are read from an
155: // Iterator<? extends T>, so we know that the cast is always valid
156:
157: return (T) _cache[n];
158: }
159:
160: /**
161: * Removes the last item returned from the base iterator, if the base
162: * iterator supports the remove operation. The item is not removed from
163: * the cache.
164: * @throws UnsupportedOperationException if the base iterator does not
165: * support the remove() operation
166: */
167: public void remove() {
168: _base.remove();
169: }
170: }
|