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