Clover icon

Magnolia Module Memcached 5.4.2

  1. Project Clover database Wed Aug 19 2015 15:04:32 CEST
  2. Package info.magnolia.cache.concurrent

File ReadWriteLockSync.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
16% of files have more coverage

Code metrics

0
19
7
1
100
45
11
0.58
2.71
7
1.57
7.1% of code in this file is excluded from these metrics.

Classes

Class Line # Actions
ReadWriteLockSync 28 19 7.1% 11 6
0.769230876.9%
 

Contributing tests

This file is covered by 10 tests. .

Source view

1    /**
2    * Copyright Terracotta, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package info.magnolia.cache.concurrent;
17   
18    import java.util.concurrent.locks.ReentrantReadWriteLock;
19    import java.util.concurrent.locks.Lock;
20    import java.util.concurrent.TimeUnit;
21    import java.util.concurrent.locks.ReadWriteLock;
22   
23    /**
24    * A simple ReadWriteLock synchronizer.
25    *
26    * @author Alex Snaps
27    */
 
28    public class ReadWriteLockSync {
29   
30    private final ReentrantReadWriteLock rrwl;
31   
32    /**
33    * default constructor.
34    */
 
35  36864 toggle public ReadWriteLockSync() {
36  36864 this(new ReentrantReadWriteLock());
37    }
38   
39    /**
40    * Constructor.
41    * @param lock
42    */
 
43  36864 toggle public ReadWriteLockSync(ReentrantReadWriteLock lock) {
44  36864 this.rrwl = lock;
45    }
46    /**
47    * {@inheritDoc}
48    */
 
49  10 toggle public void lock(final LockType type) {
50  10 getLock(type).lock();
51    }
52   
53    /**
54    * {@inheritDoc}
55    */
 
56  34 toggle public boolean tryLock(final LockType type, final long msec) throws InterruptedException {
57  34 return getLock(type).tryLock(msec, TimeUnit.MILLISECONDS);
58    }
59   
60    /**
61    * {@inheritDoc}
62    */
 
63  43 toggle public void unlock(final LockType type) {
64  43 getLock(type).unlock();
65    }
66   
 
67  87 toggle private Lock getLock(final LockType type) {
68  87 switch (type) {
69  57 case READ:
70  57 return rrwl.readLock();
71  30 case WRITE:
72  30 return rrwl.writeLock();
73  0 default:
74  0 throw new IllegalArgumentException("We don't support any other lock type than READ or WRITE!");
75    }
76    }
77   
78    /**
79    * Gets the {@code ReadWriteLock} backing this sync.
80    *
81    * @return the backing {@code ReadWriteLock}
82    */
 
83    toggle public ReadWriteLock getReadWriteLock() {
84    return rrwl;
85    }
86   
87    /**
88    * {@inheritDoc}
89    */
 
90  15 toggle public boolean isHeldByCurrentThread(LockType type) {
91  15 switch (type) {
92  0 case READ:
93  0 throw new UnsupportedOperationException("Querying of read lock is not supported.");
94  15 case WRITE:
95  15 return rrwl.isWriteLockedByCurrentThread();
96  0 default:
97  0 throw new IllegalArgumentException("We don't support any other lock type than READ or WRITE!");
98    }
99    }
100    }