View Javadoc

1   /*
2    * @(#) $Id: TennisBall.java 332218 2005-11-10 03:52:42Z trustin $
3    */
4   package org.apache.mina.examples.tennis;
5   
6   /***
7    * A tennis ball which has TTL value and state whose value is one of 'PING' and
8    * 'PONG'.
9    * 
10   * @author The Apache Directory Project (dev@directory.apache.org)
11   * @version $Rev: 332218 $, $Date: 2005-11-10 12:52:42 +0900 $
12   */
13  public class TennisBall
14  {
15      private final boolean ping;
16  
17      private final int ttl;
18  
19      /***
20       * Creates a new ball with the specified TTL (Time To Live) value.
21       */
22      public TennisBall( int ttl )
23      {
24          this( ttl, true );
25      }
26  
27      /***
28       * Creates a new ball with the specified TTL value and PING/PONG state.
29       */
30      private TennisBall( int ttl, boolean ping )
31      {
32          this.ttl = ttl;
33          this.ping = ping;
34      }
35  
36      /***
37       * Returns the TTL value of this ball.
38       */
39      public int getTTL()
40      {
41          return ttl;
42      }
43  
44      /***
45       * Returns the ball after {@link TennisPlayer}'s stroke.
46       * The returned ball has decreased TTL value and switched PING/PONG state.
47       */
48      public TennisBall stroke()
49      {
50          return new TennisBall( ttl - 1, !ping );
51      }
52  
53      /***
54       * Returns string representation of this message (<code>[PING|PONG]
55       * (TTL)</code>).
56       */
57      public String toString()
58      {
59          if( ping )
60          {
61              return "PING (" + ttl + ")";
62          }
63          else
64          {
65              return "PONG (" + ttl + ")";
66          }
67      }
68  }