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

View File

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

View File

@ -40,6 +40,10 @@ import java.util.Iterator;
* @since 4.5 * @since 4.5
*/ */
public class PredicatedDeque<E> extends PredicatedCollection<E> implements Deque<E> { 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. * Factory method to create a predicated (validating) deque.
* <p> * <p>

View File

@ -34,7 +34,7 @@ import java.util.Iterator;
public class SynchronizedDeque<E> extends SynchronizedCollection<E> implements Deque<E> { public class SynchronizedDeque<E> extends SynchronizedCollection<E> implements Deque<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 4565577797338001569L;
/** /**
* Factory method to create a synchronized deque. * 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> { public class TransformedDeque<E> extends TransformedCollection<E> implements Deque<E> {
/** Serialization version */ /** Serialization version */
private static final long serialVersionUID = -7901091318986132033L; private static final long serialVersionUID = 7959728067506831816L;
/** /**
* Factory method to create a transforming deque. * 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, public static <E> TransformedDeque<E> transformedDeque(final Deque<E> deque,
final Transformer<? super E, ? extends E> transformer) { 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); final TransformedDeque<E> decorated = new TransformedDeque<>(deque, transformer);
if (deque.size() > 0) { if (deque.size() > 0) {
@SuppressWarnings("unchecked") // deque is type <E> @SuppressWarnings("unchecked") // deque is type <E>

View File

@ -750,8 +750,8 @@ public abstract class AbstractDequeTest<E> extends AbstractCollectionTest<E> {
return; return;
} }
final byte[] objekt = writeExternalFormToBytes((Serializable) deque); final byte[] object = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt); final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(object);
assertEquals("Both deques are empty", 0, deque.size()); assertEquals("Both deques are empty", 0, deque.size());
assertEquals("Both deques are empty", 0, deque2.size()); assertEquals("Both deques are empty", 0, deque2.size());
@ -765,8 +765,8 @@ public abstract class AbstractDequeTest<E> extends AbstractCollectionTest<E> {
return; return;
} }
final byte[] objekt = writeExternalFormToBytes((Serializable) deque); final byte[] object = writeExternalFormToBytes((Serializable) deque);
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt); final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(object);
assertEquals("Both deques are same size", size, deque.size()); assertEquals("Both deques are same size", size, deque.size());
assertEquals("Both deques are same size", size, deque2.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() { public void testConstructorException1() {
try { try {
new CircularDeque<E>(0); new CircularDeque<E>(0);
} catch (final IllegalArgumentException ex) {
return;
}
fail(); fail();
} catch (final IllegalArgumentException ex) {
}
} }
/** /**
@ -93,10 +93,10 @@ public class CircularDequeTest<E> extends AbstractDequeTest<E> {
public void testConstructorException2() { public void testConstructorException2() {
try { try {
new CircularDeque<E>(-20); new CircularDeque<E>(-20);
} catch (final IllegalArgumentException ex) {
return;
}
fail(); fail();
} catch (final IllegalArgumentException ex) {
}
} }
/** /**
@ -105,10 +105,10 @@ public class CircularDequeTest<E> extends AbstractDequeTest<E> {
public void testConstructorException3() { public void testConstructorException3() {
try { try {
new CircularDeque<E>(null); new CircularDeque<E>(null);
} catch (final NullPointerException ex) {
return;
}
fail(); fail();
} catch (final NullPointerException ex) {
}
} }
/** /**