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