View Javadoc

1   /**
2    *
3    */
4   package org.sourceforge.jemmrpc.server;
5   
6   import java.util.concurrent.atomic.AtomicInteger;
7   
8   /**
9    * A client identifier.
10   *
11   * @author Rory Graves
12   */
13  public class ClientId
14  {
15      /** Internal counter for client ids */
16      protected static final AtomicInteger nextClientId = new AtomicInteger(1);
17  
18      /** The internal id of this client. */
19      protected final String clientIdString;
20  
21      /**
22       * Create a unique ClientId.
23       */
24      public ClientId()
25      {
26          this.clientIdString = "CL:" + nextClientId.getAndIncrement();
27      }
28  
29      /**
30       * Create a stored client id.
31       *
32       * @param internalId The internal id of the client.
33       */
34      public ClientId(String internalId)
35      {
36          if(internalId == null)
37              throw new IllegalArgumentException("internal id cannot be null");
38          this.clientIdString = internalId;
39      }
40  
41      /**
42       * Returns the internal representation if this id for storage.
43       *
44       * @return The internal representation of this id.
45       */
46      public String getInternalId()
47      {
48          return clientIdString;
49      }
50  
51      /*
52       * (non-Javadoc)
53       *
54       * @see java.lang.Object#equals(java.lang.Object)
55       */
56      @Override
57      public boolean equals(Object obj)
58      {
59          if (this == obj)
60              return true;
61          if (obj == null)
62              return false;
63          if (getClass() != obj.getClass())
64              return false;
65  
66          // client ID String can never by null so shortcut the equals code.
67          final ClientId other = (ClientId) obj;
68          return clientIdString.equals(other.clientIdString);
69      }
70  
71      /*
72       * (non-Javadoc)
73       *
74       * @see java.lang.Object#hashCode()
75       */
76      @Override
77      public int hashCode()
78      {
79          return clientIdString.hashCode();
80      }
81  
82      @Override
83      public String toString()
84      {
85          return "ClientId(" + clientIdString + ")";
86      }
87  }