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.handler.timeout;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  import java.text.DateFormat;
21  import java.util.Date;
22  import java.util.Locale;
23  
24  import org.jboss.netty.channel.Channel;
25  import org.jboss.netty.channel.ChannelFuture;
26  
27  /**
28   * The default {@link IdleStateEvent} implementation.
29   */
30  public class DefaultIdleStateEvent implements IdleStateEvent {
31  
32      private final Channel channel;
33      private final IdleState state;
34      private final long lastActivityTimeMillis;
35  
36      /**
37       * Creates a new instance.
38       */
39      public DefaultIdleStateEvent(
40              Channel channel, IdleState state, long lastActivityTimeMillis) {
41          if (channel == null) {
42              throw new NullPointerException("channel");
43          }
44          if (state == null) {
45              throw new NullPointerException("state");
46          }
47          this.channel = channel;
48          this.state = state;
49          this.lastActivityTimeMillis = lastActivityTimeMillis;
50      }
51  
52      public Channel getChannel() {
53          return channel;
54      }
55  
56      public ChannelFuture getFuture() {
57          return succeededFuture(getChannel());
58      }
59  
60      public IdleState getState() {
61          return state;
62      }
63  
64      public long getLastActivityTimeMillis() {
65          return lastActivityTimeMillis;
66      }
67  
68      @Override
69      public String toString() {
70          return getChannel().toString() + ' ' + getState() + " since " +
71                 DateFormat.getDateTimeInstance(
72                         DateFormat.SHORT, DateFormat.SHORT, Locale.US).format(
73                                 new Date(getLastActivityTimeMillis()));
74      }
75  }