rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.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  * An {@code int} array in which elements may be updated atomically.
    40  * See the {@link java.util.concurrent.atomic} package
    41  * specification for description of the properties of atomic
    42  * variables.
    43  * @since 1.5
    44  * @author Doug Lea
    45  */
    46 public class AtomicIntegerArray implements java.io.Serializable {
    47     private static final long serialVersionUID = 2862133569453604235L;
    48 
    49     private final int[] array;
    50 
    51     /**
    52      * Creates a new AtomicIntegerArray of the given length, with all
    53      * elements initially zero.
    54      *
    55      * @param length the length of the array
    56      */
    57     public AtomicIntegerArray(int length) {
    58         array = new int[length];
    59     }
    60 
    61     /**
    62      * Creates a new AtomicIntegerArray with the same length as, and
    63      * all elements copied from, the given array.
    64      *
    65      * @param array the array to copy elements from
    66      * @throws NullPointerException if array is null
    67      */
    68     public AtomicIntegerArray(int[] array) {
    69         // Visibility guaranteed by final field guarantees
    70         this.array = array.clone();
    71     }
    72 
    73     /**
    74      * Returns the length of the array.
    75      *
    76      * @return the length of the array
    77      */
    78     public final int length() {
    79         return array.length;
    80     }
    81 
    82     /**
    83      * Gets the current value at position {@code i}.
    84      *
    85      * @param i the index
    86      * @return the current value
    87      */
    88     public final int get(int i) {
    89         return array[i];
    90     }
    91 
    92     /**
    93      * Sets the element at position {@code i} to the given value.
    94      *
    95      * @param i the index
    96      * @param newValue the new value
    97      */
    98     public final void set(int i, int newValue) {
    99         array[i] = newValue;
   100     }
   101 
   102     /**
   103      * Eventually sets the element at position {@code i} to the given value.
   104      *
   105      * @param i the index
   106      * @param newValue the new value
   107      * @since 1.6
   108      */
   109     public final void lazySet(int i, int newValue) {
   110         array[i] = newValue;
   111     }
   112 
   113     /**
   114      * Atomically sets the element at position {@code i} to the given
   115      * value 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 int getAndSet(int i, int newValue) {
   122         int current = array[i];
   123         array[i] = newValue;
   124         return current;
   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, int expect, int 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, int expect, int 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 int 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 int 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 int getAndAdd(int i, int delta) {
   191         int 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 int 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 int 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 final int addAndGet(int i, int 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 }