| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
package org.fest.swing.lock; |
| 17 |
|
|
| 18 |
|
import static org.fest.util.Strings.concat; |
| 19 |
|
|
| 20 |
|
import java.util.concurrent.locks.*; |
| 21 |
|
|
| 22 |
|
import net.jcip.annotations.GuardedBy; |
| 23 |
|
import net.jcip.annotations.ThreadSafe; |
| 24 |
|
|
| 25 |
|
import org.fest.swing.exception.ScreenLockException; |
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
@author |
| 32 |
|
@author |
| 33 |
|
|
| 34 |
|
@ThreadSafe |
|
|
|
| 97.8% |
Uncovered Elements: 1 (45) |
Complexity: 12 |
Complexity Density: 0.4 |
|
| 35 |
|
public final class ScreenLock { |
| 36 |
|
|
| 37 |
|
private final Lock lock = new ReentrantLock(); |
| 38 |
|
private final Condition released = lock.newCondition(); |
| 39 |
|
|
| 40 |
|
@GuardedBy("lock") |
| 41 |
|
private Object owner; |
| 42 |
|
|
| 43 |
|
@GuardedBy("lock") |
| 44 |
|
private boolean acquired; |
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
@param |
| 50 |
|
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
| 51 |
1851
|
public void acquire(Object newOwner) {... |
| 52 |
1851
|
lock.lock(); |
| 53 |
1851
|
try { |
| 54 |
1
|
if (alreadyAcquiredBy(newOwner)) return; |
| 55 |
5
|
while (acquired) released.await(); |
| 56 |
1850
|
owner = newOwner; |
| 57 |
1850
|
acquired = true; |
| 58 |
|
} catch (InterruptedException ignored) { |
| 59 |
0
|
Thread.currentThread().interrupt(); |
| 60 |
|
} finally { |
| 61 |
1851
|
lock.unlock(); |
| 62 |
|
} |
| 63 |
|
} |
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
@param |
| 68 |
|
@throws |
| 69 |
|
@throws |
| 70 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 3 |
Complexity Density: 0.3 |
|
| 71 |
1852
|
public void release(Object currentOwner) {... |
| 72 |
1852
|
lock.lock(); |
| 73 |
1852
|
try { |
| 74 |
1
|
if (!acquired) throw new ScreenLockException("No lock to release"); |
| 75 |
1
|
if (owner != currentOwner) throw new ScreenLockException(concat(currentOwner, " is not the lock owner")); |
| 76 |
1850
|
acquired = false; |
| 77 |
1850
|
owner = null; |
| 78 |
1850
|
released.signal(); |
| 79 |
|
} finally { |
| 80 |
1852
|
lock.unlock(); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
|
| 84 |
|
|
| 85 |
|
|
| 86 |
|
@param |
| 87 |
|
@return |
| 88 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 89 |
1604
|
public boolean acquiredBy(Object possibleOwner) {... |
| 90 |
1604
|
lock.lock(); |
| 91 |
1604
|
try { |
| 92 |
1604
|
return alreadyAcquiredBy(possibleOwner); |
| 93 |
|
} finally { |
| 94 |
1604
|
lock.unlock(); |
| 95 |
|
} |
| 96 |
|
} |
| 97 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 98 |
3455
|
private boolean alreadyAcquiredBy(Object possibleOwner) {... |
| 99 |
3455
|
return acquired && owner == possibleOwner; |
| 100 |
|
} |
| 101 |
|
|
| 102 |
|
|
| 103 |
|
|
| 104 |
|
@return |
| 105 |
|
@see |
| 106 |
|
@since |
| 107 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 108 |
10
|
public boolean acquired() {... |
| 109 |
10
|
lock.lock(); |
| 110 |
10
|
try { |
| 111 |
10
|
return acquired; |
| 112 |
|
} finally { |
| 113 |
10
|
lock.unlock(); |
| 114 |
|
} |
| 115 |
|
} |
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
@return |
| 120 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 121 |
3678
|
public static ScreenLock instance() { return ScreenLockHolder.instance; }... |
| 122 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 123 |
|
private static class ScreenLockHolder { |
| 124 |
|
static ScreenLock instance = new ScreenLock(); |
| 125 |
|
} |
| 126 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 127 |
713
|
ScreenLock() {}... |
| 128 |
|
} |