01: /*
02: File: SyncSet.java
03:
04: Originally written by Doug Lea and released into the public domain.
05: This may be used for any purposes whatsoever without acknowledgment.
06: Thanks for the assistance and support of Sun Microsystems Labs,
07: and everyone contributing, testing, and using this code.
08:
09: History:
10: Date Who What
11: 1Aug1998 dl Create public version
12: */
13:
14: package EDU.oswego.cs.dl.util.concurrent;
15:
16: import java.util.*;
17:
18: /**
19: * SyncSets wrap Sync-based control around java.util.Sets.
20: * They support two additional reader operations than do
21: * SyncCollection: hashCode and equals.
22: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
23: * @see SyncCollection
24: **/
25:
26: public class SyncSet extends SyncCollection implements Set {
27:
28: /**
29: * Create a new SyncSet protecting the given collection,
30: * and using the given sync to control both reader and writer methods.
31: * Common, reasonable choices for the sync argument include
32: * Mutex, ReentrantLock, and Semaphores initialized to 1.
33: **/
34: public SyncSet(Set set, Sync sync) {
35: super (set, sync);
36: }
37:
38: /**
39: * Create a new SyncSet protecting the given set,
40: * and using the given ReadWriteLock to control reader and writer methods.
41: **/
42: public SyncSet(Set set, ReadWriteLock rwl) {
43: super (set, rwl.readLock(), rwl.writeLock());
44: }
45:
46: /**
47: * Create a new SyncSet protecting the given set,
48: * and using the given pair of locks to control reader and writer methods.
49: **/
50: public SyncSet(Set set, Sync readLock, Sync writeLock) {
51: super (set, readLock, writeLock);
52: }
53:
54: public int hashCode() {
55: boolean wasInterrupted = beforeRead();
56: try {
57: return c_.hashCode();
58: } finally {
59: afterRead(wasInterrupted);
60: }
61: }
62:
63: public boolean equals(Object o) {
64: boolean wasInterrupted = beforeRead();
65: try {
66: return c_.equals(o);
67: } finally {
68: afterRead(wasInterrupted);
69: }
70: }
71:
72: }
|