查看本类的 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    *   https://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 io.netty.handler.timeout;
17  
18  import io.netty.channel.Channel;
19  import io.netty.util.internal.ObjectUtil;
20  import io.netty.util.internal.StringUtil;
21  
22  /**
23   * A user event triggered by {@link IdleStateHandler} when a {@link Channel} is idle.
24   */
25  public class IdleStateEvent {
26      public static final IdleStateEvent FIRST_READER_IDLE_STATE_EVENT =
27              new DefaultIdleStateEvent(IdleState.READER_IDLE, true);
28      public static final IdleStateEvent READER_IDLE_STATE_EVENT =
29              new DefaultIdleStateEvent(IdleState.READER_IDLE, false);
30      public static final IdleStateEvent FIRST_WRITER_IDLE_STATE_EVENT =
31              new DefaultIdleStateEvent(IdleState.WRITER_IDLE, true);
32      public static final IdleStateEvent WRITER_IDLE_STATE_EVENT =
33              new DefaultIdleStateEvent(IdleState.WRITER_IDLE, false);
34      public static final IdleStateEvent FIRST_ALL_IDLE_STATE_EVENT =
35              new DefaultIdleStateEvent(IdleState.ALL_IDLE, true);
36      public static final IdleStateEvent ALL_IDLE_STATE_EVENT =
37              new DefaultIdleStateEvent(IdleState.ALL_IDLE, false);
38  
39      private final IdleState state;
40      private final boolean first;
41  
42      /**
43       * Constructor for sub-classes.
44       *
45       * @param state the {@link IdleStateEvent} which triggered the event.
46       * @param first {@code true} if its the first idle event for the {@link IdleStateEvent}.
47       */
48      protected IdleStateEvent(IdleState state, boolean first) {
49          this.state = ObjectUtil.checkNotNull(state, "state");
50          this.first = first;
51      }
52  
53      /**
54       * Returns the idle state.
55       */
56      public IdleState state() {
57          return state;
58      }
59  
60      /**
61       * Returns {@code true} if this was the first event for the {@link IdleState}
62       */
63      public boolean isFirst() {
64          return first;
65      }
66  
67      @Override
68      public String toString() {
69          return StringUtil.simpleClassName(this) + '(' + state + (first ? ", first" : "") + ')';
70      }
71  
72      private static final class DefaultIdleStateEvent extends IdleStateEvent {
73          private final String representation;
74  
75          DefaultIdleStateEvent(IdleState state, boolean first) {
76              super(state, first);
77              this.representation = "IdleStateEvent(" + state + (first ? ", first" : "") + ')';
78          }
79  
80          @Override
81          public String toString() {
82              return representation;
83          }
84      }
85  }