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(); try {return m.size();} finally { rwl.readLock().unlock(); } 61 } 62 public boolean isEmpty(){ 63 rwl.readLock().lock(); try {return m.isEmpty();} finally { rwl.readLock().unlock(); } 64 } 65 66 public Object get(Object key) { 67 rwl.readLock().lock(); try {return m.get(key);} finally { rwl.readLock().unlock(); } 68 } 69 70 public boolean containsKey(Object key) { 71 rwl.readLock().lock(); try {return m.containsKey(key);} finally { rwl.readLock().unlock(); } 72 } 73 public boolean containsValue(Object value){ 74 rwl.readLock().lock(); try {return m.containsValue(value);} finally { rwl.readLock().unlock(); } 75 } 76 77 78 public Set keySet() { // Not implemented 79 return null; 80 } 81 82 public Set entrySet() { // Not implemented 83 return null; 84 } 85 86 public Collection values() { // Not implemented 87 return null; 88 } 89 90 public boolean equals(Object o) { 91 rwl.readLock().lock(); try {return m.equals(o);} finally { rwl.readLock().unlock(); } 92 } 93 public int hashCode() { 94 rwl.readLock().lock(); try {return m.hashCode();} finally { rwl.readLock().unlock(); } 95 } 96 public String toString() { 97 rwl.readLock().lock(); try {return m.toString();} finally { rwl.readLock().unlock(); } 98 } 99 100 101 102 public Object put(Object key, Object value) { 103 rwl.writeLock().lock(); try {return m.put(key, value);} finally { rwl.writeLock().unlock(); } 104 } 105 public Object remove(Object key) { 106 rwl.writeLock().lock(); try {return m.remove(key);} finally { rwl.writeLock().unlock(); } 107 } 108 public void putAll(Map map) { 109 rwl.writeLock().lock(); try {m.putAll(map);} finally { rwl.writeLock().unlock(); } 110 } 111 public void clear() { 112 rwl.writeLock().lock(); try {m.clear();} finally { rwl.writeLock().unlock(); } 113 } 114 115 }