Removed prior to release 3.0, now available in per interface packages
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131506 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7525add3f4
commit
79c48c8ddf
|
@ -1,133 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/CircularFifoBuffer.java,v 1.5 2003/11/29 18:04:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* CircularFifoBuffer is a first in first out buffer with a fixed size that
|
||||
* replaces its oldest element if full.
|
||||
* <p>
|
||||
* The removal order of a <code>CircularFifoBuffer</code> is based on the
|
||||
* insertion order; elements are removed in the same order in which they
|
||||
* were added. The iteration order is the same as the removal order.
|
||||
* <p>
|
||||
* The {@link #add(Object)}, {@link #remove()} and {@link #get()} operations
|
||||
* all perform in constant time. All other operations perform in linear
|
||||
* time or worse.
|
||||
* <p>
|
||||
* Note that this implementation is not synchronized. The following can be
|
||||
* used to provide synchronized access to your <code>CircularFifoBuffer</code>:
|
||||
* <pre>
|
||||
* Buffer fifo = BufferUtils.synchronizedBuffer(new CircularFifoBuffer());
|
||||
* </pre>
|
||||
* <p>
|
||||
* This buffer prevents null objects from being added.
|
||||
*
|
||||
* @deprecated WILL BE REMOVED BEFORE v3.0
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.5 $ $Date: 2003/11/29 18:04:57 $
|
||||
*
|
||||
* @author Stefano Fornari
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class CircularFifoBuffer extends BoundedFifoBuffer {
|
||||
|
||||
/**
|
||||
* Constructor that creates a buffer with the default size of 32.
|
||||
*/
|
||||
public CircularFifoBuffer() {
|
||||
super(32);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that creates a buffer with the specified size.
|
||||
*
|
||||
* @param size the size of the buffer (cannot be changed)
|
||||
* @throws IllegalArgumentException if the size is less than 1
|
||||
*/
|
||||
public CircularFifoBuffer(int size) {
|
||||
super(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that creates a buffer from the specified collection.
|
||||
* The collection size also sets the buffer size
|
||||
*
|
||||
* @param coll the collection to copy into the buffer, may not be null
|
||||
* @throws NullPointerException if the collection is null
|
||||
*/
|
||||
public CircularFifoBuffer(Collection coll) {
|
||||
super(coll);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the buffer is full, the least recently added element is discarded so
|
||||
* that a new element can be inserted.
|
||||
*
|
||||
* @param element the element to add
|
||||
* @return true, always
|
||||
*/
|
||||
public boolean add(Object element) {
|
||||
if (isFull()) {
|
||||
remove();
|
||||
}
|
||||
return super.add(element);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,636 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/CommonsLinkedList.java,v 1.10 2003/12/11 00:18:06 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* An implementation of {@link List} which duplicates the behaviour
|
||||
* of {@link LinkedList}, but which provides a more open interface for
|
||||
* subclasses to extend.
|
||||
*
|
||||
* @deprecated TO BE REMOVED BEFORE v3.0
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.10 $ $Date: 2003/12/11 00:18:06 $
|
||||
*
|
||||
* @author Rich Dougherty
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
class CommonsLinkedList extends LinkedList
|
||||
implements List {
|
||||
|
||||
/*
|
||||
* Implementation notes:
|
||||
* - a standard circular doubly-linked list
|
||||
* - a marker node is stored to mark the start and the end of the list
|
||||
* - node creation and removal always occurs through createNode() and
|
||||
* removeNode().
|
||||
* - a modification count is kept, with the same semantics as
|
||||
* {@link java.util.LinkedList}.
|
||||
* - respects {@link AbstractList#modCount}
|
||||
*/
|
||||
|
||||
/**
|
||||
* A node within the {@link CommonsLinkedList}.
|
||||
*
|
||||
* @author <a href="mailto:rich@nil.co.nz">Rich Dougherty</a>
|
||||
*/
|
||||
protected static class Node {
|
||||
|
||||
/**
|
||||
* A pointer to the node before this node.
|
||||
*/
|
||||
public Node previous;
|
||||
|
||||
/**
|
||||
* A pointer to the node after this node.
|
||||
*/
|
||||
public Node next;
|
||||
|
||||
/**
|
||||
* The object contained within this node.
|
||||
*/
|
||||
public Object element;
|
||||
|
||||
public Node() {
|
||||
}
|
||||
|
||||
public Node(Node previous, Node next, Object element) {
|
||||
this.previous = previous;
|
||||
this.next = next;
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a value is equal to this node's element.
|
||||
*
|
||||
* @return <code>true</code> iff the elements are both <code>null</code>
|
||||
* or {@link Object#equals equal}.
|
||||
*/
|
||||
public boolean elementEquals(Object otherElement) {
|
||||
if (element == null) {
|
||||
return otherElement == null;
|
||||
} else {
|
||||
return element.equals(otherElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@link java.util.ListIterator} {@link CommonsLinkedList}.
|
||||
*
|
||||
* @author <a href="mailto:rich@nil.co.nz">Rich Dougherty</a>
|
||||
*/
|
||||
protected class ListIteratorImpl implements ListIterator {
|
||||
|
||||
/**
|
||||
* The node that will be returned by {@link #next()}. If this is equal
|
||||
* to {@link #marker} then there are no more elements to return.
|
||||
*/
|
||||
protected Node nextNode;
|
||||
|
||||
/**
|
||||
* The index of {@link #nextNode}.
|
||||
*/
|
||||
protected int nextIndex;
|
||||
|
||||
/**
|
||||
* The last node that was returned by {@link #next()} or {@link
|
||||
* #previous()}. Set to <code>null</code> if {@link #next()} or {@link
|
||||
* #previous()} haven't been called, or if the node has been removed
|
||||
* with {@link #remove()} or a new node added with {@link #add(Object)}.
|
||||
* Should be accesed through {@link #getLastNodeReturned()} to enforce
|
||||
* this behaviour.
|
||||
*/
|
||||
protected Node lastNodeReturned;
|
||||
|
||||
/**
|
||||
* The modification count that the list is expected to have. If the list
|
||||
* doesn't have this count, then a
|
||||
* {@link java.util.ConcurrentModificationException} may be thrown by
|
||||
* the operations.
|
||||
*/
|
||||
protected int expectedModCount;
|
||||
|
||||
/**
|
||||
* Create a ListIterator for a list, starting at the first element in
|
||||
* the list.
|
||||
*/
|
||||
public ListIteratorImpl() throws IndexOutOfBoundsException {
|
||||
this(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a ListIterator for a list.
|
||||
*
|
||||
* @param startIndex The index to start at.
|
||||
*/
|
||||
public ListIteratorImpl(int startIndex)
|
||||
throws IndexOutOfBoundsException {
|
||||
expectedModCount = modCount;
|
||||
nextNode = getNode(startIndex, true);
|
||||
nextIndex = startIndex;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the modification count of the list is the value that this
|
||||
* object expects.
|
||||
*
|
||||
* @throws ConcurrentModificationException If the list's modification
|
||||
* count isn't the value that was expected.
|
||||
*/
|
||||
protected void checkModCount()
|
||||
throws ConcurrentModificationException {
|
||||
if (modCount != expectedModCount) {
|
||||
throw new ConcurrentModificationException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the last node returned.
|
||||
*
|
||||
* @throws IllegalStateException If {@link #next()} or {@link
|
||||
* #previous()} haven't been called, or if the node has been removed
|
||||
* with {@link #remove()} or a new node added with {@link #add(Object)}.
|
||||
*/
|
||||
protected Node getLastNodeReturned() throws IllegalStateException {
|
||||
if (lastNodeReturned == null) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
return lastNodeReturned;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return nextNode != marker;
|
||||
}
|
||||
|
||||
public Object next() {
|
||||
checkModCount();
|
||||
if (!hasNext()) {
|
||||
throw new NoSuchElementException("No element at index " +
|
||||
nextIndex + ".");
|
||||
}
|
||||
Object element = nextNode.element;
|
||||
lastNodeReturned = nextNode;
|
||||
nextNode = nextNode.next;
|
||||
nextIndex++;
|
||||
return element;
|
||||
}
|
||||
|
||||
public boolean hasPrevious() {
|
||||
return nextNode.previous != marker;
|
||||
}
|
||||
|
||||
public Object previous() {
|
||||
checkModCount();
|
||||
if (!hasPrevious()) {
|
||||
throw new NoSuchElementException("Already at start of list.");
|
||||
}
|
||||
nextNode = nextNode.previous;
|
||||
Object element = nextNode.element;
|
||||
lastNodeReturned = nextNode;
|
||||
nextIndex--;
|
||||
return element;
|
||||
}
|
||||
|
||||
public int nextIndex() {
|
||||
return nextIndex;
|
||||
}
|
||||
|
||||
public int previousIndex() {
|
||||
return nextIndex - 1;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
checkModCount();
|
||||
removeNode(getLastNodeReturned());
|
||||
lastNodeReturned = null;
|
||||
nextIndex--;
|
||||
expectedModCount++;
|
||||
}
|
||||
|
||||
public void set(Object o) {
|
||||
checkModCount();
|
||||
getLastNodeReturned().element = o;
|
||||
}
|
||||
|
||||
public void add(Object o) {
|
||||
checkModCount();
|
||||
addNodeBefore(nextNode, o);
|
||||
lastNodeReturned = null;
|
||||
nextIndex++;
|
||||
expectedModCount++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* A {@link Node} which indicates the start and end of the list and does not
|
||||
* hold a value. The value of <code>next</code> is the first item in the
|
||||
* list. The value of of <code>previous</code> is the last item in the list.
|
||||
*/
|
||||
protected transient Node marker;
|
||||
|
||||
/**
|
||||
* The size of the list.
|
||||
*/
|
||||
protected transient int size;
|
||||
|
||||
public CommonsLinkedList() {
|
||||
initializeEmptyList();
|
||||
}
|
||||
|
||||
public CommonsLinkedList(Collection c) {
|
||||
initializeEmptyList();
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* The equivalent of a default constructor, broken out so it can be called
|
||||
* by any constructor and by <code>readObject</code>.
|
||||
* Subclasses which override this method should make sure they call it so
|
||||
* the list is initialised properly.
|
||||
*/
|
||||
protected void initializeEmptyList() {
|
||||
marker = createNode();
|
||||
marker.next = marker;
|
||||
marker.previous = marker;
|
||||
}
|
||||
|
||||
// Operations on nodes
|
||||
|
||||
/**
|
||||
* Creates a new node with previous, next and element all set to null.
|
||||
*
|
||||
* @return newly created node
|
||||
*/
|
||||
protected Node createNode() {
|
||||
return new Node();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new node with the specified properties.
|
||||
*
|
||||
* @param previous node to precede the new node
|
||||
* @param next node to follow the new node
|
||||
* @param element element of the new node
|
||||
*/
|
||||
protected Node createNode(Node previous, Node next, Object element) {
|
||||
return new Node(previous, next, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new node with the specified object as its
|
||||
* <code>elemnent</code> and inserts it before <code>node</code>.
|
||||
*
|
||||
* @param node node to insert before
|
||||
* @param object element of the newly added node
|
||||
* @throws NullPointerException if <code>node</code> is null
|
||||
*/
|
||||
private void addNodeBefore(Node node, Object o) {
|
||||
Node newNode = createNode(node.previous, node, o);
|
||||
node.previous.next = newNode;
|
||||
node.previous = newNode;
|
||||
size++;
|
||||
modCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new node with the specified object as its
|
||||
* <code>elemnent</code> and inserts it after <code>node</code>.
|
||||
*
|
||||
* @param node node to insert after
|
||||
* @param o element of the newly added node
|
||||
* @throws NullPointerException if <code>node</code> is null
|
||||
*/
|
||||
protected void addNodeAfter(Node node, Object o) {
|
||||
Node newNode = createNode(node, node.next, o);
|
||||
node.next.previous = newNode;
|
||||
node.next = newNode;
|
||||
size++;
|
||||
modCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified node.
|
||||
*
|
||||
* @param node the node to remove
|
||||
* @throws NullPointerException if <code>node</code> is null
|
||||
*/
|
||||
protected void removeNode(Node node) {
|
||||
node.previous.next = node.next;
|
||||
node.next.previous = node.previous;
|
||||
size--;
|
||||
modCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all nodes by resetting the circular list marker.
|
||||
*/
|
||||
protected void removeAllNodes() {
|
||||
marker.next = marker;
|
||||
marker.previous = marker;
|
||||
size = 0;
|
||||
modCount++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the node at a particular index.
|
||||
*
|
||||
* @param index The index, starting from 0.
|
||||
* @param endMarkerAllowed Whether or not the end marker can be returned if
|
||||
* startIndex is set to the list's size.
|
||||
* @throws IndexOutOfBoundsException If the index is less than 0; equal to
|
||||
* the size of the list and endMakerAllowed is false; or greater than the
|
||||
* size of the list.
|
||||
*/
|
||||
protected Node getNode(int index, boolean endMarkerAllowed) throws IndexOutOfBoundsException {
|
||||
// Check the index is within the bounds
|
||||
if (index < 0) {
|
||||
throw new IndexOutOfBoundsException("Couldn't get the node: " +
|
||||
"index (" + index + ") less than zero.");
|
||||
}
|
||||
if (!endMarkerAllowed && index == size) {
|
||||
throw new IndexOutOfBoundsException("Couldn't get the node: " +
|
||||
"index (" + index + ") is the size of the list.");
|
||||
}
|
||||
if (index > size) {
|
||||
throw new IndexOutOfBoundsException("Couldn't get the node: " +
|
||||
"index (" + index + ") greater than the size of the " +
|
||||
"list (" + size + ").");
|
||||
}
|
||||
// Search the list and get the node
|
||||
Node node;
|
||||
if (index < (size / 2)) {
|
||||
// Search forwards
|
||||
node = marker.next;
|
||||
for (int currentIndex = 0; currentIndex < index; currentIndex++) {
|
||||
node = node.next;
|
||||
}
|
||||
} else {
|
||||
// Search backwards
|
||||
node = marker;
|
||||
for (int currentIndex = size; currentIndex > index; currentIndex--) {
|
||||
node = node.previous;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
// List implementation required by AbstractSequentialList
|
||||
|
||||
public ListIterator listIterator() {
|
||||
return new ListIteratorImpl();
|
||||
}
|
||||
|
||||
public ListIterator listIterator(int startIndex) {
|
||||
return new ListIteratorImpl(startIndex);
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
// List implementation not required by AbstractSequentialList, but provided
|
||||
// for efficiency or to override LinkedList's implementation.
|
||||
|
||||
public void clear() {
|
||||
removeAllNodes();
|
||||
}
|
||||
|
||||
public boolean add(Object o) {
|
||||
addLast(o);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void add(int index, Object element) {
|
||||
Node node = getNode(index, true);
|
||||
addNodeBefore(node, element);
|
||||
}
|
||||
|
||||
public boolean addAll(Collection c) {
|
||||
return addAll(size, c);
|
||||
}
|
||||
|
||||
public boolean addAll(int index, Collection c) {
|
||||
Node node = getNode(index, true);
|
||||
for (Iterator itr = c.iterator(); itr.hasNext();) {
|
||||
Object element = itr.next();
|
||||
addNodeBefore(node, element);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Object get(int index) {
|
||||
Node node = getNode(index, false);
|
||||
return node.element;
|
||||
}
|
||||
|
||||
public Object set(int index, Object element) {
|
||||
Node node = getNode(index, false);
|
||||
Object oldElement = node.element;
|
||||
node.element = element;
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
public Object remove(int index) {
|
||||
Node node = getNode(index, false);
|
||||
Object oldElement = node.element;
|
||||
removeNode(node);
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
public boolean remove(Object element) {
|
||||
for (Node node = marker.next; node != marker; node = node.next) {
|
||||
if (node.elementEquals(element)) {
|
||||
removeNode(node);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int indexOf(Object element) {
|
||||
int i = 0;
|
||||
for (Node node = marker.next; node != marker; node = node.next) {
|
||||
if (node.elementEquals(element)) {
|
||||
return i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public int lastIndexOf(Object element) {
|
||||
int i = size - 1;
|
||||
for (Node node = marker.previous; node != marker; node = node.previous) {
|
||||
if (node.elementEquals(element)) {
|
||||
return i;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public boolean contains(Object element) {
|
||||
return indexOf(element) != -1;
|
||||
}
|
||||
|
||||
public Object[] toArray() {
|
||||
return toArray(new Object[size]);
|
||||
}
|
||||
|
||||
public Object[] toArray(Object[] array) {
|
||||
// Extend the array if needed
|
||||
if (array.length < size) {
|
||||
Class componentType = array.getClass().getComponentType();
|
||||
array = (Object[]) Array.newInstance(componentType, size);
|
||||
}
|
||||
// Copy the values into the array
|
||||
Node node = marker.next;
|
||||
for (int i = 0; i < size; i++) {
|
||||
array[i] = node.element;
|
||||
node = node.next;
|
||||
}
|
||||
// Set the value after the last element to null
|
||||
if (array.length > size) {
|
||||
array[size] = null;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
// Extra methods compatible with java.util.LinkedList.
|
||||
|
||||
public Object getFirst() {
|
||||
Node node = marker.next;
|
||||
if (node == marker) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return node.element;
|
||||
}
|
||||
|
||||
public Object getLast() {
|
||||
Node node = marker.previous;
|
||||
if (node == marker) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
return node.element;
|
||||
}
|
||||
|
||||
public void addFirst(Object o) {
|
||||
addNodeAfter(marker, o);
|
||||
}
|
||||
|
||||
public void addLast(Object o) {
|
||||
addNodeBefore(marker, o);
|
||||
}
|
||||
|
||||
public Object removeFirst() {
|
||||
Node node = marker.next;
|
||||
if (node == marker) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
Object oldElement = node.element;
|
||||
removeNode(node);
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
public Object removeLast() {
|
||||
Node node = marker.previous;
|
||||
if (node == marker) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
Object oldElement = node.element;
|
||||
removeNode(node);
|
||||
return oldElement;
|
||||
}
|
||||
|
||||
// Serialization methods
|
||||
|
||||
private void writeObject(ObjectOutputStream outputStream)
|
||||
throws IOException, ClassNotFoundException {
|
||||
outputStream.defaultWriteObject();
|
||||
// Write the size so we know how many nodes to read back
|
||||
outputStream.writeInt(size());
|
||||
for (Iterator itr = iterator(); itr.hasNext();) {
|
||||
outputStream.writeObject(itr.next());
|
||||
}
|
||||
}
|
||||
|
||||
private void readObject(ObjectInputStream inputStream)
|
||||
throws IOException, ClassNotFoundException {
|
||||
inputStream.defaultReadObject();
|
||||
initializeEmptyList();
|
||||
int size = inputStream.readInt();
|
||||
for (int i = 0; i < size; i++) {
|
||||
add(inputStream.readObject());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,228 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/MultiKey.java,v 1.5 2003/12/05 20:23:57 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A <code>MultiKey</code> allows multiple map keys to be merged together.
|
||||
* <p>
|
||||
* The purpose of this class is to avoid the need to write code to handle
|
||||
* maps of maps. An example might be the need to lookup a filename by
|
||||
* key and locale. The typical solution might be nested maps. This class
|
||||
* can be used instead by creating an instance passing in the key and locale.
|
||||
*
|
||||
* @deprecated WILL BE REMOVED BEFORE v3.0
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.5 $ $Date: 2003/12/05 20:23:57 $
|
||||
*
|
||||
* @author Howard Lewis Ship
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class MultiKey implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 4465448607415788805L;
|
||||
|
||||
/** The individual keys */
|
||||
private final Object[] keys;
|
||||
/** The cached hashCode */
|
||||
private final int hashCode;
|
||||
|
||||
/**
|
||||
* Constructor taking two keys.
|
||||
*
|
||||
* @param key1 the first key
|
||||
* @param key2 the second key
|
||||
*/
|
||||
public MultiKey(Object key1, Object key2) {
|
||||
this(new Object[] {key1, key2}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking three keys.
|
||||
*
|
||||
* @param key1 the first key
|
||||
* @param key2 the second key
|
||||
* @param key3 the third key
|
||||
*/
|
||||
public MultiKey(Object key1, Object key2, Object key3) {
|
||||
this(new Object[] {key1, key2, key3}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking four keys.
|
||||
*
|
||||
* @param key1 the first key
|
||||
* @param key2 the second key
|
||||
* @param key3 the third key
|
||||
* @param key4 the fourth key
|
||||
*/
|
||||
public MultiKey(Object key1, Object key2, Object key3, Object key4) {
|
||||
this(new Object[] {key1, key2, key3, key4}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking five keys.
|
||||
*
|
||||
* @param key1 the first key
|
||||
* @param key2 the second key
|
||||
* @param key3 the third key
|
||||
* @param key4 the fourth key
|
||||
* @param key5 the fifth key
|
||||
*/
|
||||
public MultiKey(Object key1, Object key2, Object key3, Object key4, Object key5) {
|
||||
this(new Object[] {key1, key2, key3, key4, key5}, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking an array of keys.
|
||||
*
|
||||
* @param keys the array of keys
|
||||
* @throws IllegalArgumentException if the key array is null
|
||||
*/
|
||||
public MultiKey(Object[] keys) {
|
||||
this(keys, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor taking an array of keys.
|
||||
* <p>
|
||||
* If the array is not copied, then it must not be modified.
|
||||
*
|
||||
* @param keys the array of keys
|
||||
* @param makeCopy true to copy the array, false to assign it
|
||||
* @throws IllegalArgumentException if the key array is null
|
||||
*/
|
||||
protected MultiKey(Object[] keys, boolean makeCopy) {
|
||||
super();
|
||||
if (keys == null) {
|
||||
throw new IllegalArgumentException("The array of keys must not be null");
|
||||
}
|
||||
if (makeCopy) {
|
||||
this.keys = (Object[]) keys.clone();
|
||||
} else {
|
||||
this.keys = keys;
|
||||
}
|
||||
|
||||
int total = 0;
|
||||
for (int i = 0; i < keys.length; i++) {
|
||||
if (keys[i] != null) {
|
||||
if (i == 0) {
|
||||
total = keys[i].hashCode();
|
||||
} else {
|
||||
total ^= keys[i].hashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
hashCode = total;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a copy of the individual keys.
|
||||
*
|
||||
* @return the individual keys
|
||||
*/
|
||||
public Object[] getKeys() {
|
||||
return (Object[]) keys.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this object to another.
|
||||
* <p>
|
||||
* To be equal, the other object must be a <code>MultiKey</code> with the
|
||||
* same number of keys which are also equal.
|
||||
*
|
||||
* @param other the other object to compare to
|
||||
* @return true if equal
|
||||
*/
|
||||
public boolean equals(Object other) {
|
||||
if (other == this) {
|
||||
return true;
|
||||
}
|
||||
if (other instanceof MultiKey) {
|
||||
MultiKey otherMulti = (MultiKey) other;
|
||||
return Arrays.equals(keys, otherMulti.keys);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the combined hashcode that is computed from all the keys.
|
||||
* <p>
|
||||
* This value is computed once and then cached, so elements should not
|
||||
* change their hash codes once created (note that this is the same
|
||||
* constraint that would be used if the individual keys elements were
|
||||
* themselves {@link java.util.Map Map} keys.
|
||||
*
|
||||
* @return the hashcode
|
||||
*/
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a debugging string version of the key.
|
||||
*
|
||||
* @return a debugging string
|
||||
*/
|
||||
public String toString() {
|
||||
return "MultiKey" + Arrays.asList(keys).toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,254 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/NodeCachingLinkedList.java,v 1.10 2003/12/11 00:18:06 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2002-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* A linked list implementation that caches the nodes used internally to prevent
|
||||
* unnecessary object creates and deletion. This should result in a performance
|
||||
* improvement.
|
||||
*
|
||||
* @deprecated TO BE REMOVED BEFORE v3.0
|
||||
* @since Commons Collections 3.0
|
||||
* @version $Revision: 1.10 $ $Date: 2003/12/11 00:18:06 $
|
||||
*
|
||||
* @author Jeff Varszegi
|
||||
* @author <a href="mailto:rich@rd.gen.nz">Rich Dougherty</a>
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
public class NodeCachingLinkedList extends CommonsLinkedList {
|
||||
|
||||
private static final long serialVersionUID = 1;
|
||||
|
||||
/**
|
||||
* The default value for {@link #maximumCacheSize}.
|
||||
*/
|
||||
private static final int DEFAULT_MAXIMUM_CACHE_SIZE = 20;
|
||||
|
||||
/**
|
||||
* The first cached node, or <code>null</code> if no nodes are cached.
|
||||
* Cached nodes are stored in a singly-linked list with {@link Node#next}
|
||||
* pointing to the next element.
|
||||
*/
|
||||
private transient Node firstCachedNode;
|
||||
|
||||
/**
|
||||
* The size of the cache.
|
||||
*/
|
||||
private transient int cacheSize = 0;
|
||||
|
||||
/**
|
||||
* The maximum size of the cache.
|
||||
*/
|
||||
private int maximumCacheSize = DEFAULT_MAXIMUM_CACHE_SIZE;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public NodeCachingLinkedList() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that copies the specified collection
|
||||
*
|
||||
* @param coll the collection to copy
|
||||
*/
|
||||
public NodeCachingLinkedList(Collection coll) {
|
||||
super(coll);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that species the maximum cache size.
|
||||
*
|
||||
* @param maximumCacheSize the maximum cache size
|
||||
*/
|
||||
public NodeCachingLinkedList(int maximumCacheSize) {
|
||||
super();
|
||||
this.maximumCacheSize = maximumCacheSize;
|
||||
}
|
||||
|
||||
// Cache operations
|
||||
|
||||
/**
|
||||
* Gets the maximum size of the cache.
|
||||
*/
|
||||
public int getMaximumCacheSize() {
|
||||
return maximumCacheSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maximum size of the cache.
|
||||
*/
|
||||
public void setMaximumCacheSize(int maximumCacheSize) {
|
||||
this.maximumCacheSize = maximumCacheSize;
|
||||
shrinkCacheToMaximumSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduce the size of the cache to the maximum, if necessary.
|
||||
*/
|
||||
private void shrinkCacheToMaximumSize() {
|
||||
// Rich Dougherty: This could be more efficient.
|
||||
while (cacheSize > maximumCacheSize) {
|
||||
getNodeFromCache();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a node from the cache. If a node is returned, then the value of
|
||||
* {@link #cacheSize} is decreased accordingly. The node that is returned
|
||||
* will have <code>null</code> values for next, previous and element.
|
||||
*
|
||||
* @return A node, or <code>null</code> if there are no nodes in the cache.
|
||||
*/
|
||||
private Node getNodeFromCache() {
|
||||
if (cacheSize == 0) {
|
||||
return null;
|
||||
}
|
||||
Node cachedNode = firstCachedNode;
|
||||
firstCachedNode = cachedNode.next;
|
||||
cachedNode.next = null; // This should be changed anyway, but defensively
|
||||
// set it to null.
|
||||
cacheSize--;
|
||||
return cachedNode;
|
||||
}
|
||||
|
||||
private boolean cacheFull() {
|
||||
return cacheSize >= maximumCacheSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a node to the cache, if the cache isn't full. The node's contents
|
||||
* are cleared to so they can be garbage collected.
|
||||
*/
|
||||
private void addNodeToCache(Node node) {
|
||||
if (cacheFull()) {
|
||||
// Don't cache the node.
|
||||
return;
|
||||
}
|
||||
// Clear the node's contents and add it to the cache.
|
||||
Node nextCachedNode = firstCachedNode;
|
||||
node.previous = null;
|
||||
node.next = nextCachedNode;
|
||||
node.element = null;
|
||||
firstCachedNode = node;
|
||||
cacheSize++;
|
||||
}
|
||||
|
||||
// Node operations
|
||||
|
||||
/**
|
||||
* Create a node, getting it from the cache if possible.
|
||||
*/
|
||||
protected Node createNode() {
|
||||
Node cachedNode = getNodeFromCache();
|
||||
if (cachedNode == null) {
|
||||
return super.createNode();
|
||||
} else {
|
||||
return cachedNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new node with the specified properties, using a cached Node
|
||||
* if possible.
|
||||
*
|
||||
* @param previous node to precede the new node
|
||||
* @param next node to follow the new node
|
||||
* @param element element of the new node
|
||||
*/
|
||||
protected Node createNode(Node previous, Node next, Object element) {
|
||||
Node cachedNode = getNodeFromCache();
|
||||
if (cachedNode == null) {
|
||||
return super.createNode(previous, next, element);
|
||||
} else {
|
||||
cachedNode.next = next;
|
||||
cachedNode.previous = previous;
|
||||
cachedNode.element = element;
|
||||
return cachedNode;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the superclass' implementation then calls
|
||||
* <code>addNodeToCache</code> on the node which has
|
||||
* been removed.
|
||||
*/
|
||||
protected void removeNode(Node node) {
|
||||
super.removeNode(node);
|
||||
addNodeToCache(node);
|
||||
}
|
||||
|
||||
protected void removeAllNodes() {
|
||||
// Add the removed nodes to the cache, then remove the rest.
|
||||
// We can add them to the cache before removing them, since
|
||||
// {@link CommonsLinkedList.removeAllNodes()} removes the
|
||||
// nodes by removing references directly from {@link #marker}.
|
||||
int numberOfNodesToCache = Math.min(size, maximumCacheSize - cacheSize);
|
||||
Node node = marker.next;
|
||||
for (int currentIndex = 0; currentIndex < numberOfNodesToCache; currentIndex++) {
|
||||
Node oldNode = node;
|
||||
node = node.next;
|
||||
addNodeToCache(oldNode);
|
||||
}
|
||||
super.removeAllNodes();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.54 2003/11/16 20:35:46 scolebourne Exp $
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestAll.java,v 1.55 2004/01/04 18:02:58 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
|
@ -64,7 +64,7 @@ import junit.framework.TestSuite;
|
|||
/**
|
||||
* Entry point for all Collections package tests.
|
||||
*
|
||||
* @version $Revision: 1.54 $ $Date: 2003/11/16 20:35:46 $
|
||||
* @version $Revision: 1.55 $ $Date: 2004/01/04 18:02:58 $
|
||||
*
|
||||
* @author Rodney Waldhoff
|
||||
* @author Stephen Colebourne
|
||||
|
@ -80,6 +80,7 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestClosureUtils.suite());
|
||||
suite.addTest(TestCollectionUtils.suite());
|
||||
suite.addTest(TestBufferUtils.suite());
|
||||
suite.addTest(TestEnumerationUtils.suite());
|
||||
suite.addTest(TestFactoryUtils.suite());
|
||||
suite.addTest(TestListUtils.suite());
|
||||
suite.addTest(TestMapUtils.suite());
|
||||
|
@ -92,8 +93,6 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestBinaryHeap.suite());
|
||||
suite.addTest(TestBoundedFifoBuffer.suite());
|
||||
suite.addTest(TestBoundedFifoBuffer2.suite());
|
||||
suite.addTest(TestCircularFifoBuffer.suite());
|
||||
suite.addTest(TestCommonsLinkedList.suite());
|
||||
suite.addTest(TestCursorableLinkedList.suite());
|
||||
suite.addTest(TestDoubleOrderedMap.suite());
|
||||
suite.addTest(TestExtendedProperties.suite());
|
||||
|
@ -107,14 +106,11 @@ public class TestAll extends TestCase {
|
|||
suite.addTest(TestIteratorUtils.suite());
|
||||
suite.addTest(TestLRUMap.suite());
|
||||
suite.addTest(TestMultiHashMap.suite());
|
||||
suite.addTest(TestMultiKey.suite());
|
||||
suite.addTest(TestNodeCachingLinkedList.suite());
|
||||
suite.addTest(TestReferenceMap.suite());
|
||||
suite.addTest(TestSequencedHashMap.suite());
|
||||
suite.addTest(TestStaticBucketMap.suite());
|
||||
suite.addTest(TestTreeBag.suite());
|
||||
suite.addTest(TestUnboundedFifoBuffer.suite());
|
||||
suite.addTest(TestEnumerationUtils.suite());
|
||||
return suite;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,235 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestCircularFifoBuffer.java,v 1.7 2003/11/18 22:37:16 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
import org.apache.commons.collections.collection.AbstractTestCollection;
|
||||
|
||||
/**
|
||||
* Test cases for CircularFifoBuffer.
|
||||
*
|
||||
* @version $Revision: 1.7 $ $Date: 2003/11/18 22:37:16 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TestCircularFifoBuffer extends AbstractTestCollection {
|
||||
|
||||
public TestCircularFifoBuffer(String n) {
|
||||
super(n);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return BulkTest.makeSuite(TestCircularFifoBuffer.class);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Runs through the regular verifications, but also verifies that
|
||||
* the buffer contains the same elements in the same sequence as the
|
||||
* list.
|
||||
*/
|
||||
public void verify() {
|
||||
super.verify();
|
||||
Iterator iterator1 = collection.iterator();
|
||||
Iterator iterator2 = confirmed.iterator();
|
||||
while (iterator2.hasNext()) {
|
||||
assertTrue(iterator1.hasNext());
|
||||
Object o1 = iterator1.next();
|
||||
Object o2 = iterator2.next();
|
||||
assertEquals(o1, o2);
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Overridden because UnboundedFifoBuffer doesn't allow null elements.
|
||||
* @return false
|
||||
*/
|
||||
public boolean isNullSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overridden because UnboundedFifoBuffer isn't fail fast.
|
||||
* @return false
|
||||
*/
|
||||
public boolean isFailFastSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns an empty ArrayList.
|
||||
*
|
||||
* @return an empty ArrayList
|
||||
*/
|
||||
public Collection makeConfirmedCollection() {
|
||||
return new ArrayList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a full ArrayList.
|
||||
*
|
||||
* @return a full ArrayList
|
||||
*/
|
||||
public Collection makeConfirmedFullCollection() {
|
||||
Collection c = makeConfirmedCollection();
|
||||
c.addAll(java.util.Arrays.asList(getFullElements()));
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty BoundedFifoBuffer that won't overflow.
|
||||
*
|
||||
* @return an empty BoundedFifoBuffer
|
||||
*/
|
||||
public Collection makeCollection() {
|
||||
return new CircularFifoBuffer(100);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Tests that the removal operation actually removes the first element.
|
||||
*/
|
||||
public void testCircularFifoBufferCircular() {
|
||||
List list = new ArrayList();
|
||||
list.add("A");
|
||||
list.add("B");
|
||||
list.add("C");
|
||||
Buffer buf = new CircularFifoBuffer(list);
|
||||
|
||||
assertEquals(true, buf.contains("A"));
|
||||
assertEquals(true, buf.contains("B"));
|
||||
assertEquals(true, buf.contains("C"));
|
||||
|
||||
buf.add("D");
|
||||
|
||||
assertEquals(false, buf.contains("A"));
|
||||
assertEquals(true, buf.contains("B"));
|
||||
assertEquals(true, buf.contains("C"));
|
||||
assertEquals(true, buf.contains("D"));
|
||||
|
||||
assertEquals("B", buf.get());
|
||||
assertEquals("B", buf.remove());
|
||||
assertEquals("C", buf.remove());
|
||||
assertEquals("D", buf.remove());
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the removal operation actually removes the first element.
|
||||
*/
|
||||
public void testCircularFifoBufferRemove() {
|
||||
resetFull();
|
||||
int size = confirmed.size();
|
||||
for (int i = 0; i < size; i++) {
|
||||
Object o1 = ((CircularFifoBuffer) collection).remove();
|
||||
Object o2 = ((ArrayList) confirmed).remove(0);
|
||||
assertEquals("Removed objects should be equal", o1, o2);
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
((CircularFifoBuffer) collection).remove();
|
||||
fail("Empty buffer should raise Underflow.");
|
||||
} catch (BufferUnderflowException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the constructor correctly throws an exception.
|
||||
*/
|
||||
public void testConstructorException1() {
|
||||
try {
|
||||
new CircularFifoBuffer(0);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return;
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the constructor correctly throws an exception.
|
||||
*/
|
||||
public void testConstructorException2() {
|
||||
try {
|
||||
new CircularFifoBuffer(-20);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
return;
|
||||
}
|
||||
fail();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the constructor correctly throws an exception.
|
||||
*/
|
||||
public void testConstructorException3() {
|
||||
try {
|
||||
new CircularFifoBuffer(null);
|
||||
} catch (NullPointerException ex) {
|
||||
return;
|
||||
}
|
||||
fail();
|
||||
}
|
||||
}
|
|
@ -1,222 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestCommonsLinkedList.java,v 1.8 2003/12/11 00:18:06 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
/**
|
||||
* Test case for {@link CommonsLinkedList}.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/12/11 00:18:06 $
|
||||
*
|
||||
* @author Rich Dougherty
|
||||
* @author David Hay
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
public class TestCommonsLinkedList extends TestLinkedList {
|
||||
|
||||
protected CommonsLinkedList list = null;
|
||||
|
||||
public TestCommonsLinkedList(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
public LinkedList makeEmptyLinkedList() {
|
||||
return new CommonsLinkedList();
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return BulkTest.makeSuite(TestCommonsLinkedList.class);
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
return "3";
|
||||
}
|
||||
|
||||
protected boolean skipSerializedCanonicalTests() {
|
||||
return true;
|
||||
}
|
||||
public void testCanonicalEmptyCollectionExists() {
|
||||
}
|
||||
public void testCanonicalFullCollectionExists() {
|
||||
}
|
||||
|
||||
|
||||
public void setUp() {
|
||||
list = (CommonsLinkedList)makeEmptyList();
|
||||
}
|
||||
|
||||
public void testRemoveFirst() {
|
||||
list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
|
||||
assertEquals( "value1", list.removeFirst() );
|
||||
checkNodes();
|
||||
list.addLast( "value3");
|
||||
checkNodes();
|
||||
assertEquals( "value2", list.removeFirst() );
|
||||
assertEquals( "value3", list.removeFirst() );
|
||||
checkNodes();
|
||||
list.addLast( "value4" );
|
||||
checkNodes();
|
||||
assertEquals( "value4", list.removeFirst() );
|
||||
checkNodes();
|
||||
}
|
||||
|
||||
public void testRemoveLast() {
|
||||
list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
|
||||
assertEquals( "value2", list.removeLast() );
|
||||
list.addFirst( "value3");
|
||||
checkNodes();
|
||||
assertEquals( "value1", list.removeLast() );
|
||||
assertEquals( "value3", list.removeLast() );
|
||||
list.addFirst( "value4" );
|
||||
checkNodes();
|
||||
assertEquals( "value4", list.removeFirst() );
|
||||
}
|
||||
|
||||
public void testAddNodeAfter() {
|
||||
list.addFirst("value1");
|
||||
list.addNodeAfter(list.getNode(0,false),"value2");
|
||||
assertEquals("value1", list.getFirst());
|
||||
assertEquals("value2", list.getLast());
|
||||
list.removeFirst();
|
||||
checkNodes();
|
||||
list.addNodeAfter(list.getNode(0,false),"value3");
|
||||
checkNodes();
|
||||
assertEquals("value2", list.getFirst());
|
||||
assertEquals("value3", list.getLast());
|
||||
list.addNodeAfter(list.getNode(0, false),"value4");
|
||||
checkNodes();
|
||||
assertEquals("value2", list.getFirst());
|
||||
assertEquals("value3", list.getLast());
|
||||
assertEquals("value4", list.get(1));
|
||||
list.addNodeAfter(list.getNode(2, false), "value5");
|
||||
checkNodes();
|
||||
assertEquals("value2", list.getFirst());
|
||||
assertEquals("value4", list.get(1));
|
||||
assertEquals("value3", list.get(2));
|
||||
assertEquals("value5", list.getLast());
|
||||
}
|
||||
|
||||
public void testRemoveNode() {
|
||||
list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
|
||||
list.removeNode(list.getNode(0, false));
|
||||
checkNodes();
|
||||
assertEquals("value2", list.getFirst());
|
||||
assertEquals("value2", list.getLast());
|
||||
list.addFirst("value1");
|
||||
list.addFirst("value0");
|
||||
checkNodes();
|
||||
list.removeNode(list.getNode(1, false));
|
||||
assertEquals("value0", list.getFirst());
|
||||
assertEquals("value2", list.getLast());
|
||||
checkNodes();
|
||||
list.removeNode(list.getNode(1, false));
|
||||
assertEquals("value0", list.getFirst());
|
||||
assertEquals("value0", list.getLast());
|
||||
checkNodes();
|
||||
}
|
||||
|
||||
public void testGetNode() {
|
||||
// get marker
|
||||
assertEquals(list.getNode(0, true).previous, list.getNode(0, true).next);
|
||||
try {
|
||||
Object obj = list.getNode(0, false);
|
||||
fail("Expecting IndexOutOfBoundsException.");
|
||||
} catch (IndexOutOfBoundsException ex) {
|
||||
// expected
|
||||
}
|
||||
list.addAll( Arrays.asList( new String[]{"value1", "value2"}));
|
||||
checkNodes();
|
||||
list.addFirst("value0");
|
||||
checkNodes();
|
||||
list.removeNode(list.getNode(1, false));
|
||||
checkNodes();
|
||||
try {
|
||||
Object obj = list.getNode(2, false);
|
||||
fail("Expecting IndexOutOfBoundsException.");
|
||||
} catch (IndexOutOfBoundsException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
Object obj = list.getNode(-1, false);
|
||||
fail("Expecting IndexOutOfBoundsException.");
|
||||
} catch (IndexOutOfBoundsException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
Object obj = list.getNode(3, true);
|
||||
fail("Expecting IndexOutOfBoundsException.");
|
||||
} catch (IndexOutOfBoundsException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkNodes() {
|
||||
for (int i = 0; i < list.size; i++) {
|
||||
assertEquals(list.getNode(i, false).next, list.getNode(i + 1, true));
|
||||
if (i < list.size - 1) {
|
||||
assertEquals(list.getNode(i + 1, false).previous,
|
||||
list.getNode(i, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,151 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestMultiKey.java,v 1.3 2003/10/05 21:17:40 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import junit.framework.Assert;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.apache.commons.collections.MultiKey}.
|
||||
*
|
||||
* @version $Revision: 1.3 $ $Date: 2003/10/05 21:17:40 $
|
||||
*
|
||||
* @author Stephen Colebourne
|
||||
*/
|
||||
public class TestMultiKey extends TestCase {
|
||||
|
||||
Integer ONE = new Integer(1);
|
||||
Integer TWO = new Integer(2);
|
||||
Integer THREE = new Integer(3);
|
||||
Integer FOUR = new Integer(4);
|
||||
Integer FIVE = new Integer(5);
|
||||
|
||||
public TestMultiKey(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestMultiKey.class);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] testCaseName = { TestMultiKey.class.getName() };
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
}
|
||||
|
||||
protected void tearDown() throws Exception {
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
|
||||
public void testConstructorsAndGet() throws Exception {
|
||||
MultiKey mk = null;
|
||||
mk = new MultiKey(ONE, TWO);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO}, mk.getKeys()));
|
||||
|
||||
mk = new MultiKey(ONE, TWO, THREE);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE}, mk.getKeys()));
|
||||
|
||||
mk = new MultiKey(ONE, TWO, THREE, FOUR);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR}, mk.getKeys()));
|
||||
|
||||
mk = new MultiKey(ONE, TWO, THREE, FOUR, FIVE);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {ONE, TWO, THREE, FOUR, FIVE}, mk.getKeys()));
|
||||
|
||||
mk = new MultiKey(new Object[] {THREE, FOUR, ONE, TWO}, false);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
|
||||
|
||||
// don't do this!
|
||||
Object[] keys = new Object[] {THREE, FOUR, ONE, TWO};
|
||||
mk = new MultiKey(keys);
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
|
||||
keys[3] = FIVE; // no effect
|
||||
Assert.assertTrue(Arrays.equals(new Object[] {THREE, FOUR, ONE, TWO}, mk.getKeys()));
|
||||
}
|
||||
|
||||
public void testHashCode() {
|
||||
MultiKey mk1 = new MultiKey(ONE, TWO);
|
||||
MultiKey mk2 = new MultiKey(ONE, TWO);
|
||||
MultiKey mk3 = new MultiKey(ONE, "TWO");
|
||||
|
||||
Assert.assertTrue(mk1.hashCode() == mk1.hashCode());
|
||||
Assert.assertTrue(mk1.hashCode() == mk2.hashCode());
|
||||
Assert.assertTrue(mk1.hashCode() != mk3.hashCode());
|
||||
}
|
||||
|
||||
public void testEquals() {
|
||||
MultiKey mk1 = new MultiKey(ONE, TWO);
|
||||
MultiKey mk2 = new MultiKey(ONE, TWO);
|
||||
MultiKey mk3 = new MultiKey(ONE, "TWO");
|
||||
|
||||
Assert.assertEquals(mk1, mk1);
|
||||
Assert.assertEquals(mk1, mk2);
|
||||
Assert.assertTrue(mk1.equals(mk3) == false);
|
||||
Assert.assertTrue(mk1.equals("") == false);
|
||||
Assert.assertTrue(mk1.equals(null) == false);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,184 +0,0 @@
|
|||
/*
|
||||
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestNodeCachingLinkedList.java,v 1.8 2003/10/06 21:02:50 scolebourne Exp $
|
||||
* ====================================================================
|
||||
*
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2001-2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowledgement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowledgement may appear in the software itself,
|
||||
* if and wherever such third-party acknowledgements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.apache.commons.collections;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
||||
/**
|
||||
* Test class for NodeCachingLinkedList, a performance optimised LinkedList.
|
||||
*
|
||||
* @version $Revision: 1.8 $ $Date: 2003/10/06 21:02:50 $
|
||||
*
|
||||
* @author Jeff Varszegi
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
public class TestNodeCachingLinkedList extends TestCommonsLinkedList {
|
||||
|
||||
public TestNodeCachingLinkedList(String _testName) {
|
||||
super(_testName);
|
||||
}
|
||||
|
||||
public LinkedList makeEmptyLinkedList() {
|
||||
return new NodeCachingLinkedList();
|
||||
}
|
||||
|
||||
public void setUp() {
|
||||
list = (NodeCachingLinkedList)makeEmptyList();
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
return BulkTest.makeSuite(TestNodeCachingLinkedList.class);
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
return "3";
|
||||
}
|
||||
|
||||
public void testShrinkCache() {
|
||||
list.addAll( Arrays.asList( new String[]{"1", "2", "3", "4"}));
|
||||
list.removeAllNodes(); // Will dump all 4 elements into cache
|
||||
((NodeCachingLinkedList) list).setMaximumCacheSize(2); // shrink cache
|
||||
list.addAll( Arrays.asList( new String[]{"1", "2", "3", "4"}));
|
||||
checkNodes();
|
||||
list.removeNode(list.getNode(0, false)); // no room in cache
|
||||
list.removeNode(list.getNode(0, false));
|
||||
list.removeNode(list.getNode(0, false));
|
||||
checkNodes();
|
||||
list.addAll( Arrays.asList( new String[]{"1", "2", "3", "4"}));
|
||||
checkNodes();
|
||||
}
|
||||
|
||||
public static void compareSpeed() {
|
||||
NodeCachingLinkedList ncll = new NodeCachingLinkedList();
|
||||
LinkedList ll = new LinkedList();
|
||||
|
||||
Object o1 = new Object();
|
||||
Object o2 = new Object();
|
||||
|
||||
int loopCount = 4000000;
|
||||
|
||||
long startTime, endTime;
|
||||
|
||||
System.out.println("Testing relative execution time of commonly-used methods...");
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
for(int x = loopCount; x > 0; x--) {
|
||||
// unrolled a few times to minimize effect of loop
|
||||
ll.addFirst(o1);
|
||||
ll.addLast(o2);
|
||||
ll.removeFirst();
|
||||
ll.removeLast();
|
||||
ll.add(o1);
|
||||
ll.remove(0);
|
||||
//
|
||||
ll.addFirst(o1);
|
||||
ll.addLast(o2);
|
||||
ll.removeFirst();
|
||||
ll.removeLast();
|
||||
ll.add(o1);
|
||||
ll.remove(0);
|
||||
//
|
||||
ll.addFirst(o1);
|
||||
ll.addLast(o2);
|
||||
ll.removeFirst();
|
||||
ll.removeLast();
|
||||
ll.add(o1);
|
||||
ll.remove(0);
|
||||
}
|
||||
endTime = System.currentTimeMillis();
|
||||
System.out.println("Time with LinkedList: " + (endTime - startTime) + " ms");
|
||||
|
||||
startTime = System.currentTimeMillis();
|
||||
for(int x = loopCount; x > 0; x--) {
|
||||
ncll.addFirst(o1);
|
||||
ncll.addLast(o2);
|
||||
ncll.removeFirst();
|
||||
ncll.removeLast();
|
||||
ncll.add(o1);
|
||||
ncll.remove(0);
|
||||
//
|
||||
ncll.addFirst(o1);
|
||||
ncll.addLast(o2);
|
||||
ncll.removeFirst();
|
||||
ncll.removeLast();
|
||||
ncll.add(o1);
|
||||
ncll.remove(0);
|
||||
//
|
||||
ncll.addFirst(o1);
|
||||
ncll.addLast(o2);
|
||||
ncll.removeFirst();
|
||||
ncll.removeLast();
|
||||
ncll.add(o1);
|
||||
ncll.remove(0);
|
||||
}
|
||||
endTime = System.currentTimeMillis();
|
||||
System.out.println("Time with NodeCachingLinkedList: " + (endTime - startTime) + " ms");
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static void main(String args[]) {
|
||||
compareSpeed();
|
||||
String[] testCaseName = { TestNodeCachingLinkedList.class.getName()};
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue