View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.model;
6   
7   import java.io.*;
8   
9   /***
10   * An <code>Id</code> is used as an identifier for all model objects.  It's purpose
11   * is to be used in other classes to identify the object instead of
12   * using a pointer to it.  This reduces network traffic by sending
13   * <code>Id</code>s instead of the entire object.
14   * <p>
15   * An <code>Id</code> is an opaque object, classes just need to create one
16   * and only need to test for equality.  There is no access to the internal fields.
17   *
18   * @author Barrie Treloar
19   */
20  public class Id implements Serializable {
21      /***
22       * This <code>Id</code> indicates that a message is for everyone.
23       */
24      public static final Id EVERYONE = new Id( -1 );
25      
26      /***
27       * This <oode>Id</code> indicates that a message is from the game engine.
28       */
29      public static final Id SPELLCAST = new Id( -2 );
30      
31      /***
32       * This <code>Id</code> indicates that a message is from no one.
33       * Also used by the client when the network has not yet established a connection.
34       */
35      public static final Id NO_ONE = new Id( -3 );
36      
37      private int id;
38      
39      private static int nextId;
40      
41      private Id(int id_) {
42          id = id_;
43      }
44      
45      public boolean equals( Object o ) {
46          boolean result = false;
47          
48          if ( o instanceof Id ) {
49              Id other = (Id)o;
50              if ( id == other.id ) {
51                  result = true;
52              }
53          }
54          
55          return result;
56      }
57      
58      public String toString() {
59          return Integer.toString( id );
60      }
61      
62      public int hashCode() {
63          return id;
64      }
65      
66      public static Id createId() {
67          return new Id( nextId++ );
68      }
69  }
70