Add support for deque interface

This commit is contained in:
dota17 2019-12-20 15:22:53 +08:00
parent c9500bb6bd
commit 896c4b7dd8
7 changed files with 31 additions and 20 deletions

View File

@ -40,6 +40,10 @@ import java.util.concurrent.locks.ReentrantLock;
* @since 4.5
*/
public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>, Serializable {
/** Serialization version */
private static final long serialVersionUID = 3961982897426944557L;
final Deque<E> deque;
final ReentrantLock lock = new ReentrantLock();
@ -116,7 +120,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.offerFirst(e)) == true)
if (r = deque.offerFirst(e))
notEmpty.signal();
return r;
} catch (Exception ex) {
@ -139,7 +143,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.offerLast(e)) == true)
if (r = deque.offerLast(e))
notEmpty.signal();
return r;
} catch (Exception ex) {
@ -330,7 +334,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.removeFirstOccurrence(o)) == true)
if (r = deque.removeFirstOccurrence(o))
notEmpty.signal();
return r;
} finally {
@ -343,7 +347,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.removeLastOccurrence(o)) == true)
if (r = deque.removeLastOccurrence(o))
notEmpty.signal();
return r;
} finally {
@ -412,7 +416,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.addAll(c)) == true)
if (r = deque.addAll(c))
notEmpty.signalAll();
return r;
} finally {
@ -425,7 +429,7 @@ public class BlockingDeque<E> extends AbstractCollection<E> implements Deque<E>,
lock.lock();
try {
boolean r;
if ((r = deque.removeAll(c)) == true)
if (r = deque.removeAll(c))
notFull.signalAll();
return r;
} finally {

View File

@ -56,6 +56,9 @@ import java.util.Iterator;
public class CircularDeque<E> extends AbstractCollection<E>
implements Deque<E>, BoundedCollection<E>, Serializable {
/** Serialization version */
private static final long serialVersionUID = 6013343683263686185L;
/** Underlying storage array. */
private transient E[] elements;
@ -469,7 +472,7 @@ public class CircularDeque<E> extends AbstractCollection<E>
System.arraycopy(elements, i + 1, elements, i, back);
tail--;
}
} else if (head >tail) {
} else if (head > tail) {
if (i > head) {
System.arraycopy(elements, head, elements, head + 1, front);
head++;

View File

@ -40,6 +40,10 @@ import java.util.Iterator;
* @since 4.5
*/
public class PredicatedDeque<E> extends PredicatedCollection<E> implements Deque<E> {
/** Serialization version */
private static final long serialVersionUID = 7027705675493389763L;
/**
* Factory method to create a predicated (validating) deque.
* <p>

View File

@ -34,7 +34,7 @@ import java.util.Iterator;
public class SynchronizedDeque<E> extends SynchronizedCollection<E> implements Deque<E> {
/** Serialization version */
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 4565577797338001569L;
/**
* Factory method to create a synchronized deque.

View File

@ -39,7 +39,7 @@ import java.util.Iterator;
public class TransformedDeque<E> extends TransformedCollection<E> implements Deque<E> {
/** Serialization version */
private static final long serialVersionUID = -7901091318986132033L;
private static final long serialVersionUID = 7959728067506831816L;
/**
* Factory method to create a transforming deque.
@ -76,7 +76,7 @@ public class TransformedDeque<E> extends TransformedCollection<E> implements Deq
*/
public static <E> TransformedDeque<E> transformedDeque(final Deque<E> deque,
final Transformer<? super E, ? extends E> transformer) {
// throws IAE if deque or transformer is null
// throws NullPointerException if deque or transformer is null
final TransformedDeque<E> decorated = new TransformedDeque<>(deque, transformer);
if (deque.size() > 0) {
@SuppressWarnings("unchecked") // deque is type <E>

View File

@ -750,8 +750,8 @@ public abstract class AbstractDequeTest<E> extends AbstractCollectionTest<E> {
return;
}
final byte[] objekt = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt);
final byte[] object = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(object);
assertEquals("Both deques are empty", 0, deque.size());
assertEquals("Both deques are empty", 0, deque2.size());
@ -765,8 +765,8 @@ public abstract class AbstractDequeTest<E> extends AbstractCollectionTest<E> {
return;
}
final byte[] objekt = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt);
final byte[] object = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(object);
assertEquals("Both deques are same size", size, deque.size());
assertEquals("Both deques are same size", size, deque2.size());

View File

@ -81,10 +81,10 @@ public class CircularDequeTest<E> extends AbstractDequeTest<E> {
public void testConstructorException1() {
try {
new CircularDeque<E>(0);
fail();
} catch (final IllegalArgumentException ex) {
return;
}
fail();
}
/**
@ -93,10 +93,10 @@ public class CircularDequeTest<E> extends AbstractDequeTest<E> {
public void testConstructorException2() {
try {
new CircularDeque<E>(-20);
fail();
} catch (final IllegalArgumentException ex) {
return;
}
fail();
}
/**
@ -105,10 +105,10 @@ public class CircularDequeTest<E> extends AbstractDequeTest<E> {
public void testConstructorException3() {
try {
new CircularDeque<E>(null);
fail();
} catch (final NullPointerException ex) {
return;
}
fail();
}
/**