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. 025 * See the {@link java.util.concurrent.atomic} package specification 026 * for description of the properties of atomic variables. 027 * 028 * <p><a name="bitEquals">This class compares primitive {@code double} 029 * values in methods such as {@link #compareAndSet} by comparing their 030 * bitwise representation using {@link Double#doubleToRawLongBits}, 031 * which differs from both the primitive double {@code ==} operator 032 * and from {@link Double#equals}, as if implemented by: 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 * }}</pre> 039 * 040 * @author Doug Lea 041 * @author Martin Buchholz 042 * @since 11.0 043 */ 044@GwtIncompatible 045public class AtomicDoubleArray implements java.io.Serializable { 046 private static final long serialVersionUID = 0L; 047 048 // Making this non-final is the lesser evil according to Effective 049 // Java 2nd Edition Item 76: Write readObject methods defensively. 050 private transient AtomicLongArray longs; 051 052 /** 053 * Creates a new {@code AtomicDoubleArray} of the given length, 054 * with all elements initially zero. 055 * 056 * @param length the length of the array 057 */ 058 public AtomicDoubleArray(int length) { 059 this.longs = new AtomicLongArray(length); 060 } 061 062 /** 063 * Creates a new {@code AtomicDoubleArray} with the same length 064 * as, and all elements copied from, the given array. 065 * 066 * @param array the array to copy elements from 067 * @throws NullPointerException if array is null 068 */ 069 public AtomicDoubleArray(double[] array) { 070 final int len = array.length; 071 long[] longArray = new long[len]; 072 for (int i = 0; i < len; i++) { 073 longArray[i] = doubleToRawLongBits(array[i]); 074 } 075 this.longs = new AtomicLongArray(longArray); 076 } 077 078 /** 079 * Returns the length of the array. 080 * 081 * @return the length of the array 082 */ 083 public final int length() { 084 return longs.length(); 085 } 086 087 /** 088 * Gets the current value at position {@code i}. 089 * 090 * @param i the index 091 * @return the current value 092 */ 093 public final double get(int i) { 094 return longBitsToDouble(longs.get(i)); 095 } 096 097 /** 098 * Sets the element at position {@code i} to the given value. 099 * 100 * @param i the index 101 * @param newValue the new value 102 */ 103 public final void set(int i, double newValue) { 104 long next = doubleToRawLongBits(newValue); 105 longs.set(i, next); 106 } 107 108 /** 109 * Eventually sets the element at position {@code i} to the given value. 110 * 111 * @param i the index 112 * @param newValue the new value 113 */ 114 public final void lazySet(int i, double newValue) { 115 set(i, newValue); 116 // TODO(user): replace with code below when jdk5 support is dropped. 117 // long next = doubleToRawLongBits(newValue); 118 // longs.lazySet(i, next); 119 } 120 121 /** 122 * Atomically sets the element at position {@code i} to the given value 123 * and returns the old value. 124 * 125 * @param i the index 126 * @param newValue the new value 127 * @return the previous value 128 */ 129 public final double getAndSet(int i, double newValue) { 130 long next = doubleToRawLongBits(newValue); 131 return longBitsToDouble(longs.getAndSet(i, next)); 132 } 133 134 /** 135 * Atomically sets the element at position {@code i} to the given 136 * updated value 137 * if the current value is <a href="#bitEquals">bitwise equal</a> 138 * to the expected value. 139 * 140 * @param i the index 141 * @param expect the expected value 142 * @param update the new value 143 * @return true if successful. False return indicates that 144 * the actual value was not equal to the expected value. 145 */ 146 public final boolean compareAndSet(int i, double expect, double update) { 147 return longs.compareAndSet(i, 148 doubleToRawLongBits(expect), 149 doubleToRawLongBits(update)); 150 } 151 152 /** 153 * Atomically sets the element at position {@code i} to the given 154 * updated value 155 * if the current value is <a href="#bitEquals">bitwise equal</a> 156 * to the expected value. 157 * 158 * <p>May <a 159 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 160 * fail spuriously</a> 161 * and does not provide ordering guarantees, so is only rarely an 162 * appropriate alternative to {@code compareAndSet}. 163 * 164 * @param i the index 165 * @param expect the expected value 166 * @param update the new value 167 * @return true if successful 168 */ 169 public final boolean weakCompareAndSet(int i, double expect, double update) { 170 return longs.weakCompareAndSet(i, 171 doubleToRawLongBits(expect), 172 doubleToRawLongBits(update)); 173 } 174 175 /** 176 * Atomically adds the given value to the element at index {@code i}. 177 * 178 * @param i the index 179 * @param delta the value to add 180 * @return the previous value 181 */ 182 @CanIgnoreReturnValue 183 public final double getAndAdd(int i, double delta) { 184 while (true) { 185 long current = longs.get(i); 186 double currentVal = longBitsToDouble(current); 187 double nextVal = currentVal + delta; 188 long next = doubleToRawLongBits(nextVal); 189 if (longs.compareAndSet(i, current, next)) { 190 return currentVal; 191 } 192 } 193 } 194 195 /** 196 * Atomically adds the given value to the element at index {@code i}. 197 * 198 * @param i the index 199 * @param delta the value to add 200 * @return the updated value 201 */ 202 @CanIgnoreReturnValue 203 public double addAndGet(int i, double delta) { 204 while (true) { 205 long current = longs.get(i); 206 double currentVal = longBitsToDouble(current); 207 double nextVal = currentVal + delta; 208 long next = doubleToRawLongBits(nextVal); 209 if (longs.compareAndSet(i, current, next)) { 210 return nextVal; 211 } 212 } 213 } 214 215 /** 216 * Returns the String representation of the current values of array. 217 * @return the String representation of the current values of array 218 */ 219 public String toString() { 220 int iMax = length() - 1; 221 if (iMax == -1) { 222 return "[]"; 223 } 224 225 // Double.toString(Math.PI).length() == 17 226 StringBuilder b = new StringBuilder((17 + 2) * (iMax + 1)); 227 b.append('['); 228 for (int i = 0;; i++) { 229 b.append(longBitsToDouble(longs.get(i))); 230 if (i == iMax) { 231 return b.append(']').toString(); 232 } 233 b.append(',').append(' '); 234 } 235 } 236 237 /** 238 * Saves the state to a stream (that is, serializes it). 239 * 240 * @serialData The length of the array is emitted (int), followed by all 241 * of its elements (each a {@code double}) in the proper order. 242 */ 243 private void writeObject(java.io.ObjectOutputStream s) 244 throws java.io.IOException { 245 s.defaultWriteObject(); 246 247 // Write out array length 248 int length = length(); 249 s.writeInt(length); 250 251 // Write out all elements in the proper order. 252 for (int i = 0; i < length; i++) { 253 s.writeDouble(get(i)); 254 } 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 this.longs = new AtomicLongArray(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}