From: <bra...@us...> - 2007-07-25 00:19:23
|
Revision: 1857 http://archive-access.svn.sourceforge.net/archive-access/?rev=1857&view=rev Author: bradtofel Date: 2007-07-24 17:19:26 -0700 (Tue, 24 Jul 2007) Log Message: ----------- REFACTOR: moved PeekableIterator into it's own class TWEAK: added type safety Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/CompositeSortedIterator.java Added Paths: ----------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/PeekableIterator.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/CompositeSortedIterator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/CompositeSortedIterator.java 2007-07-25 00:17:15 UTC (rev 1856) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/CompositeSortedIterator.java 2007-07-25 00:19:26 UTC (rev 1857) @@ -39,26 +39,27 @@ * * @author brad * @version $Date$, $Revision$ + * @param <E> */ -public class CompositeSortedIterator implements CloseableIterator { +public class CompositeSortedIterator<E> implements CloseableIterator<E> { - private ArrayList<PeekableIterator> components; - private Object next; - private Comparator<Object> comparator; + private ArrayList<PeekableIterator<E>> components; + private E next; + private Comparator<E> comparator; /** * @param comparator Comparator to use for sorting order */ - public CompositeSortedIterator(Comparator<Object> comparator) { + public CompositeSortedIterator(Comparator<E> comparator) { this.comparator = comparator; - components = new ArrayList<PeekableIterator>(); + components = new ArrayList<PeekableIterator<E>>(); next = null; } /** * @param itr Iterator which is a component of this composite */ - public void addComponent(Iterator itr) { - components.add(new PeekableIterator(itr)); + public void addComponent(Iterator<E> itr) { + components.add(new PeekableIterator<E>(itr)); } /* (non-Javadoc) * @see java.util.Iterator#hasNext() @@ -68,11 +69,11 @@ return true; } // find lowest next: - PeekableIterator nextSource = null; + PeekableIterator<E> nextSource = null; for(int i = 0; i < components.size(); i++) { - PeekableIterator pi = components.get(i); + PeekableIterator<E> pi = components.get(i); if(pi.hasNext()) { - Object piNext = pi.peekNext(); + E piNext = pi.peekNext(); if((next == null) || (comparator.compare(next,piNext) > 0)) { nextSource = pi; next = piNext; @@ -87,11 +88,11 @@ /* (non-Javadoc) * @see java.util.Iterator#next() */ - public Object next() { + public E next() { if(!hasNext()) { throw new NoSuchElementException(); } - Object retObject = next; + E retObject = next; next = null; return retObject; } @@ -107,66 +108,8 @@ */ public void close() throws IOException { for(int i = 0; i < components.size(); i++) { - PeekableIterator pi = (PeekableIterator) components.get(i); + PeekableIterator<E> pi = (PeekableIterator<E>) components.get(i); pi.close(); } } - - private class PeekableIterator implements CloseableIterator { - private Object cachedNext; - private Iterator itr; - /** - * @param itr - */ - public PeekableIterator(Iterator itr) { - this.itr = itr; - this.cachedNext = null; - } - /** - * @return true if this Iterator has another element. - */ - public boolean hasNext() { - if(cachedNext != null) { - return true; - } - return itr.hasNext(); - } - /** - * @return Object that will be returned from next(), or null - */ - public Object peekNext() { - if(cachedNext == null) { - if(itr.hasNext()) { - cachedNext = itr.next(); - } - } - return cachedNext; - } - /** - * @return next Object - */ - public Object next() { - if(cachedNext != null) { - Object retObject = cachedNext; - cachedNext = null; - return retObject; - } - return itr.next(); - } - /* (non-Javadoc) - * @see org.archive.wayback.util.Cleanable#clean() - */ - public void close() throws IOException { - if(itr instanceof CloseableIterator) { - CloseableIterator toBeClosed = (CloseableIterator) itr; - toBeClosed.close(); - } - } - /* (non-Javadoc) - * @see java.util.Iterator#remove() - */ - public void remove() { - throw new NotImplementedException(); - } - } } Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/PeekableIterator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/PeekableIterator.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/PeekableIterator.java 2007-07-25 00:19:26 UTC (rev 1857) @@ -0,0 +1,95 @@ +/* PeekableIterator + * + * $Id$ + * + * Created on 4:37:15 PM Jul 24, 2007. + * + * Copyright (C) 2007 Internet Archive. + * + * This file is part of wayback-core. + * + * wayback-core is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or + * any later version. + * + * wayback-core 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 Lesser Public License for more details. + * + * You should have received a copy of the GNU Lesser Public License + * along with wayback-core; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +package org.archive.wayback.util; + +import java.io.IOException; +import java.util.Iterator; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; + +/** + * + * + * @author brad + * @version $Date$, $Revision$ + * @param <E> + */ +public class PeekableIterator<E> implements CloseableIterator<E> { + private E cachedNext; + private Iterator<E> itr; + /** + * @param itr + */ + public PeekableIterator(Iterator<E> itr) { + this.itr = itr; + this.cachedNext = null; + } + /** + * @return true if this Iterator has another element. + */ + public boolean hasNext() { + if(cachedNext != null) { + return true; + } + return itr.hasNext(); + } + /** + * @return Object that will be returned from next(), or null + */ + public E peekNext() { + if(cachedNext == null) { + if(itr.hasNext()) { + cachedNext = itr.next(); + } + } + return cachedNext; + } + /** + * @return next Object + */ + public E next() { + if(cachedNext != null) { + E retObject = cachedNext; + cachedNext = null; + return retObject; + } + return itr.next(); + } + /* (non-Javadoc) + * @see org.archive.wayback.util.Cleanable#clean() + */ + public void close() throws IOException { + if(itr instanceof CloseableIterator) { + CloseableIterator<E> toBeClosed = (CloseableIterator<E>) itr; + toBeClosed.close(); + } + } + /* (non-Javadoc) + * @see java.util.Iterator#remove() + */ + public void remove() { + throw new NotImplementedException(); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |