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