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   129   11   1.78
16   45   0.69   9
9     1.22  
1    
 
  AtomicBoolean       Line # 20 16 11 95.1% 0.9512195
 
  (14)
 
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    * A {@code boolean} value that may be updated atomically. See the
11    * {@link edu.emory.mathcs.backport.java.util.concurrent.atomic} package specification for
12    * description of the properties of atomic variables. An
13    * {@code AtomicBoolean} is used in applications such as atomically
14    * updated flags, and cannot be used as a replacement for a
15    * {@link java.lang.Boolean}.
16    *
17    * @since 1.5
18    * @author Doug Lea
19    */
 
20    public class AtomicBoolean implements java.io.Serializable {
21    private static final long serialVersionUID = 4654671469794556979L;
22   
23    private volatile int value;
24   
25    /**
26    * Creates a new {@code AtomicBoolean} with the given initial value.
27    *
28    * @param initialValue the initial value
29    */
 
30  9 toggle public AtomicBoolean(boolean initialValue) {
31  9 value = initialValue ? 1 : 0;
32    }
33   
34    /**
35    * Creates a new {@code AtomicBoolean} with initial value {@code false}.
36    */
 
37  4 toggle public AtomicBoolean() {
38    }
39   
40    /**
41    * Returns the current value.
42    *
43    * @return the current value
44    */
 
45  27 toggle public final boolean get() {
46  27 return value != 0;
47    }
48   
49    /**
50    * Atomically sets the value to the given updated value
51    * if the current value {@code ==} the expected value.
52    *
53    * @param expect the expected value
54    * @param update the new value
55    * @return true if successful. False return indicates that
56    * the actual value was not equal to the expected value.
57    */
 
58  6 toggle public final synchronized boolean compareAndSet(boolean expect, boolean update) {
59  6 if (expect == (value != 0)) {
60  5 value = update ? 1 : 0;
61  5 return true;
62    }
63    else {
64  1 return false;
65    }
66    }
67   
68    /**
69    * Atomically sets the value to the given updated value
70    * if the current value {@code ==} the expected value.
71    *
72    * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
73    * and does not provide ordering guarantees, so is only rarely an
74    * appropriate alternative to {@code compareAndSet}.
75    *
76    * @param expect the expected value
77    * @param update the new value
78    * @return true if successful.
79    */
 
80  3 toggle public synchronized boolean weakCompareAndSet(boolean expect, boolean update) {
81  3 if (expect == (value != 0)) {
82  3 value = update ? 1 : 0;
83  3 return true;
84    }
85    else {
86  0 return false;
87    }
88    }
89   
90    /**
91    * Unconditionally sets to the given value.
92    *
93    * @param newValue the new value
94    */
 
95  7 toggle public final synchronized void set(boolean newValue) {
96  7 value = newValue ? 1 : 0;
97    }
98   
99    /**
100    * Eventually sets to the given value.
101    *
102    * @param newValue the new value
103    * @since 1.6
104    */
 
105  2 toggle public final synchronized void lazySet(boolean newValue) {
106  2 value = newValue ? 1 : 0;
107    }
108   
109    /**
110    * Atomically sets to the given value and returns the previous value.
111    *
112    * @param newValue the new value
113    * @return the previous value
114    */
 
115  3 toggle public final synchronized boolean getAndSet(boolean newValue) {
116  3 int old = value;
117  3 value = newValue ? 1 : 0;
118  3 return old != 0;
119    }
120   
121    /**
122    * Returns the String representation of the current value.
123    * @return the String representation of the current value.
124    */
 
125  2 toggle public String toString() {
126  2 return Boolean.toString(get());
127    }
128   
129    }