blob: b5c60b7f94e6353aaee421efbdd9aeacc36b0226 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.control;
import java.net.InetAddress;
/**
* EOTemporaryGlobalID is a network-wide unique key. This is used by
* EOEditingContext to construct temporary ids when new objects are created.
* <br>
* <br>
*
* The specified format of the key is a byte array: < Sequence [2], ProcessID
* [2], Time [4], IP Addr [4] >, but because java does not allow access to
* the process id, the timestamp of when this class is first loaded is used to
* simulate a process id.
*
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 893 $
*/
public class EOTemporaryGlobalID extends EOGlobalID {
private static final long serialVersionUID = 7652533198394290800L;
/**
* Holds the length in bytes of the key that is generated.
*/
public static final int UniqueBinaryKeyLength = 12;
private static int sequence;
private static byte[] processid; // 2 bytes
private static byte[] ipaddr; // 4 bytes
static // static initializer
{
// init sequence
sequence = 0;
// init processid
processid = new byte[2];
long time = System.currentTimeMillis();
processid[1] = (byte) time;
time = time >> 8;
processid[0] = (byte) time;
// init ipaddr
ipaddr = new byte[4];
try {
ipaddr = InetAddress.getLocalHost().getAddress();
} catch (Exception exc) {
// could not obtain ip address - use pid twice
ipaddr[0] = processid[0];
ipaddr[1] = processid[1];
ipaddr[2] = processid[0];
ipaddr[3] = processid[1];
}
}
private byte[] key;
private int hashCode;
/**
* Generates a new id with a unique key.
*/
public EOTemporaryGlobalID() {
key = new byte[UniqueBinaryKeyLength];
// init sequence (important byte first)
key[0] = (byte) (sequence);
key[1] = (byte) (sequence >> 8);
sequence++;
// populate pid (important byte first)
key[2] = processid[1];
key[3] = processid[0];
// init time (important byte first)
long time = System.currentTimeMillis();
key[4] = (byte) time;
time = time >> 8;
key[5] = (byte) time;
time = time >> 8;
key[6] = (byte) time;
time = time >> 8;
key[7] = (byte) time;
// populate ipaddr
key[8] = ipaddr[0];
key[9] = ipaddr[1];
key[10] = ipaddr[2];
key[11] = ipaddr[3];
// use string's hash code
hashCode = new String(key).hashCode();
}
/**
* Private constructor for cloning.
*/
private EOTemporaryGlobalID(byte[] aKey) {
// key = aKey; // this might be faster
// make copy of key - might be safer
key = new byte[UniqueBinaryKeyLength];
for (int i = 0; i < UniqueBinaryKeyLength; i++) {
key[i] = aKey[i];
}
// use string's hash code
hashCode = new String(aKey).hashCode();
}
/**
* Returns true.
*/
public boolean isTemporary() {
return true;
}
/**
* Returns whether the keys are equal.
*/
public boolean equals(Object anObject) {
if (!(anObject instanceof EOTemporaryGlobalID))
return false;
byte[] otherKey = ((EOTemporaryGlobalID) anObject).key;
for (int i = 0; i < UniqueBinaryKeyLength; i++) {
if (key[i] != otherKey[i])
return false;
}
return true;
}
/**
* Returns a copy of this object.
*/
public Object clone() {
// faster than super.clone()
return new EOTemporaryGlobalID(key);
}
public int hashCode() {
return hashCode;
}
/**
* Returns a string representation of this key. This is a 24-character string
* with each pair of characters holding a hexadecimal value that is 128 more
* than the value of the corresponding byte (to account for two's complement).
*/
public String toString() {
String hex;
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < key.length; i++) {
// get string: adjust for two's complement
hex = Integer.toHexString(key[i] + 128);
// pad with zero so we take two characters
if (hex.length() == 1)
hex = "0" + hex;
// append hex code
buffer.append(hex);
}
return buffer.toString();
}
}
/*
* $Log$ Revision 1.1 2006/02/16 13:19:57 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.4 2001/04/29 22:02:45 mpowers Work on id transposing between
* editing contexts.
*
* Revision 1.3 2001/02/15 21:13:30 mpowers First draft implementation is
* complete. Now on to debugging.
*
* Revision 1.2 2001/02/14 23:03:02 mpowers A near-complete first draft of
* EOEditingContext.
*
* Revision 1.1 2001/02/13 23:24:29 mpowers Implementing more of editing
* context.
*
*
*/
|