From: <bra...@us...> - 2011-06-16 17:18:16
|
Revision: 3474 http://archive-access.svn.sourceforge.net/archive-access/?rev=3474&view=rev Author: bradtofel Date: 2011-06-16 17:18:09 +0000 (Thu, 16 Jun 2011) Log Message: ----------- INITIAL REV: new classes, as yet unused, for dealing with Iterators Added Paths: ----------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/AbstractPeekableIterator.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/IPeekableIterator.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/PeekableIteratorComparator.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/SortedCompositeIterator.java Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/AbstractPeekableIterator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/AbstractPeekableIterator.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/AbstractPeekableIterator.java 2011-06-16 17:18:09 UTC (rev 3474) @@ -0,0 +1,93 @@ +package org.archive.wayback.util.iterator; + +import java.io.BufferedReader; +import java.io.IOException; +import java.util.Comparator; +import java.util.Iterator; +import java.util.NoSuchElementException; + + +public abstract class AbstractPeekableIterator<E> implements IPeekableIterator<E> { + private E cachedNext = null; + private boolean done = false; + + // returns next E, or null if hasNext() would return false; + public abstract E getNextInner(); + + public boolean hasNext() { + if(cachedNext != null) { + return true; + } + if(done) { + return false; + } + cachedNext = getNextInner(); + return (cachedNext != null); + } + + public E next() { + if(cachedNext == null) { + if(!hasNext()) { + throw new NoSuchElementException("Call hasNext!"); + } + } + E tmp = cachedNext; + cachedNext = null; + return tmp; + } + + public void remove() { + throw new UnsupportedOperationException("No remove"); + } + + public E peek() { + if(cachedNext == null) { + if(!hasNext()) { + throw new NoSuchElementException("Call hasNext!"); + } + } + return cachedNext; + } + public static <T> IPeekableIterator<T> wrap(Iterator<T> itr) { + return new IteratorWrappedPeekableIterator<T>(itr); + } + public static IPeekableIterator<String> wrapReader(BufferedReader reader) { + return new BufferedReaderPeekableIterator(reader); + } + + private static class IteratorWrappedPeekableIterator<C> extends AbstractPeekableIterator<C> { + private Iterator<C> wrapped = null; + public IteratorWrappedPeekableIterator(Iterator<C> wrapped) { + this.wrapped = wrapped; + } + @Override + public C getNextInner() { + C next = null; + if(wrapped != null) { + if(wrapped.hasNext()) { + next = wrapped.next(); + } + } + return next; + } + } + private static class BufferedReaderPeekableIterator extends AbstractPeekableIterator<String> { + private BufferedReader reader = null; + public BufferedReaderPeekableIterator(BufferedReader reader) { + this.reader = reader; + } + @Override + public String getNextInner() { + String next = null; + if(reader != null) { + try { + next = reader.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return next; + } + } + +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/AbstractPeekableIterator.java ___________________________________________________________________ Added: svn:executable + * Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/IPeekableIterator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/IPeekableIterator.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/IPeekableIterator.java 2011-06-16 17:18:09 UTC (rev 3474) @@ -0,0 +1,7 @@ +package org.archive.wayback.util.iterator; + +import java.util.Iterator; + +public interface IPeekableIterator<E> extends Iterator<E> { + public E peek(); +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/IPeekableIterator.java ___________________________________________________________________ Added: svn:executable + * Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/PeekableIteratorComparator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/PeekableIteratorComparator.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/PeekableIteratorComparator.java 2011-06-16 17:18:09 UTC (rev 3474) @@ -0,0 +1,44 @@ +/* + * This file is part of the Wayback archival access software + * (http://archive-access.sourceforge.net/projects/wayback/). + * + * Licensed to the Internet Archive (IA) by one or more individual + * contributors. + * + * The IA licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.archive.wayback.util.iterator; + +import java.util.Comparator; + +/** + * @author brad + * + * @param <J> type found in Iterators + */ +public class PeekableIteratorComparator<J> implements Comparator<IPeekableIterator<J>> { + private Comparator<J> comparator = null; + /** + * @param comparator to compare the iterators + */ + public PeekableIteratorComparator(Comparator<J> comparator) { + this.comparator = comparator; + } + + public int compare(IPeekableIterator<J> o1, IPeekableIterator<J> o2) { + return comparator.compare(o1.peek(), o2.peek()); + } + public static <K> Comparator<IPeekableIterator<K>> getComparator(Comparator<K> comparator) { + return new PeekableIteratorComparator<K>(comparator); + } +} Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/SortedCompositeIterator.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/SortedCompositeIterator.java (rev 0) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/SortedCompositeIterator.java 2011-06-16 17:18:09 UTC (rev 3474) @@ -0,0 +1,56 @@ +package org.archive.wayback.util.iterator; + +import java.util.Collection; +import java.util.Comparator; +import java.util.Iterator; +import java.util.NoSuchElementException; +import java.util.PriorityQueue; + + +public class SortedCompositeIterator<E> implements Iterator<E> { + private static final int DEFAULT_CAPACITY = 10; + PriorityQueue<IPeekableIterator<E>> q = null; + + public SortedCompositeIterator(Comparator<E> comparator) { + this(DEFAULT_CAPACITY,comparator); + } + public SortedCompositeIterator(int capacity, Comparator<E> comparator) { + q = new PriorityQueue<IPeekableIterator<E>>(capacity, + new PeekableIteratorComparator<E>(comparator)); + } + public void addAll(Collection<Iterator<E>> toAdd) { + for(Iterator<E> e : toAdd) { + addIterator(e); + } + } + public void addIterator(Iterator<E> itr) { + IPeekableIterator<E> i = null; + if(itr instanceof IPeekableIterator) { + i = (IPeekableIterator<E>) itr; + } else { + i = AbstractPeekableIterator.wrap(itr); + } + if(i.hasNext()) { + q.add(i); + } + } + + public boolean hasNext() { + return (q.peek() != null); + } + + public E next() { + IPeekableIterator<E> i = q.poll(); + if(i == null) { + throw new NoSuchElementException("Call hasNext!"); + } + E tmp = i.next(); + if(i.hasNext()) { + q.add(i); + } + return tmp; + } + public void remove() { + throw new UnsupportedOperationException("No remove"); + } +} Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/iterator/SortedCompositeIterator.java ___________________________________________________________________ Added: svn:executable + * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |