Make PriorityBuffer Serializable
bug 36163, from Steve Phelps git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@307292 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f836ff09ee
commit
4fd151ac2d
|
@ -71,6 +71,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
|
|||
<li>BlockingBuffer - now includes stack trace if InterupttedException occurs [33700]</li>
|
||||
<li>BlockingBuffer - new methods that allow get and remove with a timeout [27691]</li>
|
||||
<li>Transformed*Map - new factory decorateTransform() that transforms any existing entries in the map [30959]</li>
|
||||
<li>PriorityBuffer - now Serializable [36163]</li>
|
||||
</ul>
|
||||
|
||||
<center><h3>BUG FIXES</h3></center>
|
||||
|
|
Binary file not shown.
Binary file not shown.
|
@ -15,6 +15,7 @@
|
|||
*/
|
||||
package org.apache.commons.collections.buffer;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.AbstractCollection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
|
@ -57,8 +58,13 @@ import org.apache.commons.collections.BufferUnderflowException;
|
|||
* @author Michael A. Smith
|
||||
* @author Paul Jack
|
||||
* @author Stephen Colebourne
|
||||
* @author Steve Phelps
|
||||
*/
|
||||
public class PriorityBuffer extends AbstractCollection implements Buffer {
|
||||
public class PriorityBuffer extends AbstractCollection
|
||||
implements Buffer, Serializable {
|
||||
|
||||
/** Serialization lock. */
|
||||
private static final long serialVersionUID = 6891186490470027896L;
|
||||
|
||||
/**
|
||||
* The default capacity for the buffer.
|
||||
|
|
|
@ -15,10 +15,12 @@
|
|||
*/
|
||||
package org.apache.commons.collections.buffer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.Random;
|
||||
|
||||
import junit.framework.Test;
|
||||
|
@ -37,6 +39,7 @@ import org.apache.commons.collections.comparators.ReverseComparator;
|
|||
* @version $Revision$ $Date$
|
||||
*
|
||||
* @author Michael A. Smith
|
||||
* @author Steve Phelps
|
||||
*/
|
||||
public class TestPriorityBuffer extends AbstractTestCollection {
|
||||
|
||||
|
@ -306,7 +309,6 @@ public class TestPriorityBuffer extends AbstractTestCollection {
|
|||
protected void checkOrder(PriorityBuffer h) {
|
||||
Integer lastNum = null;
|
||||
Integer num = null;
|
||||
boolean fail = false;
|
||||
while (!h.isEmpty()) {
|
||||
num = (Integer) h.remove();
|
||||
if (h.ascendingOrder) {
|
||||
|
@ -336,5 +338,70 @@ public class TestPriorityBuffer extends AbstractTestCollection {
|
|||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Generates 500 randomly initialized heaps of size 100
|
||||
* and tests that after serializing and restoring them to a byte array
|
||||
* that the following conditions hold:
|
||||
*
|
||||
* - the size of the restored heap is the same
|
||||
* as the size of the orignal heap
|
||||
*
|
||||
* - all elements in the original heap are present in the restored heap
|
||||
*
|
||||
* - the heap order of the restored heap is intact as
|
||||
* verified by checkOrder()
|
||||
*/
|
||||
public void testSerialization() {
|
||||
int iterations = 500;
|
||||
int heapSize = 100;
|
||||
PriorityBuffer h;
|
||||
Random randGenerator = new Random();
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
if (i < iterations / 2) {
|
||||
h = new PriorityBuffer(true);
|
||||
} else {
|
||||
h = new PriorityBuffer(false);
|
||||
}
|
||||
for (int r = 0; r < heapSize; r++) {
|
||||
h.add(new Integer(randGenerator.nextInt(heapSize)));
|
||||
}
|
||||
assertTrue(h.size() == heapSize);
|
||||
PriorityBuffer h1 = serializeAndRestore(h);
|
||||
assertTrue(h1.size() == heapSize);
|
||||
Iterator hit = h.iterator();
|
||||
while (hit.hasNext()) {
|
||||
Integer n = (Integer) hit.next();
|
||||
assertTrue(h1.contains(n));
|
||||
}
|
||||
checkOrder(h1);
|
||||
}
|
||||
}
|
||||
|
||||
public PriorityBuffer serializeAndRestore(PriorityBuffer h) {
|
||||
PriorityBuffer h1 = null;
|
||||
try {
|
||||
byte[] objekt = writeExternalFormToBytes(h);
|
||||
h1 = (PriorityBuffer) readExternalFormFromBytes(objekt);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.toString());
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
fail(e.toString());
|
||||
}
|
||||
return h1;
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
return "3.2";
|
||||
}
|
||||
|
||||
// public void testCreate() throws Exception {
|
||||
// resetEmpty();
|
||||
// writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/PriorityBuffer.emptyCollection.version3.2.obj");
|
||||
// resetFull();
|
||||
// writeExternalFormToDisk((java.io.Serializable) collection, "C:/commons/collections/data/test/PriorityBuffer.fullCollection.version3.2.obj");
|
||||
// }
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue