Initial import
[jpf-core.git] / src / classes / java / util / concurrent / atomic / AtomicLongArray.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 AtomicLongArray
26  */
27 public class AtomicLongArray implements Serializable {
28   private static final long serialVersionUID = -2308431214976778248L;
29
30   private final long[] array;
31
32   public AtomicLongArray(int length) {
33     array = new long[length];
34     // <2do> need a volatile write in order to conform to JMM  // Does this really matter in JPF?
35   }
36
37   public AtomicLongArray(long[] array) {
38     if (array == null)
39       throw new NullPointerException();
40
41     int length = array.length;
42     this.array = new long[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 long get(int i) {
55     checkIndex(i);
56     return(getNative(i));
57   }
58
59   private final native long getNative(int i);
60
61   public final boolean compareAndSet(int i, long expect, long update) {
62     checkIndex(i);
63     return(compareAndSetNative(i, expect, update));
64   }
65
66   private final native boolean compareAndSetNative(int i, long expect, long update);
67
68   public final boolean weakCompareAndSet(int i, long expect, long update) {
69     return(compareAndSet(i, expect, update));
70   }
71
72   public final long getAndSet(int i, long newValue) {
73     while (true) {
74       long current = get(i);
75       if (compareAndSet(i, current, newValue))
76         return(current);
77     }
78   }
79
80   public final void set(int i, long newValue) {
81     getAndSet(i, newValue);
82   }
83
84   public final void lazySet(int i, long newValue) {
85     set(i, newValue);
86   }
87
88   public final long getAndIncrement(int i) {
89     return(getAndAdd(i, 1));
90   }
91
92   public final long getAndDecrement(int i) {
93     return(getAndAdd(i, -1));
94   }
95
96   public final long getAndAdd(int i, long delta) {
97     while (true) {
98       long current = get(i);
99       long next = current + delta;
100       if (compareAndSet(i, current, next))
101         return(current);
102     }
103   }
104
105   public final long incrementAndGet(int i) {
106     return(getAndIncrement(i) + 1);
107   }
108
109   public final long decrementAndGet(int i) {
110     return(getAndDecrement(i) - 1);
111   }
112
113   public final long addAndGet(int i, long delta) {
114     return(getAndAdd(i, delta) + delta);
115   }
116
117   @Override
118   public String toString() {
119     // <2do> need a volatile read in order to conform to JMM  // Does this really matter in JPF?
120     return(Arrays.toString(array));
121   }
122
123   private void checkIndex(int i) {
124     if (i < 0 || i >= array.length)
125       throw new IndexOutOfBoundsException("index " + i);
126   }
127 }