Clover Coverage Report - Backport Util Concurrent v3.0
Coverage timestamp: Fri May 9 2008 11:05:23 EST
../../../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
16   120   11   1.78
4   41   0.69   9
9     1.22  
1    
 
  AtomicReference       Line # 16 16 11 93.1% 0.9310345
 
  (24)
 
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 edu.emory.mathcs.backport.java.util.concurrent.atomic;
8   
9    /**
10    * An object reference that may be updated atomically. See the {@link
11    * edu.emory.mathcs.backport.java.util.concurrent.atomic} package specification for description
12    * of the properties of atomic variables.
13    * @since 1.5
14    * @author Doug Lea
15    */
 
16    public class AtomicReference implements java.io.Serializable {
17    private static final long serialVersionUID = -1848883965231344442L;
18   
19    private volatile Object value;
20   
21    /**
22    * Creates a new AtomicReference with the given initial value.
23    *
24    * @param initialValue the initial value
25    */
 
26  24 toggle public AtomicReference(Object initialValue) {
27  24 value = initialValue;
28    }
29   
30    /**
31    * Creates a new AtomicReference with null initial value.
32    */
 
33  2 toggle public AtomicReference() {
34    }
35   
36    /**
37    * Gets the current value.
38    *
39    * @return the current value
40    */
 
41  100 toggle public final Object get() {
42  100 return value;
43    }
44   
45    /**
46    * Sets to the given value.
47    *
48    * @param newValue the new value
49    */
 
50  8 toggle public final synchronized void set(Object newValue) {
51  8 value = newValue;
52    }
53   
54    /**
55    * Eventually sets to the given value.
56    *
57    * @param newValue the new value
58    * @since 1.6
59    */
 
60  2 toggle public final synchronized void lazySet(Object newValue) {
61  2 value = newValue;
62    }
63   
64    /**
65    * Atomically sets the value to the given updated value
66    * if the current value {@code ==} the expected value.
67    * @param expect the expected value
68    * @param update the new value
69    * @return true if successful. False return indicates that
70    * the actual value was not equal to the expected value.
71    */
 
72  20 toggle public final synchronized boolean compareAndSet(Object expect, Object update) {
73  20 if (value == expect) {
74  19 value = update;
75  19 return true;
76    }
77  1 return false;
78    }
79   
80    /**
81    * Atomically sets the value to the given updated value
82    * if the current value {@code ==} the expected value.
83    *
84    * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
85    * and does not provide ordering guarantees, so is only rarely an
86    * appropriate alternative to {@code compareAndSet}.
87    *
88    * @param expect the expected value
89    * @param update the new value
90    * @return true if successful.
91    */
 
92  7 toggle public final synchronized boolean weakCompareAndSet(Object expect, Object update) {
93  7 if (value == expect) {
94  7 value = update;
95  7 return true;
96    }
97  0 return false;
98    }
99   
100    /**
101    * Atomically sets to the given value and returns the old value.
102    *
103    * @param newValue the new value
104    * @return the previous value
105    */
 
106  3 toggle public final synchronized Object getAndSet(Object newValue) {
107  3 Object old = value;
108  3 value = newValue;
109  3 return old;
110    }
111   
112    /**
113    * Returns the String representation of the current value.
114    * @return the String representation of the current value.
115    */