KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > presumo > jms > message > MessageID


1 /**
2  * This file is part of Presumo.
3  *
4  * Presumo is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * Presumo is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Presumo; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  * Copyright 2001 Dan Greff
20  */

21 package com.presumo.jms.message;
22
23 import java.io.DataInput JavaDoc;
24 import java.io.DataOutput JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  *
30  * @author Dan Greff
31  */

32 public final class MessageID
33 {
34   /** String that should uniquely identifiy the entity which
35    * produces the message.
36    **/

37   private final String JavaDoc name;
38
39   /** Something unique on the host like a process ID
40    * or memory location.
41    **/

42   private final int hostUnique;
43
44   /** Timestamp which when combined with count should be
45    * unique in the JVM
46    **/

47   private final long time;
48
49   /** Counter from the last timestamp **/
50   private final short count;
51
52   /** Cache of hashcode calculation **/
53   private final transient int hash;
54
55   private static long lastTime = System.currentTimeMillis();
56   private static short lastCount = Short.MIN_VALUE;
57   private static final Object JavaDoc lock = "MESSAGE_ID_LOCK";
58   private static final int memoryLocation = new Object JavaDoc().hashCode();
59
60   
61     /////////////////////////////////////////////////////////////////////////
62
// Constructors //
63
/////////////////////////////////////////////////////////////////////////
64

65   public MessageID(String JavaDoc name)
66   {
67     synchronized (lock) {
68       if (lastCount == Short.MAX_VALUE) {
69         boolean done = false;
70         long currentTime = System.currentTimeMillis();
71         long diff = currentTime - lastTime;
72         if (diff <= 0) {
73           ++lastTime;
74         }
75         else {
76           lastTime = currentTime;
77         }
78         lastCount = Short.MIN_VALUE;
79       }
80       this.name = name;
81       this.hostUnique = memoryLocation;
82       this.time = lastTime;
83       this.count = lastCount++;
84     }
85
86     hash = toString().hashCode();
87   }
88
89   private MessageID(String JavaDoc name, int hostUnique, long time, short count)
90   {
91     this.name = name;
92     this.hostUnique = hostUnique;
93     this.time = time;
94     this.count = count;
95     hash = toString().hashCode();
96   }
97
98     /////////////////////////////////////////////////////////////////////////
99
// Public Methods //
100
/////////////////////////////////////////////////////////////////////////
101

102   public int hashCode()
103   {
104     return hash;
105   }
106
107   public boolean equals(Object JavaDoc obj)
108   {
109     if ( (obj != null) && (obj instanceof MessageID) ) {
110       MessageID id = (MessageID) obj;
111
112       return (hostUnique == id.hostUnique &&
113               time == id.time &&
114               count == id.count &&
115               name.equals(id.name) );
116     }
117     return false;
118   }
119
120   public String JavaDoc toString()
121   {
122     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
123     result.append(name);
124     result.append('.');
125     result.append(Integer.toString(hostUnique, 16));
126     result.append('.');
127     result.append(Long.toString(time, 16));
128     result.append('.');
129     result.append(Integer.toString(count, 16));
130     return result.toString();
131   }
132
133   public void marshal(DataOutput JavaDoc out) throws java.io.IOException JavaDoc
134   {
135     out.writeUTF(name);
136     out.writeInt(hostUnique);
137     out.writeLong(time);
138     out.writeShort(count);
139   }
140
141   public static MessageID unmarshal(DataInput JavaDoc in)
142     throws java.io.IOException JavaDoc
143   {
144     String JavaDoc name = in.readUTF();
145     int hostUnique = in.readInt();
146     long time = in.readLong();
147     short count = in.readShort();
148     return new MessageID(name, hostUnique, time, count);
149   }
150
151   public static MessageID unmarshal(String JavaDoc id)
152   {
153     StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(id, ".");
154     String JavaDoc name = tokens.nextToken();
155     int hostUnique = Integer.parseInt(tokens.nextToken(), 16);
156     long time = Long.parseLong(tokens.nextToken(), 16);
157     short count = (short) Integer.parseInt(tokens.nextToken(), 16);
158     return new MessageID(name, hostUnique, time, count);
159   }
160 }
161
Popular Tags