001/* 002 * Written by Doug Lea with assistance from members of JCP JSR-166 003 * Expert Group and released to the public domain, as explained at 004 * http://creativecommons.org/publicdomain/zero/1.0/ 005 */ 006 007/* 008 * Source: 009 * http://gee.cs.oswego.edu/cgi-bin/viewcvs.cgi/jsr166/src/jsr166e/extra/AtomicDoubleArray.java?revision=1.5 010 * (Modified to adapt to guava coding conventions and 011 * to use AtomicLongArray instead of sun.misc.Unsafe) 012 */ 013 014package com.google.common.util.concurrent; 015 016import static java.lang.Double.doubleToRawLongBits; 017import static java.lang.Double.longBitsToDouble; 018 019import com.google.common.annotations.GwtIncompatible; 020import com.google.errorprone.annotations.CanIgnoreReturnValue; 021import java.util.concurrent.atomic.AtomicLongArray; 022 023/** 024 * A {@code double} array in which elements may be updated atomically. See the {@link 025 * java.util.concurrent.atomic} package specification for description of the properties of atomic 026 * variables. 027 * 028 * <p><a name="bitEquals"></a>This class compares primitive {@code double} values in methods such as 029 * {@link #compareAndSet} by comparing their bitwise representation using {@link 030 * Double#doubleToRawLongBits}, which differs from both the primitive double {@code ==} operator and 031 * from {@link Double#equals}, as if implemented by: 032 * 033 * <pre>{@code 034 * static boolean bitEquals(double x, double y) { 035 * long xBits = Double.doubleToRawLongBits(x); 036 * long yBits = Double.doubleToRawLongBits(y); 037 * return xBits == yBits; 038 * } 039 * }</pre> 040 * 041 * @author Doug Lea 042 * @author Martin Buchholz 043 * @since 11.0 044 */ 045@GwtIncompatible 046public class AtomicDoubleArray implements java.io.Serializable { 047 private static final long serialVersionUID = 0L; 048 049 // Making this non-final is the lesser evil according to Effective 050 // Java 2nd Edition Item 76: Write readObject methods defensively. 051 private transient AtomicLongArray longs; 052 053 /** 054 * Creates a new {@code AtomicDoubleArray} of the given length, 055 * with all elements initially zero. 056 * 057 * @param length the length of the array 058 */ 059 public AtomicDoubleArray(int length) { 060 this.longs = new AtomicLongArray(length); 061 } 062 063 /** 064 * Creates a new {@code AtomicDoubleArray} with the same length 065 * as, and all elements copied from, the given array. 066 * 067 * @param array the array to copy elements from 068 * @throws NullPointerException if array is null 069 */ 070 public AtomicDoubleArray(double[] array) { 071 final int len = array.length; 072 long[] longArray = new long[len]; 073 for (int i = 0; i < len; i++) { 074 longArray[i] = doubleToRawLongBits(array[i]); 075 } 076 this.longs = new AtomicLongArray(longArray); 077 } 078 079 /** 080 * Returns the length of the array. 081 * 082 * @return the length of the array 083 */ 084 public final int length() { 085 return longs.length(); 086 } 087 088 /** 089 * Gets the current value at position {@code i}. 090 * 091 * @param i the index 092 * @return the current value 093 */ 094 public final double get(int i) { 095 return longBitsToDouble(longs.get(i)); 096 } 097 098 /** 099 * Sets the element at position {@code i} to the given value. 100 * 101 * @param i the index 102 * @param newValue the new value 103 */ 104 public final void set(int i, double newValue) { 105 long next = doubleToRawLongBits(newValue); 106 longs.set(i, next); 107 } 108 109 /** 110 * Eventually sets the element at position {@code i} to the given value. 111 * 112 * @param i the index 113 * @param newValue the new value 114 */ 115 public final void lazySet(int i, double newValue) { 116 long next = doubleToRawLongBits(newValue); 117 longs.lazySet(i, next); 118 } 119 120 /** 121 * Atomically sets the element at position {@code i} to the given value 122 * and returns the old value. 123 * 124 * @param i the index 125 * @param newValue the new value 126 * @return the previous value 127 */ 128 public final double getAndSet(int i, double newValue) { 129 long next = doubleToRawLongBits(newValue); 130 return longBitsToDouble(longs.getAndSet(i, next)); 131 } 132 133 /** 134 * Atomically sets the element at position {@code i} to the given 135 * updated value 136 * if the current value is <a href="#bitEquals">bitwise equal</a> 137 * to the expected value. 138 * 139 * @param i the index 140 * @param expect the expected value 141 * @param update the new value 142 * @return true if successful. False return indicates that 143 * the actual value was not equal to the expected value. 144 */ 145 public final boolean compareAndSet(int i, double expect, double update) { 146 return longs.compareAndSet(i, 147 doubleToRawLongBits(expect), 148 doubleToRawLongBits(update)); 149 } 150 151 /** 152 * Atomically sets the element at position {@code i} to the given 153 * updated value 154 * if the current value is <a href="#bitEquals">bitwise equal</a> 155 * to the expected value. 156 * 157 * <p>May <a 158 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 159 * fail spuriously</a> 160 * and does not provide ordering guarantees, so is only rarely an 161 * appropriate alternative to {@code compareAndSet}. 162 * 163 * @param i the index 164 * @param expect the expected value 165 * @param update the new value 166 * @return true if successful 167 */ 168 public final boolean weakCompareAndSet(int i, double expect, double update) { 169 return longs.weakCompareAndSet(i, 170 doubleToRawLongBits(expect), 171 doubleToRawLongBits(update)); 172 } 173 174 /** 175 * Atomically adds the given value to the element at index {@code i}. 176 * 177 * @param i the index 178 * @param delta the value to add 179 * @return the previous value 180 */ 181 @CanIgnoreReturnValue 182 public final double getAndAdd(int i, double delta) { 183 while (true) { 184 long current = longs.get(i); 185 double currentVal = longBitsToDouble(current); 186 double nextVal = currentVal + delta; 187 long next = doubleToRawLongBits(nextVal); 188 if (longs.compareAndSet(i, current, next)) { 189 return currentVal; 190 } 191 } 192 } 193 194 /** 195 * Atomically adds the given value to the element at index {@code i}. 196 * 197 * @param i the index 198 * @param delta the value to add 199 * @return the updated value 200 */ 201 @CanIgnoreReturnValue 202 public double addAndGet(int i, double delta) { 203 while (true) { 204 long current = longs.get(i); 205 double currentVal = longBitsToDouble(current); 206 double nextVal = currentVal + delta; 207 long next = doubleToRawLongBits(nextVal); 208 if (longs.compareAndSet(i, current, next)) { 209 return nextVal; 210 } 211 } 212 } 213 214 /** 215 * Returns the String representation of the current values of array. 216 * @return the String representation of the current values of array 217 */ 218 public String toString() { 219 int iMax = length() - 1; 220 if (iMax == -1) { 221 return "[]"; 222 } 223 224 // Double.toString(Math.PI).length() == 17 225 StringBuilder b = new StringBuilder((17 + 2) * (iMax + 1)); 226 b.append('['); 227 for (int i = 0;; i++) { 228 b.append(longBitsToDouble(longs.get(i))); 229 if (i == iMax) { 230 return b.append(']').toString(); 231 } 232 b.append(',').append(' '); 233 } 234 } 235 236 /** 237 * Saves the state to a stream (that is, serializes it). 238 * 239 * @serialData The length of the array is emitted (int), followed by all 240 * of its elements (each a {@code double}) in the proper order. 241 */ 242 private void writeObject(java.io.ObjectOutputStream s) 243 throws java.io.IOException { 244 s.defaultWriteObject(); 245 246 // Write out array length 247 int length = length(); 248 s.writeInt(length); 249 250 // Write out all elements in the proper order. 251 for (int i = 0; i < length; i++) { 252 s.writeDouble(get(i)); 253 } 254 } 255 256 /** 257 * Reconstitutes the instance from a stream (that is, deserializes it). 258 */ 259 private void readObject(java.io.ObjectInputStream s) 260 throws java.io.IOException, ClassNotFoundException { 261 s.defaultReadObject(); 262 263 // Read in array length and allocate array 264 int length = s.readInt(); 265 this.longs = new AtomicLongArray(length); 266 267 // Read in all elements in the proper order. 268 for (int i = 0; i < length; i++) { 269 set(i, s.readDouble()); 270 } 271 } 272}