2b2854a2559b4a94547afbf027b28610e23eb073
[IRC.git] / Robust / src / ClassLibrary / MGC / gnu / AtomicInteger.java
1 /*
2  * Written by Doug Lea with assistance from members of JCP JSR-166
3  * Expert Group and released to the public domain, as explained at
4  * http://creativecommons.org/licenses/publicdomain
5  */
6
7 /*package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;*/
9
10 /**
11  * An <tt>int</tt> value that may be updated atomically.  See the
12  * {@link java.util.concurrent.atomic} package specification for
13  * description of the properties of atomic variables. An
14  * <tt>AtomicInteger</tt> is used in applications such as atomically
15  * incremented counters, and cannot be used as a replacement for an
16  * {@link java.lang.Integer}. However, this class does extend
17  * <tt>Number</tt> to allow uniform access by tools and utilities that
18  * deal with numerically-based classes.
19  *
20  * @since 1.5
21  * @author Doug Lea
22 */
23 public class AtomicInteger /*extends Number*/ implements /*java.io.*/Serializable {
24     private static final long serialVersionUID = 6214790243416807050L;
25
26     // setup to use Unsafe.compareAndSwapInt for updates
27     /*private static final Unsafe unsafe = Unsafe.getUnsafe();
28     private static final long valueOffset;
29
30     static {
31       try {
32         valueOffset = unsafe.objectFieldOffset
33             (AtomicInteger.class.getDeclaredField("value"));
34       } catch (Exception ex) { throw new Error(ex); }
35     }*/
36
37     private volatile int value;
38
39     /**
40      * Creates a new AtomicInteger with the given initial value.
41      *
42      * @param initialValue the initial value
43      */
44     public AtomicInteger(int initialValue) {
45         value = initialValue;
46     }
47
48     /**
49      * Creates a new AtomicInteger with initial value <tt>0</tt>.
50      */
51     public AtomicInteger() {
52     }
53
54     /**
55      * Gets the current value.
56      *
57      * @return the current value
58      */
59     public final int get() {
60         return value;
61     }
62
63     /**
64      * Sets to the given value.
65      *
66      * @param newValue the new value
67      */
68     public final void set(int newValue) {
69         value = newValue;
70     }
71
72     /**
73      * Eventually sets to the given value.
74      *
75      * @param newValue the new value
76      * @since 1.6
77      */
78     public final void lazySet(int newValue) {
79         //unsafe.putOrderedInt(this, valueOffset, newValue);
80         System.out.println("Unimplemented AtomicInteger.lazySet()!");
81     }
82
83     /**
84      * Atomically sets to the given value and returns the old value.
85      *
86      * @param newValue the new value
87      * @return the previous value
88      */
89     public final int getAndSet(int newValue) {
90         for (;;) {
91             int current = get();
92             if (compareAndSet(current, newValue))
93                 return current;
94         }
95     }
96
97     /**
98      * Atomically sets the value to the given updated value
99      * if the current value <tt>==</tt> the expected value.
100      *
101      * @param expect the expected value
102      * @param update the new value
103      * @return true if successful. False return indicates that
104      * the actual value was not equal to the expected value.
105      */
106     public final boolean compareAndSet(int expect, int update) {
107       System.out.println("Unimplemented AtomicInteger.compareAndSet()!");
108         return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
109     }
110
111     /**
112      * Atomically sets the value to the given updated value
113      * if the current value <tt>==</tt> the expected value.
114      * May fail spuriously and does not provide ordering guarantees,
115      * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
116      *
117      * @param expect the expected value
118      * @param update the new value
119      * @return true if successful.
120      */
121     public final boolean weakCompareAndSet(int expect, int update) {
122       System.out.println("Unimplemented AtomicInteger.weakCompareAndSet()!");
123         return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
124     }
125
126     /**
127      * Atomically increments by one the current value.
128      *
129      * @return the previous value
130      */
131     public final int getAndIncrement() {
132         for (;;) {
133             int current = get();
134             int next = current + 1;
135             if (compareAndSet(current, next))
136                 return current;
137         }
138     }
139
140     /**
141      * Atomically decrements by one the current value.
142      *
143      * @return the previous value
144      */
145     public final int getAndDecrement() {
146         for (;;) {
147             int current = get();
148             int next = current - 1;
149             if (compareAndSet(current, next))
150                 return current;
151         }
152     }
153
154     /**
155      * Atomically adds the given value to the current value.
156      *
157      * @param delta the value to add
158      * @return the previous value
159      */
160     public final int getAndAdd(int delta) {
161         for (;;) {
162             int current = get();
163             int next = current + delta;
164             if (compareAndSet(current, next))
165                 return current;
166         }
167     }
168
169     /**
170      * Atomically increments by one the current value.
171      *
172      * @return the updated value
173      */
174     public final int incrementAndGet() {
175         for (;;) {
176             int current = get();
177             int next = current + 1;
178             if (compareAndSet(current, next))
179                 return next;
180         }
181     }
182
183     /**
184      * Atomically decrements by one the current value.
185      *
186      * @return the updated value
187      */
188     public final int decrementAndGet() {
189         for (;;) {
190             int current = get();
191             int next = current - 1;
192             if (compareAndSet(current, next))
193                 return next;
194         }
195     }
196
197     /**
198      * Atomically adds the given value to the current value.
199      *
200      * @param delta the value to add
201      * @return the updated value
202      */
203     public final int addAndGet(int delta) {
204         for (;;) {
205             int current = get();
206             int next = current + delta;
207             if (compareAndSet(current, next))
208                 return next;
209         }
210     }
211
212     /**
213      * Returns the String representation of the current value.
214      * @return the String representation of the current value.
215      */
216     /*public String toString() {
217         return Integer.toString(get());
218     }*/
219
220
221     public int intValue() {
222         return get();
223     }
224
225     public long longValue() {
226         return (long)get();
227     }
228
229     public float floatValue() {
230         return (float)get();
231     }
232
233     public double doubleValue() {
234         return (double)get();
235     }
236
237 }