1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  */
  22 
  23 /*
  24  * This file is available under and governed by the GNU General Public
  25  * License version 2 only, as published by the Free Software Foundation.
  26  * However, the following notice accompanied the original version of this
  27  * file:
  28  *
  29  * Written by Doug Lea with assistance from members of JCP JSR-166
  30  * Expert Group and released to the public domain, as explained at
  31  * http://creativecommons.org/licenses/publicdomain
  32  */
  33 
  34 import java.util.*;
  35 import java.util.concurrent.*;
  36 import java.util.concurrent.locks.*;
  37 
  38 
  39 /**
  40  * This is an incomplete implementation of a wrapper class
  41  * that places read-write locks around unsynchronized Maps.
  42  * Exists as a sample input for MapLoops test.
  43  */
  44 
  45 public class RWMap implements Map {
  46     private final Map m;
  47     private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
  48 
  49     public RWMap(Map m) {
  50         if (m == null)
  51             throw new NullPointerException();
  52         this.m = m;
  53     }
  54 
  55     public RWMap() {
  56         this(new TreeMap()); // use TreeMap by default
  57     }
  58 
  59     public int size() {
  60         rwl.readLock().lock();
  61         try { return m.size(); }
  62         finally { rwl.readLock().unlock(); }
  63     }
  64 
  65     public boolean isEmpty() {
  66         rwl.readLock().lock();
  67         try { return m.isEmpty(); }
  68         finally { rwl.readLock().unlock(); }
  69     }
  70 
  71     public Object get(Object key) {
  72         rwl.readLock().lock();
  73         try { return m.get(key); }
  74         finally { rwl.readLock().unlock(); }
  75     }
  76 
  77     public boolean containsKey(Object key) {
  78         rwl.readLock().lock();
  79         try { return m.containsKey(key); }
  80         finally { rwl.readLock().unlock(); }
  81     }
  82 
  83     public boolean containsValue(Object value) {
  84         rwl.readLock().lock();
  85         try { return m.containsValue(value); }
  86         finally { rwl.readLock().unlock(); }
  87     }
  88 
  89 
  90     public Set keySet() { // Not implemented
  91         return null;
  92     }
  93 
  94     public Set entrySet() { // Not implemented
  95         return null;
  96     }
  97 
  98     public Collection values() { // Not implemented
  99         return null;
 100     }
 101 
 102     public boolean equals(Object o) {
 103         rwl.readLock().lock();
 104         try { return m.equals(o); }
 105         finally { rwl.readLock().unlock(); }
 106     }
 107 
 108     public int hashCode() {
 109         rwl.readLock().lock();
 110         try { return m.hashCode(); }
 111         finally { rwl.readLock().unlock(); }
 112     }
 113 
 114     public String toString() {
 115         rwl.readLock().lock();
 116         try { return m.toString(); }
 117         finally { rwl.readLock().unlock(); }
 118     }
 119 


 120     public Object put(Object key, Object value) {
 121         rwl.writeLock().lock();
 122         try { return m.put(key, value); }
 123         finally { rwl.writeLock().unlock(); }
 124     }
 125 
 126     public Object remove(Object key) {
 127         rwl.writeLock().lock();
 128         try { return m.remove(key); }
 129         finally { rwl.writeLock().unlock(); }
 130     }
 131 
 132     public void putAll(Map map) {
 133         rwl.writeLock().lock();
 134         try { m.putAll(map); }
 135         finally { rwl.writeLock().unlock(); }
 136     }
 137 
 138     public void clear() {
 139         rwl.writeLock().lock();
 140         try { m.clear(); }
 141         finally { rwl.writeLock().unlock(); }
 142     }
 143 
 144 }
--- EOF ---