File: J2SE50LockFactory.java

package info (click to toggle)
libglazedlists-java 1.8.0.dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,016 kB
  • sloc: java: 21,991; xml: 860; sh: 48; makefile: 5
file content (95 lines) | stat: -rw-r--r-- 2,935 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* Glazed Lists                                                 (c) 2003-2006 */
/* http://publicobject.com/glazedlists/                      publicobject.com,*/
/*                                                     O'Dell Engineering Ltd.*/
package ca.odell.glazedlists.impl.java15;

import ca.odell.glazedlists.impl.SerializedReadWriteLock;
import ca.odell.glazedlists.util.concurrent.Lock;
import ca.odell.glazedlists.util.concurrent.LockFactory;
import ca.odell.glazedlists.util.concurrent.ReadWriteLock;

import java.io.ObjectStreamException;
import java.io.Serializable;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * An implementation of {@link LockFactory} that has been derived from
 * {@link java.util.concurrent.locks.ReadWriteLock JDK 1.5 Locks}.
 *
 * @author James Lemieux
 */
public class J2SE50LockFactory implements LockFactory {
    public ReadWriteLock createReadWriteLock() {
        return new J2SE50ReadWriteLock();
    }

    public Lock createLock() {
        return new LockAdapter(new java.util.concurrent.locks.ReentrantLock());
    }
}

/**
 * A ReadWriteLock implementation that is compatable with J2SE 5.0 and better. This
 * implementation is a facade over {@link java.util.concurrent.locks.ReadWriteLock}.
 *
 * @author James Lemieux
 */
final class J2SE50ReadWriteLock implements ReadWriteLock, Serializable {

    /** For versioning as a {@link Serializable} */
    private static final long serialVersionUID = 188277016505951193L;
    
    private transient final Lock readLock;
    private transient final Lock writeLock;

    J2SE50ReadWriteLock() {
        final java.util.concurrent.locks.ReadWriteLock delegate = new ReentrantReadWriteLock();
        this.readLock = new LockAdapter(delegate.readLock());
        this.writeLock = new LockAdapter(delegate.writeLock());
    }

    /** Use a {@link SerializedReadWriteLock} as a placeholder in the serialization stream. */
    private Object writeReplace() throws ObjectStreamException {
        return new SerializedReadWriteLock();
    }
    
    /**
     * Return the lock used for reading.
     */
    public Lock readLock() {
        return this.readLock;
    }

    /**
     * Return the lock used for writing.
     */
    public Lock writeLock() {
        return this.writeLock;
    }
}

/**
 * This adapts a J2SE 5.0 compatible Lock to the Glazed Lists Lock interface.
 *
 * @author James Lemieux
 */
final class LockAdapter implements Lock {

    private final java.util.concurrent.locks.Lock delegateLock;

    LockAdapter(java.util.concurrent.locks.Lock delegateLock) {
        this.delegateLock = delegateLock;
    }

    public void lock() {
        delegateLock.lock();
    }

    public boolean tryLock() {
        return delegateLock.tryLock();
    }

    public void unlock() {
        delegateLock.unlock();
    }
}