View Javadoc

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