[COLLECTIONS-432] Add queue decorators.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1457501 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0ec35de2c5
commit
a1179b9c7d
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
* 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.collections;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.queue.PredicatedQueue;
|
||||
import org.apache.commons.collections.queue.TransformedQueue;
|
||||
import org.apache.commons.collections.queue.UnmodifiableQueue;
|
||||
|
||||
/**
|
||||
* Provides utility methods and decorators for {@link Queue} instances.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class QueueUtils {
|
||||
|
||||
/**
|
||||
* An empty unmodifiable queue.
|
||||
*/
|
||||
public static final Queue<Object> EMPTY_QUEUE = UnmodifiableQueue.unmodifiableQueue(new LinkedList<Object>());
|
||||
|
||||
/**
|
||||
* <code>QueueUtils</code> should not normally be instantiated.
|
||||
*/
|
||||
private QueueUtils() {}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns an unmodifiable queue backed by the given queue.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to make unmodifiable, must not be null
|
||||
* @return an unmodifiable queue backed by that queue
|
||||
* @throws IllegalArgumentException if the Queue is null
|
||||
*/
|
||||
public static <E> Queue<E> unmodifiableQueue(final Queue<E> queue) {
|
||||
return UnmodifiableQueue.unmodifiableQueue(queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a predicated (validating) queue backed by the given queue.
|
||||
* <p>
|
||||
* Only objects that pass the test in the given predicate can be added to the queue.
|
||||
* Trying to add an invalid object results in an IllegalArgumentException.
|
||||
* It is important not to use the original queue after invoking this method,
|
||||
* as it is a backdoor for adding invalid objects.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to predicate, must not be null
|
||||
* @param predicate the predicate used to evaluate new elements, must not be null
|
||||
* @return a predicated queue
|
||||
* @throws IllegalArgumentException if the Queue or Predicate is null
|
||||
*/
|
||||
public static <E> Queue<E> predicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
|
||||
return PredicatedQueue.predicatedQueue(queue, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a transformed queue backed by the given queue.
|
||||
* <p>
|
||||
* Each object is passed through the transformer as it is added to the
|
||||
* Queue. It is important not to use the original queue after invoking this
|
||||
* method, as it is a backdoor for adding untransformed objects.
|
||||
* <p>
|
||||
* Existing entries in the specified queue will not be transformed.
|
||||
* If you want that behaviour, see {@link TransformedQueue#transformedQueue}.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to predicate, must not be null
|
||||
* @param transformer the transformer for the queue, must not be null
|
||||
* @return a transformed queue backed by the given queue
|
||||
* @throws IllegalArgumentException if the Queue or Transformer is null
|
||||
*/
|
||||
public static <E> Queue<E> transformingQueue(final Queue<E> queue,
|
||||
final Transformer<? super E, ? extends E> transformer) {
|
||||
return TransformedQueue.transformingQueue(queue, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an empty <code>Queue</code>.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @return an empty {@link Queue}
|
||||
*/
|
||||
@SuppressWarnings("unchecked") // OK, empty queue is compatible with any type
|
||||
public static <E> Queue<E> emptyQueue() {
|
||||
return (Queue<E>) EMPTY_QUEUE;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.collection.AbstractCollectionDecorator;
|
||||
|
||||
/**
|
||||
* Decorates another {@link Queue} to provide additional behaviour.
|
||||
* <p>
|
||||
* Methods are forwarded directly to the decorated queue.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public abstract class AbstractQueueDecorator<E> extends AbstractCollectionDecorator<E>
|
||||
implements Queue<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -2629815475789577029L;
|
||||
|
||||
/**
|
||||
* Constructor only used in deserialization, do not use otherwise.
|
||||
*/
|
||||
protected AbstractQueueDecorator() {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
*
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @throws IllegalArgumentException if list is null
|
||||
*/
|
||||
protected AbstractQueueDecorator(final Queue<E> queue) {
|
||||
super(queue);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the queue being decorated.
|
||||
*
|
||||
* @return the decorated queue
|
||||
*/
|
||||
@Override
|
||||
protected Queue<E> decorated() {
|
||||
return (Queue<E>) super.decorated();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
public boolean offer(E obj) {
|
||||
return decorated().offer(obj);
|
||||
}
|
||||
|
||||
public E poll() {
|
||||
return decorated().poll();
|
||||
}
|
||||
|
||||
public E peek() {
|
||||
return decorated().peek();
|
||||
}
|
||||
|
||||
public E element() {
|
||||
return decorated().element();
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
return decorated().remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.Predicate;
|
||||
import org.apache.commons.collections.collection.PredicatedCollection;
|
||||
|
||||
/**
|
||||
* Decorates another {@link Queue} to validate that additions
|
||||
* match a specified predicate.
|
||||
* <p>
|
||||
* This queue exists to provide validation for the decorated queue.
|
||||
* It is normally created to decorate an empty queue.
|
||||
* If an object cannot be added to the queue, an IllegalArgumentException is thrown.
|
||||
* <p>
|
||||
* One usage would be to ensure that no null entries are added to the queue.
|
||||
* <pre>Queue queue = PredicatedQueue.predicatedQueue(new UnboundedFifoQueue(), NotNullPredicate.INSTANCE);</pre>
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class PredicatedQueue<E> extends PredicatedCollection<E> implements Queue<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = 2307609000539943581L;
|
||||
|
||||
/**
|
||||
* Factory method to create a predicated (validating) queue.
|
||||
* <p>
|
||||
* If there are any elements already in the queue being decorated, they
|
||||
* are validated.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param Queue the queue to decorate, must not be null
|
||||
* @param predicate the predicate to use for validation, must not be null
|
||||
* @return a new predicated queue
|
||||
* @throws IllegalArgumentException if queue or predicate is null
|
||||
* @throws IllegalArgumentException if the queue contains invalid elements
|
||||
*/
|
||||
public static <E> PredicatedQueue<E> predicatedQueue(final Queue<E> Queue,
|
||||
final Predicate<? super E> predicate) {
|
||||
return new PredicatedQueue<E>(Queue, predicate);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
* <p>
|
||||
* If there are any elements already in the collection being decorated, they
|
||||
* are validated.
|
||||
*
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @param predicate the predicate to use for validation, must not be null
|
||||
* @throws IllegalArgumentException if Queue or predicate is null
|
||||
* @throws IllegalArgumentException if the Queue contains invalid elements
|
||||
*/
|
||||
protected PredicatedQueue(final Queue<E> queue, final Predicate<? super E> predicate) {
|
||||
super(queue, predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the queue being decorated.
|
||||
*
|
||||
* @return the decorated queue
|
||||
*/
|
||||
@Override
|
||||
protected Queue<E> decorated() {
|
||||
return (Queue<E>) super.decorated();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Override to validate the object being added to ensure it matches
|
||||
* the predicate.
|
||||
*
|
||||
* @param object the object being added
|
||||
* @return the result of adding to the underlying queue
|
||||
* @throws IllegalArgumentException if the add is invalid
|
||||
*/
|
||||
public boolean offer(E object) {
|
||||
validate(object);
|
||||
return decorated().offer(object);
|
||||
}
|
||||
|
||||
public E poll() {
|
||||
return decorated().poll();
|
||||
}
|
||||
|
||||
public E peek() {
|
||||
return decorated().peek();
|
||||
}
|
||||
|
||||
public E element() {
|
||||
return decorated().element();
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
return decorated().remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
import org.apache.commons.collections.collection.TransformedCollection;
|
||||
|
||||
/**
|
||||
* Decorates another {@link Queue} 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.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class TransformedQueue<E> extends TransformedCollection<E> implements Queue<E> {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = -7901091318986132033L;
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming queue.
|
||||
* <p>
|
||||
* If there are any elements already in the queue being decorated, they
|
||||
* are NOT transformed.
|
||||
* Contrast this with {@link #transformedQueue(Queue, Transformer)}.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @return a new transformed Queue
|
||||
* @throws IllegalArgumentException if queue or transformer is null
|
||||
*/
|
||||
public static <E> TransformedQueue<E> transformingQueue(final Queue<E> queue,
|
||||
final Transformer<? super E, ? extends E> transformer) {
|
||||
return new TransformedQueue<E>(queue, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a transforming queue that will transform
|
||||
* existing contents of the specified queue.
|
||||
* <p>
|
||||
* If there are any elements already in the queue being decorated, they
|
||||
* will be transformed by this method.
|
||||
* Contrast this with {@link #transformingQueue(Queue, Transformer)}.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @return a new transformed Queue
|
||||
* @throws IllegalArgumentException if queue or transformer is null
|
||||
* @since 4.0
|
||||
*/
|
||||
public static <E> TransformedQueue<E> transformedQueue(final Queue<E> queue,
|
||||
final Transformer<? super E, ? extends E> transformer) {
|
||||
// throws IAE if queue or transformer is null
|
||||
final TransformedQueue<E> decorated = new TransformedQueue<E>(queue, transformer);
|
||||
if (queue.size() > 0) {
|
||||
@SuppressWarnings("unchecked") // queue is type <E>
|
||||
final E[] values = (E[]) queue.toArray();
|
||||
queue.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 queue being decorated, they
|
||||
* are NOT transformed.
|
||||
*
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @param transformer the transformer to use for conversion, must not be null
|
||||
* @throws IllegalArgumentException if queue or transformer is null
|
||||
*/
|
||||
protected TransformedQueue(final Queue<E> queue, final Transformer<? super E, ? extends E> transformer) {
|
||||
super(queue, transformer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the decorated queue.
|
||||
*
|
||||
* @return the decorated queue
|
||||
*/
|
||||
protected Queue<E> getQueue() {
|
||||
return (Queue<E>) collection;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
public boolean offer(E obj) {
|
||||
return getQueue().offer(transform(obj));
|
||||
}
|
||||
|
||||
public E poll() {
|
||||
return getQueue().poll();
|
||||
}
|
||||
|
||||
public E peek() {
|
||||
return getQueue().peek();
|
||||
}
|
||||
|
||||
public E element() {
|
||||
return getQueue().element();
|
||||
}
|
||||
|
||||
public E remove() {
|
||||
return getQueue().remove();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.Unmodifiable;
|
||||
import org.apache.commons.collections.iterators.UnmodifiableIterator;
|
||||
|
||||
/**
|
||||
* Decorates another {@link Queue} to ensure it can't be altered.
|
||||
* <p>
|
||||
* Attempts to modify it will result in an UnsupportedOperationException.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public final class UnmodifiableQueue<E>
|
||||
extends AbstractQueueDecorator<E>
|
||||
implements Unmodifiable, Serializable {
|
||||
|
||||
/** Serialization version */
|
||||
private static final long serialVersionUID = 1832948656215393357L;
|
||||
|
||||
/**
|
||||
* Factory method to create an unmodifiable queue.
|
||||
* <p>
|
||||
* If the queue passed in is already unmodifiable, it is returned.
|
||||
*
|
||||
* @param <E> the type of the elements in the queue
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @return an unmodifiable Queue
|
||||
* @throws IllegalArgumentException if queue is null
|
||||
*/
|
||||
public static <E> Queue<E> unmodifiableQueue(final Queue<E> queue) {
|
||||
if (queue instanceof Unmodifiable) {
|
||||
return queue;
|
||||
}
|
||||
return new UnmodifiableQueue<E>(queue);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Constructor that wraps (not copies).
|
||||
*
|
||||
* @param queue the queue to decorate, must not be null
|
||||
* @throws IllegalArgumentException if queue is null
|
||||
*/
|
||||
private UnmodifiableQueue(final Queue<E> queue) {
|
||||
super(queue);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
/**
|
||||
* Write the collection out using a custom routine.
|
||||
*
|
||||
* @param out the output stream
|
||||
* @throws IOException if an I/O error occurs while writing to the output stream
|
||||
*/
|
||||
private void writeObject(final ObjectOutputStream out) throws IOException {
|
||||
out.defaultWriteObject();
|
||||
out.writeObject(collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the collection in using a custom routine.
|
||||
*
|
||||
* @param in the input stream
|
||||
* @throws IOException if an I/O error occurs while reading from the input stream
|
||||
* @throws ClassNotFoundException if the class of a serialized object can not be found
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
|
||||
in.defaultReadObject();
|
||||
collection = (Collection<E>) in.readObject();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return UnmodifiableIterator.unmodifiableIterator(decorated().iterator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean add(final Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addAll(final Collection<? extends E> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean remove(final Object object) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removeAll(final Collection<?> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean retainAll(final Collection<?> coll) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
@Override
|
||||
public boolean offer(E obj) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E poll() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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.
|
||||
*/
|
||||
/**
|
||||
* This package contains decorators for the {@link java.util.Queue Queue} interface.
|
||||
* <p>
|
||||
* The following decorators are provided in the package:
|
||||
* <ul>
|
||||
* <li>Predicated - ensures that only elements that are valid according to a predicate can be added
|
||||
* <li>Transformed - transforms elements added to the queue
|
||||
* <li>Unmodifiable - ensures the collection cannot be altered
|
||||
* </ul>
|
||||
*
|
||||
* @version $Id$
|
||||
*/
|
||||
package org.apache.commons.collections.queue;
|
|
@ -0,0 +1,109 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.Predicate;
|
||||
import org.apache.commons.collections.collection.PredicatedCollectionTest;
|
||||
|
||||
/**
|
||||
* Extension of {@link PredicatedCollectionTest} for exercising the
|
||||
* {@link PredicatedQueue} implementation.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class PredicatedQueueTest<E> extends PredicatedCollectionTest<E> {
|
||||
|
||||
public PredicatedQueueTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
//---------------------------------------------------------------
|
||||
|
||||
protected Queue<E> decorateCollection(final Queue<E> queue, final Predicate<E> predicate) {
|
||||
return PredicatedQueue.predicatedQueue(queue, predicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queue<E> makeObject() {
|
||||
return decorateCollection(new LinkedList<E>(), truePredicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeFullCollection() {
|
||||
final Queue<E> queue = new LinkedList<E>();
|
||||
queue.addAll(Arrays.asList(getFullElements()));
|
||||
return decorateCollection(queue, truePredicate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeConfirmedCollection() {
|
||||
return new LinkedList<E>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeConfirmedFullCollection() {
|
||||
final List<E> list = new LinkedList<E>();
|
||||
list.addAll(java.util.Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
|
||||
public Queue<E> makeTestQueue() {
|
||||
return decorateCollection(new LinkedList<E>(), testPredicate);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testGet() {
|
||||
final Queue<E> queue = makeTestQueue();
|
||||
|
||||
assertNull(queue.peek());
|
||||
|
||||
queue.add((E) "one");
|
||||
queue.add((E) "two");
|
||||
queue.add((E) "three");
|
||||
assertEquals("Queue get", "one", queue.peek());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testRemove() {
|
||||
final Queue<E> queue = makeTestQueue();
|
||||
queue.add((E) "one");
|
||||
assertEquals("Queue get", "one", queue.poll());
|
||||
assertNull(queue.peek());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompatibilityVersion() {
|
||||
return "4";
|
||||
}
|
||||
|
||||
// public void testCreate() throws Exception {
|
||||
// resetEmpty();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedQueue.emptyCollection.version4.obj");
|
||||
// resetFull();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/PredicatedQueue.fullCollection.version4.obj");
|
||||
// }
|
||||
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.Transformer;
|
||||
import org.apache.commons.collections.collection.AbstractCollectionTest;
|
||||
import org.apache.commons.collections.collection.TransformedCollectionTest;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractCollectionTest} for exercising the
|
||||
* {@link TransformedQueue} implementation.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class TransformedQueueTest<E> extends AbstractCollectionTest<E> {
|
||||
|
||||
public TransformedQueueTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Override
|
||||
public Queue<E> makeConfirmedCollection() {
|
||||
return new LinkedList<E>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Queue<E> makeConfirmedFullCollection() {
|
||||
final Queue<E> list = new LinkedList<E>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Queue<E> makeObject() {
|
||||
return TransformedQueue.transformingQueue(new LinkedList<E>(),
|
||||
(Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Queue<E> makeFullCollection() {
|
||||
final Queue<E> list = new LinkedList<E>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return TransformedQueue.transformingQueue(list, (Transformer<E, E>) TransformedCollectionTest.NOOP_TRANSFORMER);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
public void testTransformedQueue() {
|
||||
final Queue<Object> queue = TransformedQueue.transformingQueue(new LinkedList<Object>(),
|
||||
TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(0, queue.size());
|
||||
final Object[] els = new Object[] { "1", "3", "5", "7", "2", "4", "6" };
|
||||
for (int i = 0; i < els.length; i++) {
|
||||
queue.add(els[i]);
|
||||
assertEquals(i + 1, queue.size());
|
||||
assertEquals(true, queue.contains(new Integer((String) els[i])));
|
||||
assertEquals(false, queue.contains(els[i]));
|
||||
}
|
||||
|
||||
assertEquals(false, queue.remove(els[0]));
|
||||
assertEquals(true, queue.remove(new Integer((String) els[0])));
|
||||
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public void testTransformedQueue_decorateTransform() {
|
||||
final Queue originalQueue = new LinkedList();
|
||||
final Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "6"};
|
||||
for (final Object el : els) {
|
||||
originalQueue.add(el);
|
||||
}
|
||||
final Queue<?> queue = TransformedQueue.transformedQueue(originalQueue,
|
||||
TransformedCollectionTest.STRING_TO_INTEGER_TRANSFORMER);
|
||||
assertEquals(els.length, queue.size());
|
||||
for (final Object el : els) {
|
||||
assertEquals(true, queue.contains(new Integer((String) el)));
|
||||
assertEquals(false, queue.contains(el));
|
||||
}
|
||||
|
||||
assertEquals(false, queue.remove(els[0]));
|
||||
assertEquals(true, queue.remove(new Integer((String) els[0])));
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
return "4";
|
||||
}
|
||||
|
||||
// public void testCreate() throws Exception {
|
||||
// resetEmpty();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedQueue.emptyCollection.version4.obj");
|
||||
// resetFull();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/TransformedQueue.fullCollection.version4.obj");
|
||||
// }
|
||||
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* 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.collections.queue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
import org.apache.commons.collections.collection.AbstractCollectionTest;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractCollectionTest} for exercising the
|
||||
* {@link UnmodifiableQueue} implementation.
|
||||
*
|
||||
* @since 4.0
|
||||
* @version $Id$
|
||||
*/
|
||||
public class UnmodifiableQueueTest<E> extends AbstractCollectionTest<E> {
|
||||
|
||||
public UnmodifiableQueueTest(final String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@Override
|
||||
public Collection<E> makeObject() {
|
||||
return UnmodifiableQueue.unmodifiableQueue(new LinkedList<E>());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeFullCollection() {
|
||||
final Queue<E> queue = new LinkedList<E>();
|
||||
queue.addAll(Arrays.asList(getFullElements()));
|
||||
return UnmodifiableQueue.unmodifiableQueue(queue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeConfirmedCollection() {
|
||||
return new LinkedList<E>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<E> makeConfirmedFullCollection() {
|
||||
final LinkedList<E> list = new LinkedList<E>();
|
||||
list.addAll(Arrays.asList(getFullElements()));
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isAddSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRemoveSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNullSupported() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void testQueueRemove() {
|
||||
resetEmpty();
|
||||
try {
|
||||
getCollection().remove();
|
||||
fail();
|
||||
} catch (final UnsupportedOperationException ex) {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getCompatibilityVersion() {
|
||||
return "4";
|
||||
}
|
||||
|
||||
// public void testCreate() throws Exception {
|
||||
// resetEmpty();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableQueue.emptyCollection.version4.obj");
|
||||
// resetFull();
|
||||
// writeExternalFormToDisk((java.io.Serializable) getCollection(), "src/test/resources/data/test/UnmodifiableQueue.fullCollection.version4.obj");
|
||||
// }
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public Queue<E> getCollection() {
|
||||
return (Queue<E>) super.getCollection();
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue