KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > transport > ConnectionID


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.net.protocol.transport;
5
6 import com.tc.net.protocol.tcm.ChannelID;
7
8 public class ConnectionID {
9
10   private final long channelID;
11   private final String JavaDoc serverID;
12
13   public static final ConnectionID NULL_ID = new ConnectionID(ChannelID.NULL_ID.toLong(),
14                                                               "ffffffffffffffffffffffffffffffff");
15
16   private static final String JavaDoc SEP = ".";
17
18   public static ConnectionID parse(String JavaDoc compositeID) throws InvalidConnectionIDException {
19     if (compositeID == null) { throw new InvalidConnectionIDException(compositeID, "null connectionID"); }
20
21     String JavaDoc[] parts = compositeID.split("\\" + SEP);
22     if (parts.length != 2) {
23       // make formatter sane
24
throw new InvalidConnectionIDException(compositeID, "wrong number of components: " + parts.length);
25     }
26
27     String JavaDoc channelID = parts[0];
28     final long channel;
29     try {
30       channel = Long.parseLong(channelID);
31     } catch (Exception JavaDoc e) {
32       throw new InvalidConnectionIDException(compositeID, "parse exception for channelID " + channelID, e);
33     }
34
35     String JavaDoc server = parts[1];
36     if (server.length() != 32) { throw new InvalidConnectionIDException(compositeID, "invalid serverID length: "
37                                                                                      + server.length()); }
38
39     if (!server.matches("[A-Fa-f0-9]+")) { throw new InvalidConnectionIDException(compositeID,
40                                                                                   "invalid chars in serverID: "
41                                                                                       + server); }
42
43     return new ConnectionID(channel, server);
44   }
45
46   public ConnectionID(long channelID, String JavaDoc serverID) {
47     this.channelID = channelID;
48     this.serverID = serverID;
49   }
50
51   public String JavaDoc toString() {
52     return "ConnectionID(" + getID() + ")";
53   }
54
55   public boolean isNull() {
56     return NULL_ID.equals(this);
57   }
58
59   public String JavaDoc getServerID() {
60     return this.serverID;
61   }
62
63   public int hashCode() {
64     int hc = 17;
65     hc = 37 * hc + (int) (this.channelID ^ (this.channelID >>> 32));
66     if (this.serverID != null) {
67       hc = 37 * hc + serverID.hashCode();
68     }
69
70     return hc;
71   }
72
73   public boolean equals(Object JavaDoc obj) {
74     if (obj instanceof ConnectionID) {
75       ConnectionID other = (ConnectionID) obj;
76       return (this.channelID == other.channelID) && (this.serverID.equals(other.serverID));
77     }
78     return false;
79   }
80
81   public long getChannelID() {
82     return channelID;
83   }
84
85   public String JavaDoc getID() {
86     return channelID + SEP + serverID ;
87   }
88
89 }
90
Popular Tags