1 21 package com.presumo.jms.message; 22 23 import java.io.DataInput ; 24 import java.io.DataOutput ; 25 import java.io.IOException ; 26 import java.util.StringTokenizer ; 27 28 32 public final class MessageID 33 { 34 37 private final String name; 38 39 42 private final int hostUnique; 43 44 47 private final long time; 48 49 50 private final short count; 51 52 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 lock = "MESSAGE_ID_LOCK"; 58 private static final int memoryLocation = new Object ().hashCode(); 59 60 61 65 public MessageID(String 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 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 102 public int hashCode() 103 { 104 return hash; 105 } 106 107 public boolean equals(Object 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 toString() 121 { 122 StringBuffer result = new StringBuffer (); 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 out) throws java.io.IOException 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 in) 142 throws java.io.IOException 143 { 144 String 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 id) 152 { 153 StringTokenizer tokens = new StringTokenizer (id, "."); 154 String 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 |