Add TransformedDeque
This commit is contained in:
parent
7f31df2d3a
commit
459e8c7f31
|
@ -0,0 +1,223 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4.deque;
|
||||
|
||||
import org.apache.commons.collections4.Transformer;
|
||||
import org.apache.commons.collections4.collection.TransformedCollection;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Deque;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* Decorates another {@link Deque} to transform objects that are added.
|
||||
* <p>
|
||||
* The add/offer methods are affected by this class.
|
||||
* Thus objects must be removed or searched for using their transformed form.
|
||||
* For example, if the transformation converts Strings to Integers, you must
|
||||
* use the Integer form to remove objects.
|
||||
* </p>
|
||||
*
|
||||
* @param <E> the type of elements held in this deque
|
||||
* @since 4.5
|
||||
*/
|
||||
|
||||
public class TransformedDeque<E> extends TransformedCollection<E> implements Deque<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -7901091318986132033L;
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming deque.
|
||||
* <p>
|
||||
* If there are any elements already in the deque being decorated, they
|
||||
* are NOT transformed.
|
||||
* Contrast this with {@link #transformedDeque(Deque, Transformer)}.
|
||||
*
|
||||
* @param <E> the type of the elements in the deque
|
||||
* @param deque the deque to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @return a new transformed Deque
|
||||
* @throws NullPointerException if deque or transformer is null
|
||||
*/
|
||||
public static <E> TransformedDeque<E> transformingDeque(final Deque<E> deque,
|
||||
final Transformer<? super E, ? extends E> transformer) {
|
||||
return new TransformedDeque<>(deque, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a transformed deque that will transform
|
||||
* existing contents of the specified deque.
|
||||
* <p>
|
||||
* If there are any elements already in the deque being decorated, they
|
||||
* will be transformed by this method.
|
||||
* Contrast this with {@link #transformingDeque(Deque, Transformer)}.
|
||||
*
|
||||
* @param <E> the type of the elements in the deque
|
||||
* @param deque the deque to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @return a new transformed Deque
|
||||
* @throws NullPointerException if deque or transformer is null
|
||||
* @since 4.0
|
||||
*/
|
||||
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
|
||||
final TransformedDeque<E> decorated = new TransformedDeque<>(deque, transformer);
|
||||
if (deque.size() > 0) {
|
||||
@SuppressWarnings("unchecked") // deque is type <E>
|
||||
final E[] values = (E[]) deque.toArray(); // NOPMD - false positive for generics
|
||||
deque.clear();
|
||||
for (final E value : values) {
|
||||
decorated.decorated().add(transformer.transform(value));
|
||||
}
|
||||
}
|
||||
return decorated;
|
||||
}
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the deque being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param deque the deque to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws NullPointerException if deque or transformer is null
|
||||
*/
|
||||
protected TransformedDeque(final Deque<E> deque, final Transformer<? super E, ? extends E> transformer) {
|
||||
super(deque, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated deque.
|
||||
*
|
||||
* @return the decorated deque
|
||||
*/
|
||||
protected Deque<E> getDeque() {
|
||||
return (Deque<E>) decorated();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addFirst(E e) {
|
||||
getDeque().addFirst(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLast(E e) {
|
||||
getDeque().addLast(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offerFirst(E e) {
|
||||
return getDeque().offerFirst(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offerLast(E e) {
|
||||
return getDeque().offerLast(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeFirst() {
|
||||
return getDeque().removeFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E removeLast() {
|
||||
return getDeque().removeLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pollFirst() {
|
||||
return getDeque().pollFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pollLast() {
|
||||
return getDeque().pollLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getFirst() {
|
||||
return getDeque().getFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getLast() {
|
||||
return getDeque().getLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peekFirst() {
|
||||
return getDeque().peekFirst();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peekLast() {
|
||||
return getDeque().peekLast();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeFirstOccurrence(Object o) {
|
||||
return getDeque().removeFirstOccurrence(transform((E) o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeLastOccurrence(Object o) {
|
||||
return getDeque().removeLastOccurrence(transform((E) o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean offer(E e) {
|
||||
return getDeque().offer(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove() {
|
||||
return getDeque().remove();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E poll() {
|
||||
return getDeque().poll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E element() {
|
||||
return getDeque().element();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E peek() {
|
||||
return getDeque().peek();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void push(E e) {
|
||||
getDeque().push(transform(e));
|
||||
}
|
||||
|
||||
@Override
|
||||
public E pop() {
|
||||
return getDeque().pop();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> descendingIterator() {
|
||||
return getDeque().descendingIterator();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,774 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4.deque;
|
||||
|
||||
import org.apache.commons.collections4.collection.AbstractCollectionTest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Abstract test class for {@link java.util.Deque} methods and contracts.
|
||||
* <p>
|
||||
* To use, simply extend this class, and implement
|
||||
* the {@link #makeObject} method.
|
||||
* <p>
|
||||
* If your {@link Deque} fails one of these tests by design,
|
||||
* you may still use this base set of cases. Simply override the
|
||||
* test case (method) your {@link Deque} fails or override one of the
|
||||
* protected methods from AbstractCollectionTest.
|
||||
*
|
||||
* @since 4.5
|
||||
*/
|
||||
public abstract class AbstractDequeTest<E> extends AbstractCollectionTest<E> {
|
||||
/**
|
||||
* JUnit constructor.
|
||||
*
|
||||
* @param testName the test class name
|
||||
*/
|
||||
public AbstractDequeTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the collections produced by
|
||||
* {@link #makeObject()} and {@link #makeFullCollection()}
|
||||
* support the <code>set operation.<p>
|
||||
* Default implementation returns true. Override if your collection
|
||||
* class does not support set.
|
||||
*/
|
||||
public boolean isSetSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Verifies that the test deque implementation matches the confirmed deque
|
||||
* implementation.
|
||||
*/
|
||||
@Override
|
||||
public void verify() {
|
||||
super.verify();
|
||||
final Iterator<E> iterator1 = getCollection().iterator();
|
||||
for (final E e : getConfirmed()) {
|
||||
assertTrue(iterator1.hasNext());
|
||||
final Object o1 = iterator1.next();
|
||||
final Object o2 = e;
|
||||
assertEquals(o1, o2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty {@link LinkedList}.
|
||||
*/
|
||||
@Override
|
||||
public Collection<E> makeConfirmedCollection() {
|
||||
final LinkedList<E> list = new LinkedList<>();
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a full {@link LinkedList}.
|
||||
*/
|
||||
@Override
|
||||
public Collection<E> makeConfirmedFullCollection() {
|
||||
final LinkedList<E> list = new LinkedList<>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@link #makeObject()}.
|
||||
*
|
||||
* @return an empty deque to be used for testing
|
||||
*/
|
||||
@Override
|
||||
public abstract Deque<E> makeObject();
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Deque<E> makeFullCollection() {
|
||||
// only works if Deque supports optional "addAll(Collection)"
|
||||
final Deque<E> deque = makeObject();
|
||||
deque.addAll(Arrays.asList(getFullElements()));
|
||||
return deque;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Returns the {@link #collection} field cast to a {@link Deque}.
|
||||
*
|
||||
* @return the collection field as a Deque
|
||||
*/
|
||||
@Override
|
||||
public Deque<E> getCollection() {
|
||||
return (Deque<E>) super.getCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#addFirst(E e)}.
|
||||
*/
|
||||
public void testDequeAddFirst(E e) {
|
||||
if (!isAddSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final E[] elements = getFullElements();
|
||||
for (final E element : elements) {
|
||||
resetEmpty();
|
||||
getCollection().addFirst(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
assertEquals("Deque size is 1 after first add", 1, getCollection().size());
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
int size = 0;
|
||||
for (final E element : elements) {
|
||||
getCollection().addFirst(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
size++;
|
||||
assertEquals("Deque size should grow after add", size, getCollection().size());
|
||||
assertTrue("Deque should contain added element", getCollection().contains(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#addLast(E e)}.
|
||||
*/
|
||||
public void testDequeAddLast(E e) {
|
||||
if (!isAddSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final E[] elements = getFullElements();
|
||||
for (final E element : elements) {
|
||||
resetEmpty();
|
||||
getCollection().addLast(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
assertEquals("Deque size is 1 after first add", 1, getCollection().size());
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
int size = 0;
|
||||
for (final E element : elements) {
|
||||
getCollection().addLast(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
size++;
|
||||
assertEquals("Deque size should grow after add", size, getCollection().size());
|
||||
assertTrue("Deque should contain added element", getCollection().contains(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#offerFirst(E e)}.
|
||||
*/
|
||||
public void testDequeOfferFirst(E e) {
|
||||
if (!isAddSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final E[] elements = getFullElements();
|
||||
for (final E element : elements) {
|
||||
resetEmpty();
|
||||
getCollection().offerFirst(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
assertEquals("Deque size is 1 after first add", 1, getCollection().size());
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
int size = 0;
|
||||
for (final E element : elements) {
|
||||
getCollection().addLast(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
size++;
|
||||
assertEquals("Deque size should grow after add", size, getCollection().size());
|
||||
assertTrue("Deque should contain added element", getCollection().contains(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#offerLast(E e)}.
|
||||
*/
|
||||
public void testDequeOfferLast(E e) {
|
||||
if (!isAddSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final E[] elements = getFullElements();
|
||||
for (final E element : elements) {
|
||||
resetEmpty();
|
||||
final boolean r = getCollection().offerLast(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
assertTrue("Empty deque changed after add", r);
|
||||
assertEquals("Deque size is 1 after first add", 1, getCollection().size());
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
int size = 0;
|
||||
for (final E element : elements) {
|
||||
final boolean r = getCollection().offerLast(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
if (r) {
|
||||
size++;
|
||||
}
|
||||
assertEquals("Deque size should grow after add", size, getCollection().size());
|
||||
assertTrue("Deque should contain added element", getCollection().contains(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#removeFirst()}.
|
||||
*/
|
||||
public void testDequeRemoveFirst() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().removeFirst();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().removeFirst();
|
||||
final boolean success = getConfirmed().remove(element);
|
||||
assertTrue("remove should return correct element", success);
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().element();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#removeLast()}.
|
||||
*/
|
||||
public void testDequeRemoveLast() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().removeLast();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().removeLast();
|
||||
LinkedList<E> list=(LinkedList<E>) getConfirmed();
|
||||
list.removeLast();
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().element();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#pollFirst()}.
|
||||
*/
|
||||
public void testDequePollFirst() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().pollFirst();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().pollFirst();
|
||||
final boolean success = getConfirmed().remove(element);
|
||||
assertTrue("poll should return correct element", success);
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().pollFirst();
|
||||
assertNull(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#pollLast()}.
|
||||
*/
|
||||
public void testDequePollLast() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().pollLast();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().pollLast();
|
||||
LinkedList<E> list=(LinkedList<E>) getConfirmed();
|
||||
list.removeLast();
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().pollLast();
|
||||
assertNull(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#getFirst()}.
|
||||
*/
|
||||
public void testDequeGetFirst() {
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().getFirst();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
assertTrue(getConfirmed().contains(getCollection().getFirst()));
|
||||
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().getFirst();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().getFirst();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#getLast()}.
|
||||
*/
|
||||
public void testDequeGetLast() {
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().getLast();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().getLast();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().getLast();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#peekFirst()}.
|
||||
*/
|
||||
public void testDequePeekFirst() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().peekFirst();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().peekFirst();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().peekFirst();
|
||||
assertNull(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#peekLast()}.
|
||||
*/
|
||||
public void testDequePeekLast() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().peekLast();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().peekLast();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().peekLast();
|
||||
assertNull(element);
|
||||
}
|
||||
/**
|
||||
* Tests {@link Deque#offer(Object)}.
|
||||
*/
|
||||
public void testDequeOffer() {
|
||||
if (!isAddSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final E[] elements = getFullElements();
|
||||
for (final E element : elements) {
|
||||
resetEmpty();
|
||||
final boolean r = getCollection().offer(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
assertTrue("Empty deque changed after add", r);
|
||||
assertEquals("Deque size is 1 after first add", 1, getCollection().size());
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
int size = 0;
|
||||
for (final E element : elements) {
|
||||
final boolean r = getCollection().offer(element);
|
||||
getConfirmed().add(element);
|
||||
verify();
|
||||
if (r) {
|
||||
size++;
|
||||
}
|
||||
assertEquals("Deque size should grow after add", size, getCollection().size());
|
||||
assertTrue("Deque should contain added element", getCollection().contains(element));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#element()}.
|
||||
*/
|
||||
public void testDequeElement() {
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().element();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
assertTrue(getConfirmed().contains(getCollection().element()));
|
||||
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().element();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().element();
|
||||
fail("Deque.element should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#peek()}.
|
||||
*/
|
||||
public void testDequePeek() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().peek();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().peek();
|
||||
|
||||
if (!isNullSupported()) {
|
||||
assertNotNull(element);
|
||||
}
|
||||
|
||||
assertTrue(getConfirmed().contains(element));
|
||||
|
||||
getCollection().remove(element);
|
||||
getConfirmed().remove(element);
|
||||
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().peek();
|
||||
assertNull(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#remove()}.
|
||||
*/
|
||||
public void testDequeRemove() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
try {
|
||||
getCollection().remove();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
final E element = getCollection().remove();
|
||||
final boolean success = getConfirmed().remove(element);
|
||||
assertTrue("remove should return correct element", success);
|
||||
verify();
|
||||
}
|
||||
|
||||
try {
|
||||
getCollection().element();
|
||||
fail("Deque.remove should throw NoSuchElementException");
|
||||
} catch (final NoSuchElementException e) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests {@link Deque#poll()}.
|
||||
*/
|
||||
public void testDequePoll() {
|
||||
if (!isRemoveSupported()) {
|
||||
return;
|
||||
}
|
||||
|
||||
resetEmpty();
|
||||
|
||||
E element = getCollection().poll();
|
||||
assertNull(element);
|
||||
|
||||
resetFull();
|
||||
|
||||
final int max = getFullElements().length;
|
||||
for (int i = 0; i < max; i++) {
|
||||
element = getCollection().poll();
|
||||
final boolean success = getConfirmed().remove(element);
|
||||
assertTrue("poll should return correct element", success);
|
||||
verify();
|
||||
}
|
||||
|
||||
element = getCollection().poll();
|
||||
assertNull(element);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEmptyDequeSerialization() throws IOException, ClassNotFoundException {
|
||||
final Deque<E> deque = makeObject();
|
||||
if (!(deque instanceof Serializable && isTestSerialization())) {
|
||||
return;
|
||||
}
|
||||
|
||||
final byte[] objekt = writeExternalFormToBytes((Serializable) deque);
|
||||
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt);
|
||||
|
||||
assertEquals("Both deques are empty", 0, deque.size());
|
||||
assertEquals("Both deques are empty", 0, deque2.size());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testFullDequeSerialization() throws IOException, ClassNotFoundException {
|
||||
final Deque<E> deque = makeFullCollection();
|
||||
final int size = getFullElements().length;
|
||||
if (!(deque instanceof Serializable && isTestSerialization())) {
|
||||
return;
|
||||
}
|
||||
|
||||
final byte[] objekt = writeExternalFormToBytes((Serializable) deque);
|
||||
final Deque<E> deque2 = (Deque<E>) readExternalFormFromBytes(objekt);
|
||||
|
||||
assertEquals("Both deques are same size", size, deque.size());
|
||||
assertEquals("Both deques are same size", size, deque2.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the current serialized form of the Deque
|
||||
* against the canonical version in SVN.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEmptyDequeCompatibility() throws IOException, ClassNotFoundException {
|
||||
/**
|
||||
* Create canonical objects with this code
|
||||
Deque deque = makeEmptyDeque();
|
||||
if (!(deque instanceof Serializable)) return;
|
||||
|
||||
writeExternalFormToDisk((Serializable) deque, getCanonicalEmptyCollectionName(deque));
|
||||
*/
|
||||
|
||||
// test to make sure the canonical form has been preserved
|
||||
final Deque<E> deque = makeObject();
|
||||
if (deque instanceof Serializable && !skipSerializedCanonicalTests()
|
||||
&& isTestSerialization()) {
|
||||
writeExternalFormToDisk((Serializable) deque, getCanonicalEmptyCollectionName(deque));
|
||||
final Deque<E> deque2 = (Deque<E>) readExternalFormFromDisk(getCanonicalEmptyCollectionName(deque));
|
||||
assertEquals("Deque is empty", 0, deque2.size());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare the current serialized form of the Deque
|
||||
* against the canonical version in SVN.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testFullDequeCompatibility() throws IOException, ClassNotFoundException {
|
||||
/**
|
||||
* Create canonical objects with this code
|
||||
Deque deque = makeFullDeque();
|
||||
if (!(deque instanceof Serializable)) return;
|
||||
|
||||
writeExternalFormToDisk((Serializable) deque, getCanonicalFullCollectionName(deque));
|
||||
*/
|
||||
|
||||
// test to make sure the canonical form has been preserved
|
||||
final Deque<E> deque = makeFullCollection();
|
||||
if(deque instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) {
|
||||
writeExternalFormToDisk((Serializable) deque, getCanonicalFullCollectionName(deque));
|
||||
final Deque<E> deque2 = (Deque<E>) readExternalFormFromDisk(getCanonicalFullCollectionName(deque));
|
||||
assertEquals("Deques are not the right size", deque.size(), deque2.size());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.collections4.deque;
|
||||
|
||||
import org.apache.commons.collections4.Transformer;
|
||||
import org.apache.commons.collections4.collection.TransformedCollectionTest;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
|
||||
|
||||
public class TransformedDequeTest<E> extends AbstractDequeTest<E> {
|
||||
|
||||
/**
|
||||
* JUnit constructor.
|
||||
*
|
||||
* @param testName the test class name
|
||||
*/
|
||||
public TransformedDequeTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Deque<E> makeConfirmedCollection() {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Deque<E> makeConfirmedFullCollection() {
|
||||
final Deque<E> list = new LinkedList<>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Deque<E> makeObject() {
|
||||
return TransformedDeque.transformingDeque(new LinkedList<E>(),
|
||||
(Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Deque<E> makeFullCollection() {
|
||||
final Deque<E> list = new LinkedList<>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return TransformedDeque.transformingDeque(list, (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
|
||||
}
|
||||
|
||||
public void testTransformedDeque() {
|
||||
final Deque<Object> deque = TransformedDeque.transformingDeque(new LinkedList<>(),
|
||||
TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(0, deque.size());
|
||||
final Object[] elements = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
|
||||
for (int i = 0; i < elements.length; i++) {
|
||||
deque.add(elements[i]);
|
||||
assertEquals(i + 1, deque.size());
|
||||
assertEquals(true, deque.contains(Integer.valueOf((String) elements[i])));
|
||||
assertEquals(false, deque.contains(elements[i]));
|
||||
}
|
||||
|
||||
assertEquals(false, deque.remove(elements[0]));
|
||||
assertEquals(true, deque.remove(Integer.valueOf((String) elements[0])));
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void testTransformedDeque_decorateTransform() {
|
||||
final Deque originalDeque = new LinkedList();
|
||||
final Object[] elements = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
|
||||
Collections.addAll(originalDeque, elements);
|
||||
final Deque<?> deque = TransformedDeque.transformedDeque(originalDeque,
|
||||
TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(elements.length, deque.size());
|
||||
for (final Object el : elements) {
|
||||
assertEquals(true, deque.contains(Integer.valueOf((String) el)));
|
||||
assertEquals(false, deque.contains(el));
|
||||
}
|
||||
|
||||
assertEquals(false, deque.remove(elements[0]));
|
||||
assertEquals(true, deque.remove(Integer.valueOf((String) elements[0])));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompatibilityVersion() {
|
||||
return "4.5";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue