rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicLongArray.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 10:52:01 +0200
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
permissions -rw-r--r--
Concurrency utilities
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  *
     4  * This code is free software; you can redistribute it and/or modify it
     5  * under the terms of the GNU General Public License version 2 only, as
     6  * published by the Free Software Foundation.  Oracle designates this
     7  * particular file as subject to the "Classpath" exception as provided
     8  * by Oracle in the LICENSE file that accompanied this code.
     9  *
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    13  * version 2 for more details (a copy is included in the LICENSE file that
    14  * accompanied this code).
    15  *
    16  * You should have received a copy of the GNU General Public License version
    17  * 2 along with this work; if not, write to the Free Software Foundation,
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    19  *
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    21  * or visit www.oracle.com if you need additional information or have any
    22  * questions.
    23  */
    24 
    25 /*
    26  * This file is available under and governed by the GNU General Public
    27  * License version 2 only, as published by the Free Software Foundation.
    28  * However, the following notice accompanied the original version of this
    29  * file:
    30  *
    31  * Written by Doug Lea with assistance from members of JCP JSR-166
    32  * Expert Group and released to the public domain, as explained at
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    34  */
    35 
    36 package java.util.concurrent.atomic;
    37 
    38 /**
    39  * A {@code long} array in which elements may be updated atomically.
    40  * See the {@link java.util.concurrent.atomic} package specification
    41  * for description of the properties of atomic variables.
    42  * @since 1.5
    43  * @author Doug Lea
    44  */
    45 public class AtomicLongArray implements java.io.Serializable {
    46     private static final long serialVersionUID = -2308431214976778248L;
    47 
    48     private final long[] array;
    49 
    50     /**
    51      * Creates a new AtomicLongArray of the given length, with all
    52      * elements initially zero.
    53      *
    54      * @param length the length of the array
    55      */
    56     public AtomicLongArray(int length) {
    57         array = new long[length];
    58     }
    59 
    60     /**
    61      * Creates a new AtomicLongArray with the same length as, and
    62      * all elements copied from, the given array.
    63      *
    64      * @param array the array to copy elements from
    65      * @throws NullPointerException if array is null
    66      */
    67     public AtomicLongArray(long[] array) {
    68         // Visibility guaranteed by final field guarantees
    69         this.array = array.clone();
    70     }
    71 
    72     /**
    73      * Returns the length of the array.
    74      *
    75      * @return the length of the array
    76      */
    77     public final int length() {
    78         return array.length;
    79     }
    80 
    81     /**
    82      * Gets the current value at position {@code i}.
    83      *
    84      * @param i the index
    85      * @return the current value
    86      */
    87     public final long get(int i) {
    88         return array[i];
    89     }
    90 
    91     /**
    92      * Sets the element at position {@code i} to the given value.
    93      *
    94      * @param i the index
    95      * @param newValue the new value
    96      */
    97     public final void set(int i, long newValue) {
    98         array[i] = newValue;
    99     }
   100 
   101     /**
   102      * Eventually sets the element at position {@code i} to the given value.
   103      *
   104      * @param i the index
   105      * @param newValue the new value
   106      * @since 1.6
   107      */
   108     public final void lazySet(int i, long newValue) {
   109         array[i] = newValue;
   110     }
   111 
   112 
   113     /**
   114      * Atomically sets the element at position {@code i} to the given value
   115      * and returns the old value.
   116      *
   117      * @param i the index
   118      * @param newValue the new value
   119      * @return the previous value
   120      */
   121     public final long getAndSet(int i, long newValue) {
   122         long v = array[i];
   123         array[i] = newValue;
   124         return v;
   125     }
   126 
   127     /**
   128      * Atomically sets the element at position {@code i} to the given
   129      * updated value if the current value {@code ==} the expected value.
   130      *
   131      * @param i the index
   132      * @param expect the expected value
   133      * @param update the new value
   134      * @return true if successful. False return indicates that
   135      * the actual value was not equal to the expected value.
   136      */
   137     public final boolean compareAndSet(int i, long expect, long update) {
   138         if (array[i] == expect) {
   139             array[i] = update;
   140             return true;
   141         } else {
   142             return false;
   143         }
   144     }
   145 
   146     /**
   147      * Atomically sets the element at position {@code i} to the given
   148      * updated value if the current value {@code ==} the expected value.
   149      *
   150      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   151      * and does not provide ordering guarantees, so is only rarely an
   152      * appropriate alternative to {@code compareAndSet}.
   153      *
   154      * @param i the index
   155      * @param expect the expected value
   156      * @param update the new value
   157      * @return true if successful.
   158      */
   159     public final boolean weakCompareAndSet(int i, long expect, long update) {
   160         return compareAndSet(i, expect, update);
   161     }
   162 
   163     /**
   164      * Atomically increments by one the element at index {@code i}.
   165      *
   166      * @param i the index
   167      * @return the previous value
   168      */
   169     public final long getAndIncrement(int i) {
   170         return getAndAdd(i, 1);
   171     }
   172 
   173     /**
   174      * Atomically decrements by one the element at index {@code i}.
   175      *
   176      * @param i the index
   177      * @return the previous value
   178      */
   179     public final long getAndDecrement(int i) {
   180         return getAndAdd(i, -1);
   181     }
   182 
   183     /**
   184      * Atomically adds the given value to the element at index {@code i}.
   185      *
   186      * @param i the index
   187      * @param delta the value to add
   188      * @return the previous value
   189      */
   190     public final long getAndAdd(int i, long delta) {
   191         long v = array[i];
   192         array[i] += delta;
   193         return v;
   194     }
   195 
   196     /**
   197      * Atomically increments by one the element at index {@code i}.
   198      *
   199      * @param i the index
   200      * @return the updated value
   201      */
   202     public final long incrementAndGet(int i) {
   203         return addAndGet(i, 1);
   204     }
   205 
   206     /**
   207      * Atomically decrements by one the element at index {@code i}.
   208      *
   209      * @param i the index
   210      * @return the updated value
   211      */
   212     public final long decrementAndGet(int i) {
   213         return addAndGet(i, -1);
   214     }
   215 
   216     /**
   217      * Atomically adds the given value to the element at index {@code i}.
   218      *
   219      * @param i the index
   220      * @param delta the value to add
   221      * @return the updated value
   222      */
   223     public long addAndGet(int i, long delta) {
   224         array[i] += delta;
   225         return array[i];
   226     }
   227 
   228     /**
   229      * Returns the String representation of the current values of array.
   230      * @return the String representation of the current values of array
   231      */
   232     public String toString() {
   233         int iMax = array.length - 1;
   234         if (iMax == -1)
   235             return "[]";
   236 
   237         StringBuilder b = new StringBuilder();
   238         b.append('[');
   239         for (int i = 0; ; i++) {
   240             b.append(get(i));
   241             if (i == iMax)
   242                 return b.append(']').toString();
   243             b.append(',').append(' ');
   244         }
   245     }
   246 
   247 }