001: /* Copyright (C) 2003 Internet Archive.
002: *
003: * This file is part of the Heritrix web crawler (crawler.archive.org).
004: *
005: * Heritrix is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Lesser Public License as published by
007: * the Free Software Foundation; either version 2.1 of the License, or
008: * any later version.
009: *
010: * Heritrix is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser Public License
016: * along with Heritrix; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * MemQueue.java
020: * Created on Oct 14, 2003
021: *
022: * $Header$
023: */
024: package org.archive.queue;
025:
026: import java.util.Iterator;
027: import java.util.LinkedList;
028:
029: import org.apache.commons.collections.Predicate;
030:
031: /** An in-memory implementation of a {@link Queue}.
032: *
033: * @author Gordon Mohr
034: *
035: */
036: public class MemQueue<T> extends LinkedList<T> implements Queue<T> {
037:
038: private static final long serialVersionUID = -9077824759011044247L;
039:
040: /** Create a new, empty MemQueue
041: */
042: public MemQueue() {
043: super ();
044: }
045:
046: /**
047: * @see org.archive.queue.Queue#enqueue(Object)
048: */
049: public void enqueue(T o) {
050: add(o);
051: }
052:
053: /**
054: * @see org.archive.queue.Queue#dequeue()
055: */
056: public T dequeue() {
057: return removeFirst();
058: }
059:
060: /**
061: * @see org.archive.queue.Queue#length()
062: */
063: public long length() {
064: return size();
065: }
066:
067: /**
068: * @see org.archive.queue.Queue#release()
069: */
070: public void release() {
071: // nothing to release
072: }
073:
074: /**
075: * @see org.archive.queue.Queue#peek()
076: */
077: public T peek() {
078: return getFirst();
079: }
080:
081: /**
082: * @see org.archive.queue.Queue#getIterator(boolean)
083: */
084: public Iterator<T> getIterator(boolean inCacheOnly) {
085: return listIterator();
086: }
087:
088: /**
089: * @see org.archive.queue.Queue#deleteMatchedItems(org.apache.commons.collections.Predicate)
090: */
091: public long deleteMatchedItems(Predicate matcher) {
092: Iterator<T> it = listIterator();
093: long numberOfDeletes = 0;
094: while (it.hasNext()) {
095: if (matcher.evaluate(it.next())) {
096: it.remove();
097: numberOfDeletes++;
098: }
099: }
100: return numberOfDeletes;
101: }
102:
103: /* (non-Javadoc)
104: * @see org.archive.queue.Queue#unpeek()
105: */
106: public void unpeek() {
107: // nothing necessary
108: }
109:
110: }
|