diff options
author | Brian Vosburgh | 2016-02-26 20:39:13 +0000 |
---|---|---|
committer | Brian Vosburgh | 2017-05-18 22:36:46 +0000 |
commit | 72419e50e9a624fd9deab3cea612aafe41fd7f35 (patch) | |
tree | a1d3a44590523972bee8a1be210d8a96727ac414 /common | |
parent | 207544a489a8348e2aab8620a0da4054e32f6c15 (diff) | |
download | webtools.dali-72419e50e9a624fd9deab3cea612aafe41fd7f35.tar.gz webtools.dali-72419e50e9a624fd9deab3cea612aafe41fd7f35.tar.xz webtools.dali-72419e50e9a624fd9deab3cea612aafe41fd7f35.zip |
remove Queue Node caches
Diffstat (limited to 'common')
7 files changed, 521 insertions, 351 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/LinkedQueue.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/LinkedQueue.java index 8ed766b131..68ca420fa5 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/LinkedQueue.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/LinkedQueue.java @@ -23,7 +23,6 @@ import org.eclipse.jpt.common.utility.queue.Queue; public class LinkedQueue<E> implements Queue<E>, Cloneable, Serializable { - private final NodeFactory<E> nodeFactory; private transient Node<E> head; // next element to dequeue private transient Node<E> tail; // last element @@ -33,31 +32,10 @@ public class LinkedQueue<E> // ********** constructors ********** /** - * Construct an empty queue with no node cache. + * Construct an empty linked queue. */ public LinkedQueue() { - this(0); - } - - /** - * Construct an empty queue with a node cache with the specified size. - * Specify a cache size of -1 for an unlimited cache. - */ - public LinkedQueue(int cacheSize) { - this(LinkedQueue.<E>buildNodeFactory(cacheSize)); - this.head = null; - } - - private static <E> NodeFactory<E> buildNodeFactory(int cacheSize) { - if (cacheSize < -1) { - throw new IllegalArgumentException("Cache size must be greater than or equal to -1: " + cacheSize); //$NON-NLS-1$ - } - return (cacheSize == 0) ? SimpleNodeFactory.<E>instance() : new CachingNodeFactory<>(cacheSize); - } - - private LinkedQueue(NodeFactory<E> nodeFactory) { super(); - this.nodeFactory = nodeFactory; this.head = null; this.tail = null; } @@ -66,7 +44,7 @@ public class LinkedQueue<E> // ********** Queue implementation ********** public void enqueue(E element) { - Node<E> newNode = this.nodeFactory.buildNode(element, null); + Node<E> newNode = new Node<>(element, null); if (this.tail == null) { this.head = newNode; // first node } else { @@ -84,9 +62,7 @@ public class LinkedQueue<E> if (this.head == null) { this.tail = null; // last node } - E element = node.element; - this.nodeFactory.release(node); - return element; + return node.element; } public E peek() { @@ -105,7 +81,7 @@ public class LinkedQueue<E> @Override public LinkedQueue<E> clone() { - LinkedQueue<E> clone = new LinkedQueue<>(this.nodeFactory.copy()); + LinkedQueue<E> clone = new LinkedQueue<>(); E[] elements = this.buildElements(); for (E element : elements) { clone.enqueue(element); @@ -164,7 +140,7 @@ public class LinkedQueue<E> } - // ********** Node classes ********** + // ********** Node ********** private static final class Node<E> { E element; @@ -181,103 +157,4 @@ public class LinkedQueue<E> return ObjectTools.toString(this, this.element); } } - - private abstract static class NodeFactory<E> { - NodeFactory() { - super(); - } - - Node<E> buildNode(E element, Node<E> next) { - return new Node<>(element, next); - } - - abstract void release(Node<E> node); - - abstract NodeFactory<E> copy(); - } - - private static class SimpleNodeFactory<E> - extends NodeFactory<E> - implements Serializable - { - @SuppressWarnings("rawtypes") - public static final NodeFactory INSTANCE = new SimpleNodeFactory(); - @SuppressWarnings("unchecked") - public static <E> NodeFactory<E> instance() { - return INSTANCE; - } - - private SimpleNodeFactory() { - super(); - } - - @Override - void release(Node<E> node) { - // NOP - } - - @Override - NodeFactory<E> copy() { - return this; - } - - @Override - public String toString() { - return ObjectTools.singletonToString(this); - } - - private static final long serialVersionUID = 1L; - private Object readResolve() { - // replace this object with the singleton - return INSTANCE; - } - } - - private static final class CachingNodeFactory<E> - extends NodeFactory<E> - implements Serializable - { - private final int maxCacheSize; - private transient int cacheSize = 0; - private transient Node<E> cacheHead; - private static final long serialVersionUID = 1L; - - CachingNodeFactory(int maxCacheSize) { - super(); - this.maxCacheSize = maxCacheSize; - } - - @Override - Node<E> buildNode(E element, Node<E> next) { - if (this.cacheHead == null) { - return super.buildNode(element, next); - } - Node<E> node = this.cacheHead; - this.cacheHead = node.next; - this.cacheSize--; - node.element = element; - node.next = next; - return node; - } - - @Override - void release(Node<E> node) { - if ((this.maxCacheSize == -1) || (this.cacheSize < this.maxCacheSize)) { - node.element = null; // allow GC to work - node.next = this.cacheHead; - this.cacheHead = node; - this.cacheSize++; - } - } - - @Override - NodeFactory<E> copy() { - return new CachingNodeFactory<>(this.maxCacheSize); - } - - @Override - public String toString() { - return ObjectTools.toString(this, this.cacheSize); - } - } } diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/QueueTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/QueueTools.java index e36399c3e6..f632e5ebff 100644 --- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/QueueTools.java +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/queue/QueueTools.java @@ -270,51 +270,24 @@ public final class QueueTools { // ********** linked queue factory methods ********** /** - * Return an empty link-based FIFO queue with no node cache. + * Return an empty link-based FIFO queue. */ public static <E> LinkedQueue<E> linkedQueue() { - return linkedQueue(0); - } - - /** - * Return an empty link-based FIFO queue - * with the specified node cache size. - * Specify a cache size of -1 for an unlimited cache. - */ - public static <E> LinkedQueue<E> linkedQueue(int cacheSize) { - return new LinkedQueue<>(cacheSize); + return new LinkedQueue<>(); } /** * Return a link-based FIFO queue corresponding to the specified iterable. */ public static <E> LinkedQueue<E> linkedQueue(Iterable<? extends E> iterable) { - return linkedQueue(iterable, 0); - } - - /** - * Return a link-based FIFO queue corresponding to the specified iterable - * with the specified node cache size. - * Specify a cache size of -1 for an unlimited cache. - */ - public static <E> LinkedQueue<E> linkedQueue(Iterable<? extends E> iterable, int cacheSize) { - return linkedQueue(iterable.iterator(), cacheSize); + return linkedQueue(iterable.iterator()); } /** * Return a link-based FIFO queue corresponding to the specified iterator. */ public static <E> LinkedQueue<E> linkedQueue(Iterator<? extends E> iterator) { - return linkedQueue(iterator, 0); - } - - /** - * Return a link-based FIFO queue corresponding to the specified iterator - * with the specified node cache size. - * Specify a cache size of -1 for an unlimited cache. - */ - public static <E> LinkedQueue<E> linkedQueue(Iterator<? extends E> iterator, int cacheSize) { - LinkedQueue<E> result = QueueTools.linkedQueue(cacheSize); + LinkedQueue<E> result = linkedQueue(); enqueueAll(result, iterator); return result; } @@ -324,16 +297,7 @@ public final class QueueTools { */ @SafeVarargs public static <E> LinkedQueue<E> linkedQueue(E... array) { - return linkedQueue(array, 0); - } - - /** - * Return a link-based FIFO queue corresponding to the specified array - * with the specified node cache size. - * Specify a cache size of -1 for an unlimited cache. - */ - public static <E> LinkedQueue<E> linkedQueue(E[] array, int cacheSize) { - LinkedQueue<E> result = QueueTools.linkedQueue(cacheSize); + LinkedQueue<E> result = linkedQueue(); enqueueAll(result, array); return result; } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueue.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueue.java new file mode 100644 index 0000000000..ca1f9399a8 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueue.java @@ -0,0 +1,282 @@ +/******************************************************************************* + * Copyright (c) 2009, 2016 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.common.utility.tests.internal.queue; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.NoSuchElementException; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.queue.Queue; + +/** + * Linked FIFO implementation of the {@link Queue} interface. + * @param <E> the type of elements maintained by the queue + */ +public class CachingLinkedQueue<E> + implements Queue<E>, Cloneable, Serializable +{ + private final NodeFactory<E> nodeFactory; + private transient Node<E> head; // next element to dequeue + private transient Node<E> tail; // last element + + private static final long serialVersionUID = 1L; + + + // ********** constructors ********** + + /** + * Construct an empty queue with no node cache. + */ + public CachingLinkedQueue() { + this(0); + } + + /** + * Construct an empty queue with a node cache with the specified size. + * Specify a cache size of -1 for an unlimited cache. + */ + public CachingLinkedQueue(int cacheSize) { + this(CachingLinkedQueue.<E>buildNodeFactory(cacheSize)); + this.head = null; + } + + private static <E> NodeFactory<E> buildNodeFactory(int cacheSize) { + if (cacheSize < -1) { + throw new IllegalArgumentException("Cache size must be greater than or equal to -1: " + cacheSize); //$NON-NLS-1$ + } + return (cacheSize == 0) ? SimpleNodeFactory.<E>instance() : new CachingNodeFactory<>(cacheSize); + } + + private CachingLinkedQueue(NodeFactory<E> nodeFactory) { + super(); + this.nodeFactory = nodeFactory; + this.head = null; + this.tail = null; + } + + + // ********** Queue implementation ********** + + public void enqueue(E element) { + Node<E> newNode = this.nodeFactory.buildNode(element, null); + if (this.tail == null) { + this.head = newNode; // first node + } else { + this.tail.next = newNode; + } + this.tail = newNode; + } + + public E dequeue() { + if (this.head == null) { + throw new NoSuchElementException(); + } + Node<E> node = this.head; + this.head = node.next; + if (this.head == null) { + this.tail = null; // last node + } + E element = node.element; + this.nodeFactory.release(node); + return element; + } + + public E peek() { + if (this.head == null) { + throw new NoSuchElementException(); + } + return this.head.element; + } + + public boolean isEmpty() { + return this.head == null; + } + + + // ********** standard methods ********** + + @Override + public CachingLinkedQueue<E> clone() { + CachingLinkedQueue<E> clone = new CachingLinkedQueue<>(this.nodeFactory.copy()); + E[] elements = this.buildElements(); + for (E element : elements) { + clone.enqueue(element); + } + return clone; + } + + @SuppressWarnings("unchecked") + private E[] buildElements() { + int size = this.size(); + if (size == 0) { + return (E[]) ObjectTools.EMPTY_OBJECT_ARRAY; + } + E[] elements = (E[]) new Object[size]; + int i = 0; + for (Node<E> node = this.head; node != null; node = node.next) { + elements[i++] = node.element; + } + return elements; + } + + private int size() { + int size = 0; + for (Node<E> node = this.head; node != null; node = node.next) { + size++; + } + return size; + } + + @Override + public String toString() { + return Arrays.toString(this.buildElements()); + } + + + // ********** Serializable "implementation" ********** + + private void writeObject(java.io.ObjectOutputStream stream) throws java.io.IOException { + // write nodeFactory (and any hidden stuff) + stream.defaultWriteObject(); + Object[] elements = this.buildElements(); + stream.writeInt(elements.length); + for (Object element : elements) { + stream.writeObject(element); + } + } + + @SuppressWarnings("unchecked") + private void readObject(java.io.ObjectInputStream stream) throws java.io.IOException, ClassNotFoundException { + // read nodeFactory (and any hidden stuff) + stream.defaultReadObject(); + int len = stream.readInt(); + for (int i = len; i-- > 0; ) { + this.enqueue((E) stream.readObject()); + } + } + + + // ********** Node classes ********** + + private static final class Node<E> { + E element; + Node<E> next; + + Node(E element, Node<E> next) { + super(); + this.element = element; + this.next = next; + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.element); + } + } + + private abstract static class NodeFactory<E> { + NodeFactory() { + super(); + } + + Node<E> buildNode(E element, Node<E> next) { + return new Node<>(element, next); + } + + abstract void release(Node<E> node); + + abstract NodeFactory<E> copy(); + } + + private static class SimpleNodeFactory<E> + extends NodeFactory<E> + implements Serializable + { + @SuppressWarnings("rawtypes") + public static final NodeFactory INSTANCE = new SimpleNodeFactory(); + @SuppressWarnings("unchecked") + public static <E> NodeFactory<E> instance() { + return INSTANCE; + } + + private SimpleNodeFactory() { + super(); + } + + @Override + void release(Node<E> node) { + // NOP + } + + @Override + NodeFactory<E> copy() { + return this; + } + + @Override + public String toString() { + return ObjectTools.singletonToString(this); + } + + private static final long serialVersionUID = 1L; + private Object readResolve() { + // replace this object with the singleton + return INSTANCE; + } + } + + private static final class CachingNodeFactory<E> + extends NodeFactory<E> + implements Serializable + { + private final int maxCacheSize; + private transient int cacheSize = 0; + private transient Node<E> cacheHead; + private static final long serialVersionUID = 1L; + + CachingNodeFactory(int maxCacheSize) { + super(); + this.maxCacheSize = maxCacheSize; + } + + @Override + Node<E> buildNode(E element, Node<E> next) { + if (this.cacheHead == null) { + return super.buildNode(element, next); + } + Node<E> node = this.cacheHead; + this.cacheHead = node.next; + this.cacheSize--; + node.element = element; + node.next = next; + return node; + } + + @Override + void release(Node<E> node) { + if ((this.maxCacheSize == -1) || (this.cacheSize < this.maxCacheSize)) { + node.element = null; // allow GC to work + node.next = this.cacheHead; + this.cacheHead = node; + this.cacheSize++; + } + } + + @Override + NodeFactory<E> copy() { + return new CachingNodeFactory<>(this.maxCacheSize); + } + + @Override + public String toString() { + return ObjectTools.toString(this, this.cacheSize); + } + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueueTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueueTests.java new file mode 100644 index 0000000000..b1a894b69f --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/CachingLinkedQueueTests.java @@ -0,0 +1,228 @@ +/******************************************************************************* + * Copyright (c) 2012, 2016 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.common.utility.tests.internal.queue; + +import java.util.Arrays; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.queue.Queue; +import org.eclipse.jpt.common.utility.tests.internal.TestTools; + +@SuppressWarnings("nls") +public class CachingLinkedQueueTests + extends QueueTests +{ + public CachingLinkedQueueTests(String name) { + super(name); + } + + @Override + Queue<String> buildQueue() { + return new CachingLinkedQueue<>(); + } + + public void testConstructorInt_IAE() { + boolean exCaught = false; + try { + Queue<String> queue = new CachingLinkedQueue<>(-3); + fail("bogus queue: " + queue); + } catch (IllegalArgumentException ex) { + exCaught = true; + } + assertTrue(exCaught); + } + + public void testSize() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + String third = "third"; + + assertEquals(0, ((Integer) ObjectTools.execute(queue, "size")).intValue()); + queue.enqueue(first); + queue.enqueue(second); + assertEquals(2, ((Integer) ObjectTools.execute(queue, "size")).intValue()); + queue.enqueue(third); + assertEquals(3, ((Integer) ObjectTools.execute(queue, "size")).intValue()); + queue.dequeue(); + assertEquals(2, ((Integer) ObjectTools.execute(queue, "size")).intValue()); + queue.dequeue(); + queue.dequeue(); + assertEquals(0, ((Integer) ObjectTools.execute(queue, "size")).intValue()); + } + + public void testBuildElements() { + Queue<String> queue = this.buildQueue(); + String first = "first"; + String second = "second"; + String third = "third"; + queue.enqueue(first); + queue.enqueue(second); + queue.enqueue(third); + + Object[] elements = new Object[] { first, second, third }; + assertTrue(Arrays.equals(elements, ((Object[]) ObjectTools.execute(queue, "buildElements")))); + } + + public void testNodeCache_max() { + Queue<String> queue = new CachingLinkedQueue<>(2); + String first = "first"; + String second = "second"; + String third = "third"; + String fourth = "fourth"; + String fifth = "fifth"; + + Object factory = ObjectTools.get(queue, "nodeFactory"); + + this.verifyNodeCache(0, factory); + queue.enqueue(first); + this.verifyNodeCache(0, factory); + queue.enqueue(second); + queue.enqueue(third); + queue.enqueue(fourth); + queue.enqueue(fifth); + this.verifyNodeCache(0, factory); + assertNull(ObjectTools.get(factory, "cacheHead")); + + queue.dequeue(); + this.verifyNodeCache(1, factory); + queue.dequeue(); + this.verifyNodeCache(2, factory); + queue.dequeue(); + this.verifyNodeCache(2, factory); + queue.dequeue(); + this.verifyNodeCache(2, factory); + queue.dequeue(); + this.verifyNodeCache(2, factory); + queue.enqueue(first); + this.verifyNodeCache(1, factory); + queue.enqueue(second); + this.verifyNodeCache(0, factory); + queue.enqueue(third); + this.verifyNodeCache(0, factory); + } + + public void testNodeCache_unlimited() { + Queue<String> queue = new CachingLinkedQueue<>(-1); + String first = "first"; + String second = "second"; + String third = "third"; + String fourth = "fourth"; + String fifth = "fifth"; + + Object factory = ObjectTools.get(queue, "nodeFactory"); + + this.verifyNodeCache(0, factory); + queue.enqueue(first); + this.verifyNodeCache(0, factory); + queue.enqueue(second); + queue.enqueue(third); + queue.enqueue(fourth); + queue.enqueue(fifth); + this.verifyNodeCache(0, factory); + assertNull(ObjectTools.get(factory, "cacheHead")); + + queue.dequeue(); + this.verifyNodeCache(1, factory); + queue.dequeue(); + this.verifyNodeCache(2, factory); + queue.dequeue(); + this.verifyNodeCache(3, factory); + queue.dequeue(); + this.verifyNodeCache(4, factory); + queue.dequeue(); + this.verifyNodeCache(5, factory); + queue.enqueue(first); + this.verifyNodeCache(4, factory); + queue.enqueue(second); + this.verifyNodeCache(3, factory); + queue.enqueue(third); + this.verifyNodeCache(2, factory); + queue.enqueue(fourth); + this.verifyNodeCache(1, factory); + queue.enqueue(fifth); + this.verifyNodeCache(0, factory); + } + + public void verifyNodeCache(int size, Object factory) { + assertEquals(size, ((Integer) ObjectTools.get(factory, "cacheSize")).intValue()); + int nodeCount = 0; + for (Object node = ObjectTools.get(factory, "cacheHead"); node != null; node = ObjectTools.get(node, "next")) { + nodeCount++; + } + assertEquals(size, nodeCount); + } + + public void testNodeToString() { + Queue<String> queue = new CachingLinkedQueue<>(); + String first = "first"; + String second = "second"; + String third = "third"; + queue.enqueue(first); + queue.enqueue(second); + queue.enqueue(third); + + Object head = ObjectTools.get(queue, "head"); + assertTrue(head.toString().startsWith("CachingLinkedQueue.Node")); + assertTrue(head.toString().endsWith("(first)")); + } + + public void testSimpleNodeFactoryToString() { + Queue<String> queue = new CachingLinkedQueue<>(); + Object factory = ObjectTools.get(queue, "nodeFactory"); + assertEquals("CachingLinkedQueue.SimpleNodeFactory", factory.toString()); + } + + public void testCachingNodeFactoryToString() { + Queue<String> queue = new CachingLinkedQueue<>(20); + Object factory = ObjectTools.get(queue, "nodeFactory"); + assertTrue(factory.toString().startsWith("CachingLinkedQueue.CachingNodeFactory")); + assertTrue(factory.toString().endsWith("(0)")); + } + + public void testClone_caching() throws Exception { + CachingLinkedQueue<String> original = new CachingLinkedQueue<>(20); + original.enqueue("first"); + + CachingLinkedQueue<String> clone = original.clone(); + assertEquals(original.peek(), clone.peek()); + assertEquals(original.dequeue(), clone.dequeue()); + assertNotSame(original, clone); + assertTrue(original.isEmpty()); + assertEquals(original.isEmpty(), clone.isEmpty()); + + original.enqueue("second"); + assertFalse(original.isEmpty()); + // clone should still be empty + assertTrue(clone.isEmpty()); + + Object factory = ObjectTools.get(original, "nodeFactory"); + assertTrue(factory.toString().startsWith("CachingLinkedQueue.CachingNodeFactory")); + } + + public void testSerialization_caching() throws Exception { + Queue<String> original = new CachingLinkedQueue<>(20); + original.enqueue("first"); + + Queue<String> clone = TestTools.serialize(original); + assertEquals(original.peek(), clone.peek()); + assertEquals(original.dequeue(), clone.dequeue()); + assertNotSame(original, clone); + assertTrue(original.isEmpty()); + assertEquals(original.isEmpty(), clone.isEmpty()); + + original.enqueue("second"); + assertFalse(original.isEmpty()); + // clone should still be empty + assertTrue(clone.isEmpty()); + + Object factory = ObjectTools.get(original, "nodeFactory"); + assertTrue(factory.toString().startsWith("CachingLinkedQueue.CachingNodeFactory")); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/JptCommonUtilityQueueTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/JptCommonUtilityQueueTests.java index 82ef34512b..217cd6002c 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/JptCommonUtilityQueueTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/JptCommonUtilityQueueTests.java @@ -21,6 +21,7 @@ public class JptCommonUtilityQueueTests { TestSuite suite = new TestSuite(JptCommonUtilityQueueTests.class.getPackage().getName()); suite.addTestSuite(ArrayQueueTests.class); + suite.addTestSuite(CachingLinkedQueueTests.class); suite.addTestSuite(ConcurrentQueueTests.class); suite.addTestSuite(DequeQueueTests.class); suite.addTestSuite(EmptyQueueTests.class); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/LinkedQueueTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/LinkedQueueTests.java index bc1ef8a78c..9ec0885ac9 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/LinkedQueueTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/LinkedQueueTests.java @@ -11,10 +11,8 @@ package org.eclipse.jpt.common.utility.tests.internal.queue; import java.util.Arrays; import org.eclipse.jpt.common.utility.internal.ObjectTools; -import org.eclipse.jpt.common.utility.internal.queue.LinkedQueue; import org.eclipse.jpt.common.utility.internal.queue.QueueTools; import org.eclipse.jpt.common.utility.queue.Queue; -import org.eclipse.jpt.common.utility.tests.internal.TestTools; @SuppressWarnings("nls") public class LinkedQueueTests @@ -29,17 +27,6 @@ public class LinkedQueueTests return QueueTools.linkedQueue(); } - public void testConstructorInt_IAE() { - boolean exCaught = false; - try { - Queue<String> queue = QueueTools.linkedQueue(-3); - fail("bogus queue: " + queue); - } catch (IllegalArgumentException ex) { - exCaught = true; - } - assertTrue(exCaught); - } - public void testSize() { Queue<String> queue = this.buildQueue(); String first = "first"; @@ -72,95 +59,6 @@ public class LinkedQueueTests assertTrue(Arrays.equals(elements, ((Object[]) ObjectTools.execute(queue, "buildElements")))); } - public void testNodeCache_max() { - Queue<String> queue = new LinkedQueue<>(2); - String first = "first"; - String second = "second"; - String third = "third"; - String fourth = "fourth"; - String fifth = "fifth"; - - Object factory = ObjectTools.get(queue, "nodeFactory"); - - this.verifyNodeCache(0, factory); - queue.enqueue(first); - this.verifyNodeCache(0, factory); - queue.enqueue(second); - queue.enqueue(third); - queue.enqueue(fourth); - queue.enqueue(fifth); - this.verifyNodeCache(0, factory); - assertNull(ObjectTools.get(factory, "cacheHead")); - - queue.dequeue(); - this.verifyNodeCache(1, factory); - queue.dequeue(); - this.verifyNodeCache(2, factory); - queue.dequeue(); - this.verifyNodeCache(2, factory); - queue.dequeue(); - this.verifyNodeCache(2, factory); - queue.dequeue(); - this.verifyNodeCache(2, factory); - queue.enqueue(first); - this.verifyNodeCache(1, factory); - queue.enqueue(second); - this.verifyNodeCache(0, factory); - queue.enqueue(third); - this.verifyNodeCache(0, factory); - } - - public void testNodeCache_unlimited() { - Queue<String> queue = new LinkedQueue<>(-1); - String first = "first"; - String second = "second"; - String third = "third"; - String fourth = "fourth"; - String fifth = "fifth"; - - Object factory = ObjectTools.get(queue, "nodeFactory"); - - this.verifyNodeCache(0, factory); - queue.enqueue(first); - this.verifyNodeCache(0, factory); - queue.enqueue(second); - queue.enqueue(third); - queue.enqueue(fourth); - queue.enqueue(fifth); - this.verifyNodeCache(0, factory); - assertNull(ObjectTools.get(factory, "cacheHead")); - - queue.dequeue(); - this.verifyNodeCache(1, factory); - queue.dequeue(); - this.verifyNodeCache(2, factory); - queue.dequeue(); - this.verifyNodeCache(3, factory); - queue.dequeue(); - this.verifyNodeCache(4, factory); - queue.dequeue(); - this.verifyNodeCache(5, factory); - queue.enqueue(first); - this.verifyNodeCache(4, factory); - queue.enqueue(second); - this.verifyNodeCache(3, factory); - queue.enqueue(third); - this.verifyNodeCache(2, factory); - queue.enqueue(fourth); - this.verifyNodeCache(1, factory); - queue.enqueue(fifth); - this.verifyNodeCache(0, factory); - } - - public void verifyNodeCache(int size, Object factory) { - assertEquals(size, ((Integer) ObjectTools.get(factory, "cacheSize")).intValue()); - int nodeCount = 0; - for (Object node = ObjectTools.get(factory, "cacheHead"); node != null; node = ObjectTools.get(node, "next")) { - nodeCount++; - } - assertEquals(size, nodeCount); - } - public void testNodeToString() { Queue<String> queue = QueueTools.linkedQueue(); String first = "first"; @@ -174,57 +72,4 @@ public class LinkedQueueTests assertTrue(head.toString().startsWith("LinkedQueue.Node")); assertTrue(head.toString().endsWith("(first)")); } - - public void testSimpleNodeFactoryToString() { - Queue<String> queue = QueueTools.linkedQueue(); - Object factory = ObjectTools.get(queue, "nodeFactory"); - assertEquals("LinkedQueue.SimpleNodeFactory", factory.toString()); - } - - public void testCachingNodeFactoryToString() { - Queue<String> queue = QueueTools.linkedQueue(20); - Object factory = ObjectTools.get(queue, "nodeFactory"); - assertTrue(factory.toString().startsWith("LinkedQueue.CachingNodeFactory")); - assertTrue(factory.toString().endsWith("(0)")); - } - - public void testClone_caching() throws Exception { - LinkedQueue<String> original = QueueTools.linkedQueue(20); - original.enqueue("first"); - - LinkedQueue<String> clone = original.clone(); - assertEquals(original.peek(), clone.peek()); - assertEquals(original.dequeue(), clone.dequeue()); - assertNotSame(original, clone); - assertTrue(original.isEmpty()); - assertEquals(original.isEmpty(), clone.isEmpty()); - - original.enqueue("second"); - assertFalse(original.isEmpty()); - // clone should still be empty - assertTrue(clone.isEmpty()); - - Object factory = ObjectTools.get(original, "nodeFactory"); - assertTrue(factory.toString().startsWith("LinkedQueue.CachingNodeFactory")); - } - - public void testSerialization_caching() throws Exception { - Queue<String> original = QueueTools.linkedQueue(20); - original.enqueue("first"); - - Queue<String> clone = TestTools.serialize(original); - assertEquals(original.peek(), clone.peek()); - assertEquals(original.dequeue(), clone.dequeue()); - assertNotSame(original, clone); - assertTrue(original.isEmpty()); - assertEquals(original.isEmpty(), clone.isEmpty()); - - original.enqueue("second"); - assertFalse(original.isEmpty()); - // clone should still be empty - assertTrue(clone.isEmpty()); - - Object factory = ObjectTools.get(original, "nodeFactory"); - assertTrue(factory.toString().startsWith("LinkedQueue.CachingNodeFactory")); - } } diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/QueueToolsTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/QueueToolsTests.java index 852ed96a0a..534ee84d38 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/QueueToolsTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/queue/QueueToolsTests.java @@ -339,11 +339,6 @@ public class QueueToolsTests assertTrue(queue.isEmpty()); } - public void testLinkedQueueInt() { - LinkedQueue<String> queue = QueueTools.linkedQueue(20); - assertTrue(queue.isEmpty()); - } - public void testLinkedQueueIterable() { ArrayList<String> iterable = new ArrayList<>(); iterable.add("one"); @@ -355,17 +350,6 @@ public class QueueToolsTests assertEquals("three", queue.dequeue()); } - public void testLinkedQueueIterableInt() { - ArrayList<String> iterable = new ArrayList<>(); - iterable.add("one"); - iterable.add("two"); - iterable.add("three"); - Queue<String> queue = QueueTools.linkedQueue(iterable, 5); - assertEquals("one", queue.dequeue()); - assertEquals("two", queue.dequeue()); - assertEquals("three", queue.dequeue()); - } - public void testLinkedQueueIterator() { ArrayList<String> iterable = new ArrayList<>(); iterable.add("one"); @@ -377,17 +361,6 @@ public class QueueToolsTests assertEquals("three", queue.dequeue()); } - public void testLinkedQueueIteratorInt() { - ArrayList<String> iterable = new ArrayList<>(); - iterable.add("one"); - iterable.add("two"); - iterable.add("three"); - Queue<String> queue = QueueTools.linkedQueue(iterable.iterator(), 5); - assertEquals("one", queue.dequeue()); - assertEquals("two", queue.dequeue()); - assertEquals("three", queue.dequeue()); - } - public void testLinkedQueueArray() { ArrayList<String> iterable = new ArrayList<>(); iterable.add("one"); |