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.channel;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  /**
21   * The default upstream {@link ChannelStateEvent} implementation.
22   *
23   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
24   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
25   *
26   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
27   *
28   */
29  public class UpstreamChannelStateEvent implements ChannelStateEvent {
30  
31      private final Channel channel;
32      private final ChannelState state;
33      private final Object value;
34  
35      /**
36       * Creates a new instance.
37       */
38      public UpstreamChannelStateEvent(
39              Channel channel, ChannelState state, Object value) {
40  
41          if (channel == null) {
42              throw new NullPointerException("channel");
43          }
44          if (state == null) {
45              throw new NullPointerException("state");
46          }
47  
48          this.channel = channel;
49          this.state = state;
50          this.value = value;
51      }
52  
53      public Channel getChannel() {
54          return channel;
55      }
56  
57      public ChannelFuture getFuture() {
58          return succeededFuture(getChannel());
59      }
60  
61      public ChannelState getState() {
62          return state;
63      }
64  
65      public Object getValue() {
66          return value;
67      }
68  
69      @Override
70      public String toString() {
71          String channelString = getChannel().toString();
72          StringBuilder buf = new StringBuilder(channelString.length() + 64);
73          buf.append(channelString);
74          switch (getState()) {
75          case OPEN:
76              if (Boolean.TRUE.equals(getValue())) {
77                  buf.append(" OPEN");
78              } else {
79                  buf.append(" CLOSED");
80              }
81              break;
82          case BOUND:
83              if (getValue() != null) {
84                  buf.append(" BOUND: ");
85                  buf.append(getValue());
86              } else {
87                  buf.append(" UNBOUND");
88              }
89              break;
90          case CONNECTED:
91              if (getValue() != null) {
92                  buf.append(" CONNECTED: ");
93                  buf.append(getValue());
94              } else {
95                  buf.append(" DISCONNECTED");
96              }
97              break;
98          case INTEREST_OPS:
99              buf.append(" INTEREST_CHANGED");
100             break;
101         default:
102             buf.append(getState().name());
103             buf.append(": ");
104             buf.append(getValue());
105         }
106         return buf.toString();
107     }
108 }