Resolved issue: NETTY-245 More convenience methods in CodecEmbedder

* Added CodecEmbedder.size()
* Added CodecEmbedder.pollAll()
This commit is contained in:
Trustin Lee 2009-11-02 12:01:19 +00:00
parent bc6b84e58b
commit 6cb1a504fb
4 changed files with 79 additions and 10 deletions

View File

@ -17,6 +17,7 @@ package org.jboss.netty.handler.codec.embedder;
import static org.jboss.netty.channel.Channels.*;
import java.lang.reflect.Array;
import java.util.LinkedList;
import java.util.Queue;
@ -41,7 +42,7 @@ import org.jboss.netty.channel.MessageEvent;
* @author Trustin Lee (trustin@gmail.com)
* @version $Rev$, $Date$
*/
abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> {
abstract class AbstractCodecEmbedder<E> implements CodecEmbedder<E> {
private final Channel channel;
private final ChannelPipeline pipeline;
@ -124,13 +125,50 @@ abstract class AbstractCodecEmbedder<T> implements CodecEmbedder<T> {
}
@SuppressWarnings("unchecked")
public final T poll() {
return (T) productQueue.poll();
public final E poll() {
return (E) productQueue.poll();
}
@SuppressWarnings("unchecked")
public final T peek() {
return (T) productQueue.peek();
public final E peek() {
return (E) productQueue.peek();
}
public final Object[] pollAll() {
return pollAll(new Object[size()]);
}
@SuppressWarnings("unchecked")
public final <T> T[] pollAll(T[] a) {
if (a == null) {
throw new NullPointerException("a");
}
final int size = size();
// Create a new array if the specified one is too small.
if (a.length < size) {
a = (T[]) Array.newInstance(a.getClass().getComponentType(), size);
}
for (int i = 0;; i ++) {
T product = (T) poll();
if (product == null) {
break;
}
a[i] = product;
}
// Put the terminator if necessary.
if (a.length > size) {
a[size] = null;
}
return a;
}
public final int size() {
return productQueue.size();
}
@ChannelPipelineCoverage("all")

View File

@ -15,6 +15,8 @@
*/
package org.jboss.netty.handler.codec.embedder;
import java.util.Collection;
/**
* A helper that wraps an encoder or a decoder (codec) so that they can be used
* without doing actual I/O in unit tests or higher level codecs. Please refer
@ -24,7 +26,7 @@ package org.jboss.netty.handler.codec.embedder;
* @author Trustin Lee (trustin@gmail.com)
* @version $Rev$, $Date$
*/
public interface CodecEmbedder<T> {
public interface CodecEmbedder<E> {
/**
* Offers an input object to the pipeline of this embedder.
*
@ -50,7 +52,7 @@ public interface CodecEmbedder<T> {
* {@code null} if and only if there is no output object left in the
* product queue.
*/
T poll();
E poll();
/**
* Reads an encoded or decoded output from the head of the product queue.
@ -61,5 +63,34 @@ public interface CodecEmbedder<T> {
* {@code null} if and only if there is no output object left in the
* product queue.
*/
T peek();
E peek();
/**
* Consumes all encoded or decoded output from the product queue. The
* output object is generated by the offered input objects. The behavior
* of this method is identical with {@link Collection#toArray()} except that
* the product queue is cleared.
*
* @return an array of all encoded or decoded objects.
* An empty array is returned if and only if there is no output
* object left in the product queue.
*/
Object[] pollAll();
/**
* Consumes all encoded or decoded output from the product queue. The
* output object is generated by the offered input objects. The behavior
* of this method is identical with {@link Collection#toArray(Object[])}
* except that the product queue is cleared.
*
* @return an array of all encoded or decoded objects.
* An empty array is returned if and only if there is no output
* object left in the product queue.
*/
<T> T[] pollAll(T[] a);
/**
* Returns the number of encoded or decoded output in the product queue.
*/
int size();
}

View File

@ -49,7 +49,7 @@ import org.jboss.netty.handler.codec.string.StringDecoder;
* @apiviz.landmark
* @see EncoderEmbedder
*/
public class DecoderEmbedder<T> extends AbstractCodecEmbedder<T> {
public class DecoderEmbedder<E> extends AbstractCodecEmbedder<E> {
/**
* Creates a new embedder whose pipeline is composed of the specified

View File

@ -49,7 +49,7 @@ import org.jboss.netty.handler.codec.string.StringEncoder;
* @apiviz.landmark
* @see DecoderEmbedder
*/
public class EncoderEmbedder<T> extends AbstractCodecEmbedder<T> {
public class EncoderEmbedder<E> extends AbstractCodecEmbedder<E> {
/**
* Creates a new embedder whose pipeline is composed of the specified