rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicInteger.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 10:52:01 +0200
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
permissions -rw-r--r--
Concurrency utilities
     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 /**
    39  * An {@code int} value that may be updated atomically.  See the
    40  * {@link java.util.concurrent.atomic} package specification for
    41  * description of the properties of atomic variables. An
    42  * {@code AtomicInteger} is used in applications such as atomically
    43  * incremented counters, and cannot be used as a replacement for an
    44  * {@link java.lang.Integer}. However, this class does extend
    45  * {@code Number} to allow uniform access by tools and utilities that
    46  * deal with numerically-based classes.
    47  *
    48  * @since 1.5
    49  * @author Doug Lea
    50 */
    51 public class AtomicInteger extends Number implements java.io.Serializable {
    52     private static final long serialVersionUID = 6214790243416807050L;
    53 
    54     private volatile int value;
    55 
    56     /**
    57      * Creates a new AtomicInteger with the given initial value.
    58      *
    59      * @param initialValue the initial value
    60      */
    61     public AtomicInteger(int initialValue) {
    62         value = initialValue;
    63     }
    64 
    65     /**
    66      * Creates a new AtomicInteger with initial value {@code 0}.
    67      */
    68     public AtomicInteger() {
    69     }
    70 
    71     /**
    72      * Gets the current value.
    73      *
    74      * @return the current value
    75      */
    76     public final int get() {
    77         return value;
    78     }
    79 
    80     /**
    81      * Sets to the given value.
    82      *
    83      * @param newValue the new value
    84      */
    85     public final void set(int newValue) {
    86         value = newValue;
    87     }
    88 
    89     /**
    90      * Eventually sets to the given value.
    91      *
    92      * @param newValue the new value
    93      * @since 1.6
    94      */
    95     public final void lazySet(int newValue) {
    96         value = newValue;
    97     }
    98 
    99     /**
   100      * Atomically sets to the given value and returns the old value.
   101      *
   102      * @param newValue the new value
   103      * @return the previous value
   104      */
   105     public final int getAndSet(int newValue) {
   106         for (;;) {
   107             int current = get();
   108             if (compareAndSet(current, newValue))
   109                 return current;
   110         }
   111     }
   112 
   113     /**
   114      * Atomically sets the value to the given updated value
   115      * if the current value {@code ==} the expected value.
   116      *
   117      * @param expect the expected value
   118      * @param update the new value
   119      * @return true if successful. False return indicates that
   120      * the actual value was not equal to the expected value.
   121      */
   122     public final boolean compareAndSet(int expect, int update) {
   123         if (value == expect) {
   124             value = update;
   125             return true;
   126         } else {
   127             return false;
   128         }
   129     }
   130 
   131     /**
   132      * Atomically sets the value to the given updated value
   133      * if the current value {@code ==} the expected value.
   134      *
   135      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   136      * and does not provide ordering guarantees, so is only rarely an
   137      * appropriate alternative to {@code compareAndSet}.
   138      *
   139      * @param expect the expected value
   140      * @param update the new value
   141      * @return true if successful.
   142      */
   143     public final boolean weakCompareAndSet(int expect, int update) {
   144         return compareAndSet(expect, update);
   145     }
   146 
   147     /**
   148      * Atomically increments by one the current value.
   149      *
   150      * @return the previous value
   151      */
   152     public final int getAndIncrement() {
   153         for (;;) {
   154             int current = get();
   155             int next = current + 1;
   156             if (compareAndSet(current, next))
   157                 return current;
   158         }
   159     }
   160 
   161     /**
   162      * Atomically decrements by one the current value.
   163      *
   164      * @return the previous value
   165      */
   166     public final int getAndDecrement() {
   167         for (;;) {
   168             int current = get();
   169             int next = current - 1;
   170             if (compareAndSet(current, next))
   171                 return current;
   172         }
   173     }
   174 
   175     /**
   176      * Atomically adds the given value to the current value.
   177      *
   178      * @param delta the value to add
   179      * @return the previous value
   180      */
   181     public final int getAndAdd(int delta) {
   182         for (;;) {
   183             int current = get();
   184             int next = current + delta;
   185             if (compareAndSet(current, next))
   186                 return current;
   187         }
   188     }
   189 
   190     /**
   191      * Atomically increments by one the current value.
   192      *
   193      * @return the updated value
   194      */
   195     public final int incrementAndGet() {
   196         for (;;) {
   197             int current = get();
   198             int next = current + 1;
   199             if (compareAndSet(current, next))
   200                 return next;
   201         }
   202     }
   203 
   204     /**
   205      * Atomically decrements by one the current value.
   206      *
   207      * @return the updated value
   208      */
   209     public final int decrementAndGet() {
   210         for (;;) {
   211             int current = get();
   212             int next = current - 1;
   213             if (compareAndSet(current, next))
   214                 return next;
   215         }
   216     }
   217 
   218     /**
   219      * Atomically adds the given value to the current value.
   220      *
   221      * @param delta the value to add
   222      * @return the updated value
   223      */
   224     public final int addAndGet(int delta) {
   225         for (;;) {
   226             int current = get();
   227             int next = current + delta;
   228             if (compareAndSet(current, next))
   229                 return next;
   230         }
   231     }
   232 
   233     /**
   234      * Returns the String representation of the current value.
   235      * @return the String representation of the current value.
   236      */
   237     public String toString() {
   238         return Integer.toString(get());
   239     }
   240 
   241 
   242     public int intValue() {
   243         return get();
   244     }
   245 
   246     public long longValue() {
   247         return (long)get();
   248     }
   249 
   250     public float floatValue() {
   251         return (float)get();
   252     }
   253 
   254     public double doubleValue() {
   255         return (double)get();
   256     }
   257 
   258 }