blob: e81c676c6facfd500c58f6a8b5e266a31a78875a [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
19 * CA 95054 USA or visit www.sun.com if you need additional information or
20 * have any 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 */
33import java.util.*;
34import java.util.concurrent.*;
35import java.util.concurrent.locks.*;
36
37
38/**
39 * This is an incomplete implementation of a wrapper class
40 * that places read-write locks around unsynchronized Maps.
41 * Exists as a sample input for MapLoops test.
42 */
43
44public class RWMap implements Map {
45 private final Map m;
46 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
47
48 public RWMap(Map m) {
49 if (m == null)
50 throw new NullPointerException();
51 this.m = m;
52 }
53
54 public RWMap() {
55 this(new TreeMap()); // use TreeMap by default
56 }
57
58 public int size() {
59 rwl.readLock().lock(); try {return m.size();} finally { rwl.readLock().unlock(); }
60 }
61 public boolean isEmpty(){
62 rwl.readLock().lock(); try {return m.isEmpty();} finally { rwl.readLock().unlock(); }
63 }
64
65 public Object get(Object key) {
66 rwl.readLock().lock(); try {return m.get(key);} finally { rwl.readLock().unlock(); }
67 }
68
69 public boolean containsKey(Object key) {
70 rwl.readLock().lock(); try {return m.containsKey(key);} finally { rwl.readLock().unlock(); }
71 }
72 public boolean containsValue(Object value){
73 rwl.readLock().lock(); try {return m.containsValue(value);} finally { rwl.readLock().unlock(); }
74 }
75
76
77 public Set keySet() { // Not implemented
78 return null;
79 }
80
81 public Set entrySet() { // Not implemented
82 return null;
83 }
84
85 public Collection values() { // Not implemented
86 return null;
87 }
88
89 public boolean equals(Object o) {
90 rwl.readLock().lock(); try {return m.equals(o);} finally { rwl.readLock().unlock(); }
91 }
92 public int hashCode() {
93 rwl.readLock().lock(); try {return m.hashCode();} finally { rwl.readLock().unlock(); }
94 }
95 public String toString() {
96 rwl.readLock().lock(); try {return m.toString();} finally { rwl.readLock().unlock(); }
97 }
98
99
100
101 public Object put(Object key, Object value) {
102 rwl.writeLock().lock(); try {return m.put(key, value);} finally { rwl.writeLock().unlock(); }
103 }
104 public Object remove(Object key) {
105 rwl.writeLock().lock(); try {return m.remove(key);} finally { rwl.writeLock().unlock(); }
106 }
107 public void putAll(Map map) {
108 rwl.writeLock().lock(); try {m.putAll(map);} finally { rwl.writeLock().unlock(); }
109 }
110 public void clear() {
111 rwl.writeLock().lock(); try {m.clear();} finally { rwl.writeLock().unlock(); }
112 }
113
114}