View Javadoc

1   /*
2    * Copyright 2009 Red Hat, Inc.
3    *
4    * Red Hat licenses this file to you under the Apache License, version 2.0
5    * (the "License"); you may not use this file except in compliance with the
6    * License.  You may obtain a copy of the License at:
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.handler.codec.embedder;
17  
18  import java.util.Collection;
19  
20  import org.jboss.netty.channel.ChannelPipeline;
21  
22  /**
23   * A helper that wraps an encoder or a decoder (codec) so that they can be used
24   * without doing actual I/O in unit tests or higher level codecs.  Please refer
25   * to {@link EncoderEmbedder} and {@link DecoderEmbedder} for more information.
26   *
27   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
28   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
29   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
30   */
31  public interface CodecEmbedder<E> {
32      /**
33       * Offers an input object to the pipeline of this embedder.
34       *
35       * @return {@code true} if and only if there is something to read in the
36       *         product queue (see {@link #poll()} and {@link #peek()})
37       */
38      boolean offer(Object input);
39  
40      /**
41       * Signals the pipeline that the encoding or decoding has been finished and
42       * no more data will be offered.
43       *
44       * @return {@code true} if and only if there is something to read in the
45       *         product queue (see {@link #poll()} and {@link #peek()})
46       */
47      boolean finish();
48  
49      /**
50       * Consumes an encoded or decoded output from the product queue. The output
51       * object is generated by the offered input objects.
52       *
53       * @return an encoded or decoded object.
54       *         {@code null} if and only if there is no output object left in the
55       *         product queue.
56       */
57      E poll();
58  
59      /**
60       * Reads an encoded or decoded output from the head of the product queue.
61       * The difference from {@link #poll()} is that it does not remove the
62       * retrieved object from the product queue.
63       *
64       * @return an encoded or decoded object.
65       *         {@code null} if and only if there is no output object left in the
66       *         product queue.
67       */
68      E peek();
69  
70      /**
71       * Consumes all encoded or decoded output from the product queue.  The
72       * output object is generated by the offered input objects.  The behavior
73       * of this method is identical with {@link Collection#toArray()} except that
74       * the product queue is cleared.
75       *
76       * @return an array of all encoded or decoded objects.
77       *         An empty array is returned if and only if there is no output
78       *         object left in the product queue.
79       */
80      Object[] pollAll();
81  
82      /**
83       * Consumes all encoded or decoded output from the product queue.  The
84       * output object is generated by the offered input objects.  The behavior
85       * of this method is identical with {@link Collection#toArray(Object[])}
86       * except that the product queue is cleared.
87       *
88       * @return an array of all encoded or decoded objects.
89       *         An empty array is returned if and only if there is no output
90       *         object left in the product queue.
91       */
92      <T> T[] pollAll(T[] a);
93  
94      /**
95       * Returns the number of encoded or decoded output in the product queue.
96       */
97      int size();
98  
99      /**
100      * Returns the {@link ChannelPipeline} that handles the input.
101      */
102     ChannelPipeline getPipeline();
103 }