查看本类的 API文档回源码主页即时通讯网 - 即时通讯开发者社区!
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the 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  /**
19   * The default downstream {@link ChannelStateEvent} implementation.
20   */
21  public class DownstreamChannelStateEvent implements ChannelStateEvent {
22  
23      private final Channel channel;
24      private final ChannelFuture future;
25      private final ChannelState state;
26      private final Object value;
27  
28      /**
29       * Creates a new instance.
30       */
31      public DownstreamChannelStateEvent(
32              Channel channel, ChannelFuture future,
33              ChannelState state, Object value) {
34  
35          if (channel == null) {
36              throw new NullPointerException("channel");
37          }
38          if (future == null) {
39              throw new NullPointerException("future");
40          }
41          if (state == null) {
42              throw new NullPointerException("state");
43          }
44          this.channel = channel;
45          this.future = future;
46          this.state = state;
47          this.value = value;
48      }
49  
50      public Channel getChannel() {
51          return channel;
52      }
53  
54      public ChannelFuture getFuture() {
55          return future;
56      }
57  
58      public ChannelState getState() {
59          return state;
60      }
61  
62      public Object getValue() {
63          return value;
64      }
65  
66      @Override
67      public String toString() {
68          String channelString = getChannel().toString();
69          StringBuilder buf = new StringBuilder(channelString.length() + 64);
70          buf.append(channelString);
71          switch (getState()) {
72          case OPEN:
73              if (Boolean.TRUE.equals(getValue())) {
74                  buf.append(" OPEN");
75              } else {
76                  buf.append(" CLOSE");
77              }
78              break;
79          case BOUND:
80              if (getValue() != null) {
81                  buf.append(" BIND: ");
82                  buf.append(getValue());
83              } else {
84                  buf.append(" UNBIND");
85              }
86              break;
87          case CONNECTED:
88              if (getValue() != null) {
89                  buf.append(" CONNECT: ");
90                  buf.append(getValue());
91              } else {
92                  buf.append(" DISCONNECT");
93              }
94              break;
95          case INTEREST_OPS:
96              buf.append(" CHANGE_INTEREST: ");
97              buf.append(getValue());
98              break;
99          default:
100             buf.append(' ');
101             buf.append(getState().name());
102             buf.append(": ");
103             buf.append(getValue());
104         }
105         return buf.toString();
106     }
107 }