20 |
|
* which differs from both the primitive double {@code ==} operator |
21 |
|
* and from {@link Double#equals}, as if implemented by: |
22 |
|
* <pre> {@code |
23 |
< |
* boolean bitEquals(double x, double y) { |
23 |
> |
* static boolean bitEquals(double x, double y) { |
24 |
|
* long xBits = Double.doubleToRawLongBits(x); |
25 |
|
* long yBits = Double.doubleToRawLongBits(y); |
26 |
|
* return xBits == yBits; |
32 |
|
public class AtomicDoubleArray implements java.io.Serializable { |
33 |
|
private static final long serialVersionUID = -2308431214976778248L; |
34 |
|
|
35 |
< |
private final long[] array; |
35 |
> |
private final transient long[] array; |
36 |
|
|
37 |
|
private long checkedByteOffset(int i) { |
38 |
|
if (i < 0 || i >= array.length) |
162 |
|
* if the current value is <a href="#bitEquals">bitwise equal</a> |
163 |
|
* to the expected value. |
164 |
|
* |
165 |
< |
* <p>May <a href="package-summary.html#Spurious">fail spuriously</a> |
165 |
> |
* <p>May <a |
166 |
> |
* href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> |
167 |
> |
* fail spuriously</a> |
168 |
|
* and does not provide ordering guarantees, so is only rarely an |
169 |
|
* appropriate alternative to {@code compareAndSet}. |
170 |
|
* |
224 |
|
if (iMax == -1) |
225 |
|
return "[]"; |
226 |
|
|
227 |
< |
StringBuilder b = new StringBuilder(); |
227 |
> |
// Double.toString(Math.PI).length() == 17 |
228 |
> |
StringBuilder b = new StringBuilder((17 + 2) * (iMax + 1)); |
229 |
|
b.append('['); |
230 |
< |
for (int i = 0; ; i++) { |
230 |
> |
for (int i = 0;; i++) { |
231 |
|
b.append(longBitsToDouble(getRaw(byteOffset(i)))); |
232 |
|
if (i == iMax) |
233 |
|
return b.append(']').toString(); |
235 |
|
} |
236 |
|
} |
237 |
|
|
238 |
+ |
/** |
239 |
+ |
* Saves the state to a stream (that is, serializes it). |
240 |
+ |
* |
241 |
+ |
* @serialData The length of the array is emitted (int), followed by all |
242 |
+ |
* of its elements (each a {@code double}) in the proper order. |
243 |
+ |
*/ |
244 |
+ |
private void writeObject(java.io.ObjectOutputStream s) |
245 |
+ |
throws java.io.IOException { |
246 |
+ |
s.defaultWriteObject(); |
247 |
+ |
|
248 |
+ |
// Write out array length |
249 |
+ |
int length = length(); |
250 |
+ |
s.writeInt(length); |
251 |
+ |
|
252 |
+ |
// Write out all elements in the proper order. |
253 |
+ |
for (int i = 0; i < length; i++) |
254 |
+ |
s.writeDouble(get(i)); |
255 |
+ |
} |
256 |
+ |
|
257 |
+ |
/** |
258 |
+ |
* Reconstitutes the instance from a stream (that is, deserializes it). |
259 |
+ |
*/ |
260 |
+ |
private void readObject(java.io.ObjectInputStream s) |
261 |
+ |
throws java.io.IOException, ClassNotFoundException { |
262 |
+ |
s.defaultReadObject(); |
263 |
+ |
|
264 |
+ |
// Read in array length and allocate array |
265 |
+ |
int length = s.readInt(); |
266 |
+ |
unsafe.putObjectVolatile(this, arrayOffset, new long[length]); |
267 |
+ |
|
268 |
+ |
// Read in all elements in the proper order. |
269 |
+ |
for (int i = 0; i < length; i++) |
270 |
+ |
set(i, s.readDouble()); |
271 |
+ |
} |
272 |
+ |
|
273 |
|
// Unsafe mechanics |
274 |
|
private static final sun.misc.Unsafe unsafe = getUnsafe(); |
275 |
+ |
private static final long arrayOffset; |
276 |
|
private static final int base = unsafe.arrayBaseOffset(long[].class); |
277 |
|
private static final int shift; |
278 |
|
|
279 |
|
static { |
280 |
< |
int scale = unsafe.arrayIndexScale(long[].class); |
281 |
< |
if ((scale & (scale - 1)) != 0) |
282 |
< |
throw new Error("data type scale not a power of two"); |
283 |
< |
shift = 31 - Integer.numberOfLeadingZeros(scale); |
280 |
> |
try { |
281 |
> |
Class<?> k = AtomicDoubleArray.class; |
282 |
> |
arrayOffset = unsafe.objectFieldOffset |
283 |
> |
(k.getDeclaredField("array")); |
284 |
> |
int scale = unsafe.arrayIndexScale(long[].class); |
285 |
> |
if ((scale & (scale - 1)) != 0) |
286 |
> |
throw new Error("data type scale not a power of two"); |
287 |
> |
shift = 31 - Integer.numberOfLeadingZeros(scale); |
288 |
> |
} catch (Exception e) { |
289 |
> |
throw new Error(e); |
290 |
> |
} |
291 |
|
} |
292 |
|
|
293 |
|
/** |