Initial import
[jpf-core.git] / src / classes / java / util / concurrent / atomic / AtomicReferenceArray.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18
19 package java.util.concurrent.atomic;
20
21 import java.io.Serializable;
22 import java.util.Arrays;
23
24 /**
25  * model class for AtomicReferenceArray
26  */
27 public class AtomicReferenceArray<E> implements Serializable {
28   private static final long serialVersionUID = -6209656149925076980L;
29
30   private final Object[] array;
31
32   public AtomicReferenceArray(int length) {
33     array = new Object[length];
34     // <2do> need a volatile write in order to conform to JMM  // Does this really matter in JPF?
35   }
36
37   public AtomicReferenceArray(E[] array) {
38     if (array == null)
39       throw new NullPointerException();
40
41     int length = array.length;
42     this.array = new Object[length];
43
44     for (int i = 0; i < length; ++i)
45       this.array[i] = array[i];
46
47     // <2do> need a volatile write in order to conform to JMM  // Does this really matter in JPF?
48   }
49
50   public final int length() {
51     return(array.length);
52   }
53
54   public final E get(int i) {
55     checkIndex(i);
56     return(getNative(i));
57   }
58
59   private final native E getNative(int i);
60
61   public final boolean compareAndSet(int i, E expect, E update) {
62     checkIndex(i);
63     return(compareAndSetNative(i, expect, update));
64   }
65
66   private final native boolean compareAndSetNative(int i, E expect, E update);
67
68   public final boolean weakCompareAndSet(int i, E expect, E update) {
69     return(compareAndSet(i, expect, update));
70   }
71
72   public final E getAndSet(int i, E newValue) {
73     while (true) {
74       E current = get(i);
75       if (compareAndSet(i, current, newValue))
76         return(current);
77     }
78   }
79
80   public final void set(int i, E newValue) {
81     getAndSet(i, newValue);
82   }
83
84   public final void lazySet(int i, E newValue) {
85     set(i, newValue);
86   }
87
88   @Override
89   public String toString() {
90     // <2do> need a volatile read in order to conform to JMM  // Does this really matter in JPF?
91     return(Arrays.toString(array));
92   }
93
94   private void checkIndex(int i) {
95     if (i < 0 || i >= array.length)
96       throw new IndexOutOfBoundsException("index " + i);
97   }
98 }