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.Beta; 020import com.google.common.primitives.ImmutableLongArray; 021 022import java.util.concurrent.atomic.AtomicLongArray; 023 024/** 025 * A {@code double} array in which elements may be updated atomically. 026 * See the {@link java.util.concurrent.atomic} package specification 027 * for description of the properties of atomic variables. 028 * 029 * <p><a name="bitEquals">This class compares primitive {@code double} 030 * values in methods such as {@link #compareAndSet} by comparing their 031 * bitwise representation using {@link Double#doubleToRawLongBits}, 032 * which differs from both the primitive double {@code ==} operator 033 * and from {@link Double#equals}, as if implemented by: 034 * <pre> {@code 035 * static boolean bitEquals(double x, double y) { 036 * long xBits = Double.doubleToRawLongBits(x); 037 * long yBits = Double.doubleToRawLongBits(y); 038 * return xBits == yBits; 039 * }}</pre> 040 * 041 * @author Doug Lea 042 * @author Martin Buchholz 043 * @since 11.0 044 */ 045@Beta 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 set(i, newValue); 117 // TODO(user): replace with code below when jdk5 support is dropped. 118 // long next = doubleToRawLongBits(newValue); 119 // longs.lazySet(i, next); 120 } 121 122 /** 123 * Atomically sets the element at position {@code i} to the given value 124 * and returns the old value. 125 * 126 * @param i the index 127 * @param newValue the new value 128 * @return the previous value 129 */ 130 public final double getAndSet(int i, double newValue) { 131 long next = doubleToRawLongBits(newValue); 132 return longBitsToDouble(longs.getAndSet(i, next)); 133 } 134 135 /** 136 * Atomically sets the element at position {@code i} to the given 137 * updated value 138 * if the current value is <a href="#bitEquals">bitwise equal</a> 139 * to the expected value. 140 * 141 * @param i the index 142 * @param expect the expected value 143 * @param update the new value 144 * @return true if successful. False return indicates that 145 * the actual value was not equal to the expected value. 146 */ 147 public final boolean compareAndSet(int i, double expect, double update) { 148 return longs.compareAndSet(i, 149 doubleToRawLongBits(expect), 150 doubleToRawLongBits(update)); 151 } 152 153 /** 154 * Atomically sets the element at position {@code i} to the given 155 * updated value 156 * if the current value is <a href="#bitEquals">bitwise equal</a> 157 * to the expected value. 158 * 159 * <p>May <a 160 * href="http://download.oracle.com/javase/7/docs/api/java/util/concurrent/atomic/package-summary.html#Spurious"> 161 * fail spuriously</a> 162 * and does not provide ordering guarantees, so is only rarely an 163 * appropriate alternative to {@code compareAndSet}. 164 * 165 * @param i the index 166 * @param expect the expected value 167 * @param update the new value 168 * @return true if successful 169 */ 170 public final boolean weakCompareAndSet(int i, double expect, double update) { 171 return longs.weakCompareAndSet(i, 172 doubleToRawLongBits(expect), 173 doubleToRawLongBits(update)); 174 } 175 176 /** 177 * Atomically adds the given value to the element at index {@code i}. 178 * 179 * @param i the index 180 * @param delta the value to add 181 * @return the previous value 182 */ 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 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 int length = s.readInt(); 264 ImmutableLongArray.Builder builder = ImmutableLongArray.builder(); 265 266 for (int i = 0; i < length; i++) { 267 builder.add(doubleToRawLongBits(s.readDouble())); 268 } 269 this.longs = new AtomicLongArray(builder.build().toArray()); 270 } 271}