~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/classpath/java/util/concurrent/atomic/AtomicIntegerArray.java

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
import java.util.*;
 
39
 
 
40
/**
 
41
 * An {@code int} array in which elements may be updated atomically.
 
42
 * See the {@link java.util.concurrent.atomic} package
 
43
 * specification for description of the properties of atomic
 
44
 * variables.
 
45
 * @since 1.5
 
46
 * @author Doug Lea
 
47
 */
 
48
public class AtomicIntegerArray implements java.io.Serializable {
 
49
    private static final long serialVersionUID = 2862133569453604235L;
 
50
 
 
51
    private final int[] array;
 
52
 
 
53
    /**
 
54
     * Creates a new AtomicIntegerArray of the given length, with all
 
55
     * elements initially zero.
 
56
     *
 
57
     * @param length the length of the array
 
58
     */
 
59
    public AtomicIntegerArray(int length) {
 
60
        array = new int[length];
 
61
    }
 
62
 
 
63
    /**
 
64
     * Creates a new AtomicIntegerArray with the same length as, and
 
65
     * all elements copied from, the given array.
 
66
     *
 
67
     * @param array the array to copy elements from
 
68
     * @throws NullPointerException if array is null
 
69
     */
 
70
    public AtomicIntegerArray(int[] array) {
 
71
        // Visibility guaranteed by final field guarantees
 
72
        this.array = array.clone();
 
73
    }
 
74
 
 
75
    /**
 
76
     * Returns the length of the array.
 
77
     *
 
78
     * @return the length of the array
 
79
     */
 
80
    public final int length() {
 
81
        return array.length;
 
82
    }
 
83
 
 
84
    /**
 
85
     * Gets the current value at position {@code i}.
 
86
     *
 
87
     * @param i the index
 
88
     * @return the current value
 
89
     */
 
90
    public final native int get(int i);
 
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 native void set(int i, int newValue);
 
99
 
 
100
    /**
 
101
     * Eventually sets the element at position {@code i} to the given value.
 
102
     *
 
103
     * @param i the index
 
104
     * @param newValue the new value
 
105
     * @since 1.6
 
106
     */
 
107
    public final void lazySet(int i, int newValue) {
 
108
        set(i, newValue);
 
109
    }
 
110
 
 
111
    /**
 
112
     * Atomically sets the element at position {@code i} to the given
 
113
     * value and returns the old value.
 
114
     *
 
115
     * @param i the index
 
116
     * @param newValue the new value
 
117
     * @return the previous value
 
118
     */
 
119
    public final native int getAndSet(int i, int newValue);
 
120
 
 
121
    /**
 
122
     * Atomically sets the element at position {@code i} to the given
 
123
     * updated value if the current value {@code ==} the expected value.
 
124
     *
 
125
     * @param i the index
 
126
     * @param expect the expected value
 
127
     * @param update the new value
 
128
     * @return true if successful. False return indicates that
 
129
     * the actual value was not equal to the expected value.
 
130
     */
 
131
    public final native boolean compareAndSet(int i, int expect, int update);
 
132
 
 
133
    /**
 
134
     * Atomically sets the element at position {@code i} to the given
 
135
     * updated value if the current value {@code ==} the expected value.
 
136
     *
 
137
     * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 
138
     * and does not provide ordering guarantees, so is only rarely an
 
139
     * appropriate alternative to {@code compareAndSet}.
 
140
     *
 
141
     * @param i the index
 
142
     * @param expect the expected value
 
143
     * @param update the new value
 
144
     * @return true if successful.
 
145
     */
 
146
    public final boolean weakCompareAndSet(int i, int expect, int update) {
 
147
        return compareAndSet(i, expect, update);
 
148
    }
 
149
 
 
150
    /**
 
151
     * Atomically increments by one the element at index {@code i}.
 
152
     *
 
153
     * @param i the index
 
154
     * @return the previous value
 
155
     */
 
156
    public final int getAndIncrement(int i) {
 
157
        return incrementAndGet(i) - 1;
 
158
    }
 
159
 
 
160
    /**
 
161
     * Atomically decrements by one the element at index {@code i}.
 
162
     *
 
163
     * @param i the index
 
164
     * @return the previous value
 
165
     */
 
166
    public final int getAndDecrement(int i) {
 
167
        return decrementAndGet(i) + 1;
 
168
    }
 
169
 
 
170
    /**
 
171
     * Atomically adds the given value to the element at index {@code i}.
 
172
     *
 
173
     * @param i the index
 
174
     * @param delta the value to add
 
175
     * @return the previous value
 
176
     */
 
177
    public final int getAndAdd(int i, int delta) {
 
178
        return addAndGet(i, delta) - delta;
 
179
    }
 
180
 
 
181
    /**
 
182
     * Atomically increments by one the element at index {@code i}.
 
183
     *
 
184
     * @param i the index
 
185
     * @return the updated value
 
186
     */
 
187
    public final native int incrementAndGet(int i);
 
188
 
 
189
    /**
 
190
     * Atomically decrements by one the element at index {@code i}.
 
191
     *
 
192
     * @param i the index
 
193
     * @return the updated value
 
194
     */
 
195
    public final native int decrementAndGet(int i);
 
196
 
 
197
    /**
 
198
     * Atomically adds the given value to the element at index {@code i}.
 
199
     *
 
200
     * @param i the index
 
201
     * @param delta the value to add
 
202
     * @return the updated value
 
203
     */
 
204
    public final native int addAndGet(int i, int delta);
 
205
 
 
206
    /**
 
207
     * Returns the String representation of the current values of array.
 
208
     * @return the String representation of the current values of array.
 
209
     */
 
210
    public String toString() {
 
211
        int iMax = array.length - 1;
 
212
        if (iMax == -1)
 
213
            return "[]";
 
214
 
 
215
        StringBuilder b = new StringBuilder();
 
216
        b.append('[');
 
217
        for (int i = 0; ; i++) {
 
218
            b.append(get(i));
 
219
            if (i == iMax)
 
220
                return b.append(']').toString();
 
221
            b.append(',').append(' ');
 
222
        }
 
223
    }
 
224
 
 
225
}