rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicReference.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 object reference that may be updated atomically. See the {@link
    40  * java.util.concurrent.atomic} package specification for description
    41  * of the properties of atomic variables.
    42  * @since 1.5
    43  * @author Doug Lea
    44  * @param <V> The type of object referred to by this reference
    45  */
    46 public class AtomicReference<V>  implements java.io.Serializable {
    47     private static final long serialVersionUID = -1848883965231344442L;
    48 
    49     private volatile V value;
    50 
    51     /**
    52      * Creates a new AtomicReference with the given initial value.
    53      *
    54      * @param initialValue the initial value
    55      */
    56     public AtomicReference(V initialValue) {
    57         value = initialValue;
    58     }
    59 
    60     /**
    61      * Creates a new AtomicReference with null initial value.
    62      */
    63     public AtomicReference() {
    64     }
    65 
    66     /**
    67      * Gets the current value.
    68      *
    69      * @return the current value
    70      */
    71     public final V get() {
    72         return value;
    73     }
    74 
    75     /**
    76      * Sets to the given value.
    77      *
    78      * @param newValue the new value
    79      */
    80     public final void set(V newValue) {
    81         value = newValue;
    82     }
    83 
    84     /**
    85      * Eventually sets to the given value.
    86      *
    87      * @param newValue the new value
    88      * @since 1.6
    89      */
    90     public final void lazySet(V newValue) {
    91         value = newValue;
    92     }
    93 
    94     /**
    95      * Atomically sets the value to the given updated value
    96      * if the current value {@code ==} the expected value.
    97      * @param expect the expected value
    98      * @param update the new value
    99      * @return true if successful. False return indicates that
   100      * the actual value was not equal to the expected value.
   101      */
   102     public final boolean compareAndSet(V expect, V update) {
   103         if (value == expect) {
   104             value = update;
   105             return true;
   106         } else {
   107             return false;
   108         }
   109     }
   110 
   111     /**
   112      * Atomically sets the value to the given updated value
   113      * if the current value {@code ==} the expected value.
   114      *
   115      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
   116      * and does not provide ordering guarantees, so is only rarely an
   117      * appropriate alternative to {@code compareAndSet}.
   118      *
   119      * @param expect the expected value
   120      * @param update the new value
   121      * @return true if successful.
   122      */
   123     public final boolean weakCompareAndSet(V expect, V update) {
   124         return compareAndSet(expect, update);
   125     }
   126 
   127     /**
   128      * Atomically sets to the given value and returns the old value.
   129      *
   130      * @param newValue the new value
   131      * @return the previous value
   132      */
   133     public final V getAndSet(V newValue) {
   134         while (true) {
   135             V x = get();
   136             if (compareAndSet(x, newValue))
   137                 return x;
   138         }
   139     }
   140 
   141     /**
   142      * Returns the String representation of the current value.
   143      * @return the String representation of the current value.
   144      */
   145     public String toString() {
   146         return String.valueOf(get());
   147     }
   148 
   149 }