jtulach@1334: /* jtulach@1334: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@1334: * jtulach@1334: * This code is free software; you can redistribute it and/or modify it jtulach@1334: * under the terms of the GNU General Public License version 2 only, as jtulach@1334: * published by the Free Software Foundation. Oracle designates this jtulach@1334: * particular file as subject to the "Classpath" exception as provided jtulach@1334: * by Oracle in the LICENSE file that accompanied this code. jtulach@1334: * jtulach@1334: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@1334: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@1334: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@1334: * version 2 for more details (a copy is included in the LICENSE file that jtulach@1334: * accompanied this code). jtulach@1334: * jtulach@1334: * You should have received a copy of the GNU General Public License version jtulach@1334: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@1334: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@1334: * jtulach@1334: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@1334: * or visit www.oracle.com if you need additional information or have any jtulach@1334: * questions. jtulach@1334: */ jtulach@1334: jtulach@1334: /* jtulach@1334: * This file is available under and governed by the GNU General Public jtulach@1334: * License version 2 only, as published by the Free Software Foundation. jtulach@1334: * However, the following notice accompanied the original version of this jtulach@1334: * file: jtulach@1334: * jtulach@1334: * Written by Doug Lea with assistance from members of JCP JSR-166 jtulach@1334: * Expert Group and released to the public domain, as explained at jtulach@1334: * http://creativecommons.org/publicdomain/zero/1.0/ jtulach@1334: */ jtulach@1334: jtulach@1334: package java.util.concurrent.atomic; jtulach@1334: jtulach@1334: /** jtulach@1334: * A {@code long} array in which elements may be updated atomically. jtulach@1334: * See the {@link java.util.concurrent.atomic} package specification jtulach@1334: * for description of the properties of atomic variables. jtulach@1334: * @since 1.5 jtulach@1334: * @author Doug Lea jtulach@1334: */ jtulach@1334: public class AtomicLongArray implements java.io.Serializable { jtulach@1334: private static final long serialVersionUID = -2308431214976778248L; jtulach@1334: jtulach@1334: private final long[] array; jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicLongArray of the given length, with all jtulach@1334: * elements initially zero. jtulach@1334: * jtulach@1334: * @param length the length of the array jtulach@1334: */ jtulach@1334: public AtomicLongArray(int length) { jtulach@1334: array = new long[length]; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Creates a new AtomicLongArray with the same length as, and jtulach@1334: * all elements copied from, the given array. jtulach@1334: * jtulach@1334: * @param array the array to copy elements from jtulach@1334: * @throws NullPointerException if array is null jtulach@1334: */ jtulach@1334: public AtomicLongArray(long[] array) { jtulach@1334: // Visibility guaranteed by final field guarantees jtulach@1334: this.array = array.clone(); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the length of the array. jtulach@1334: * jtulach@1334: * @return the length of the array jtulach@1334: */ jtulach@1334: public final int length() { jtulach@1334: return array.length; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Gets the current value at position {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the current value jtulach@1334: */ jtulach@1334: public final long get(int i) { jaroslav@1338: return array[i]; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Sets the element at position {@code i} to the given value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: */ jtulach@1334: public final void set(int i, long newValue) { jaroslav@1338: array[i] = newValue; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Eventually sets the element at position {@code i} to the given value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: * @since 1.6 jtulach@1334: */ jtulach@1334: public final void lazySet(int i, long newValue) { jaroslav@1338: array[i] = newValue; jtulach@1334: } jtulach@1334: jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given value jtulach@1334: * and returns the old value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param newValue the new value jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final long getAndSet(int i, long newValue) { jaroslav@1338: long v = array[i]; jaroslav@1338: array[i] = newValue; jaroslav@1338: return v; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given jtulach@1334: * updated value if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. False return indicates that jtulach@1334: * the actual value was not equal to the expected value. jtulach@1334: */ jtulach@1334: public final boolean compareAndSet(int i, long expect, long update) { jaroslav@1338: if (array[i] == expect) { jaroslav@1338: array[i] = update; jaroslav@1338: return true; jaroslav@1338: } else { jaroslav@1338: return false; jaroslav@1338: } jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically sets the element at position {@code i} to the given jtulach@1334: * updated value if the current value {@code ==} the expected value. jtulach@1334: * jtulach@1334: *

May fail spuriously jtulach@1334: * and does not provide ordering guarantees, so is only rarely an jtulach@1334: * appropriate alternative to {@code compareAndSet}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param expect the expected value jtulach@1334: * @param update the new value jtulach@1334: * @return true if successful. jtulach@1334: */ jtulach@1334: public final boolean weakCompareAndSet(int i, long expect, long update) { jtulach@1334: return compareAndSet(i, expect, update); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final long getAndIncrement(int i) { jtulach@1334: return getAndAdd(i, 1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final long getAndDecrement(int i) { jtulach@1334: return getAndAdd(i, -1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param delta the value to add jtulach@1334: * @return the previous value jtulach@1334: */ jtulach@1334: public final long getAndAdd(int i, long delta) { jaroslav@1338: long v = array[i]; jaroslav@1338: array[i] += delta; jaroslav@1338: return v; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically increments by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final long incrementAndGet(int i) { jtulach@1334: return addAndGet(i, 1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically decrements by one the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public final long decrementAndGet(int i) { jtulach@1334: return addAndGet(i, -1); jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Atomically adds the given value to the element at index {@code i}. jtulach@1334: * jtulach@1334: * @param i the index jtulach@1334: * @param delta the value to add jtulach@1334: * @return the updated value jtulach@1334: */ jtulach@1334: public long addAndGet(int i, long delta) { jaroslav@1338: array[i] += delta; jaroslav@1338: return array[i]; jtulach@1334: } jtulach@1334: jtulach@1334: /** jtulach@1334: * Returns the String representation of the current values of array. jtulach@1334: * @return the String representation of the current values of array jtulach@1334: */ jtulach@1334: public String toString() { jtulach@1334: int iMax = array.length - 1; jtulach@1334: if (iMax == -1) jtulach@1334: return "[]"; jtulach@1334: jtulach@1334: StringBuilder b = new StringBuilder(); jtulach@1334: b.append('['); jtulach@1334: for (int i = 0; ; i++) { jaroslav@1338: b.append(get(i)); jtulach@1334: if (i == iMax) jtulach@1334: return b.append(']').toString(); jtulach@1334: b.append(',').append(' '); jtulach@1334: } jtulach@1334: } jtulach@1334: jtulach@1334: }