rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicIntegerArray.java
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
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
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/
36 package java.util.concurrent.atomic;
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
46 public class AtomicIntegerArray implements java.io.Serializable {
47 private static final long serialVersionUID = 2862133569453604235L;
49 private final int[] array;
52 * Creates a new AtomicIntegerArray of the given length, with all
53 * elements initially zero.
55 * @param length the length of the array
57 public AtomicIntegerArray(int length) {
58 array = new int[length];
62 * Creates a new AtomicIntegerArray with the same length as, and
63 * all elements copied from, the given array.
65 * @param array the array to copy elements from
66 * @throws NullPointerException if array is null
68 public AtomicIntegerArray(int[] array) {
69 // Visibility guaranteed by final field guarantees
70 this.array = array.clone();
74 * Returns the length of the array.
76 * @return the length of the array
78 public final int length() {
83 * Gets the current value at position {@code i}.
86 * @return the current value
88 public final int get(int i) {
93 * Sets the element at position {@code i} to the given value.
96 * @param newValue the new value
98 public final void set(int i, int newValue) {
103 * Eventually sets the element at position {@code i} to the given value.
106 * @param newValue the new value
109 public final void lazySet(int i, int newValue) {
114 * Atomically sets the element at position {@code i} to the given
115 * value and returns the old value.
118 * @param newValue the new value
119 * @return the previous value
121 public final int getAndSet(int i, int newValue) {
122 int current = array[i];
128 * Atomically sets the element at position {@code i} to the given
129 * updated value if the current value {@code ==} the expected value.
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.
137 public final boolean compareAndSet(int i, int expect, int update) {
138 if (array[i] == expect) {
147 * Atomically sets the element at position {@code i} to the given
148 * updated value if the current value {@code ==} the expected value.
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}.
155 * @param expect the expected value
156 * @param update the new value
157 * @return true if successful.
159 public final boolean weakCompareAndSet(int i, int expect, int update) {
160 return compareAndSet(i, expect, update);
164 * Atomically increments by one the element at index {@code i}.
167 * @return the previous value
169 public final int getAndIncrement(int i) {
170 return getAndAdd(i, 1);
174 * Atomically decrements by one the element at index {@code i}.
177 * @return the previous value
179 public final int getAndDecrement(int i) {
180 return getAndAdd(i, -1);
184 * Atomically adds the given value to the element at index {@code i}.
187 * @param delta the value to add
188 * @return the previous value
190 public final int getAndAdd(int i, int delta) {
197 * Atomically increments by one the element at index {@code i}.
200 * @return the updated value
202 public final int incrementAndGet(int i) {
203 return addAndGet(i, 1);
207 * Atomically decrements by one the element at index {@code i}.
210 * @return the updated value
212 public final int decrementAndGet(int i) {
213 return addAndGet(i, -1);
217 * Atomically adds the given value to the element at index {@code i}.
220 * @param delta the value to add
221 * @return the updated value
223 public final int addAndGet(int i, int delta) {
229 * Returns the String representation of the current values of array.
230 * @return the String representation of the current values of array
232 public String toString() {
233 int iMax = array.length - 1;
237 StringBuilder b = new StringBuilder();
239 for (int i = 0; ; i++) {
242 return b.append(']').toString();
243 b.append(',').append(' ');