1 package org.sourceforge.jemm.database.components;
2
3 import java.util.LinkedList;
4
5 import org.sourceforge.jemm.database.ClientThreadId;
6 import org.sourceforge.jemm.types.ID;
7
8 public class UserLockInfo {
9 private final ID id;
10
11 private ClientThreadId lockHolder;
12 private LinkedList<ClientThreadId> waitingQueue;
13
14 public UserLockInfo(ID id) {
15 this(id,null,null);
16 }
17
18 public UserLockInfo(ID id,ClientThreadId lockHolder,LinkedList<ClientThreadId> lockQueue) {
19 this.id = id;
20 this.lockHolder = lockHolder;
21 this.waitingQueue = lockQueue;
22 }
23
24
25
26
27
28
29
30
31 public boolean acquireLock(ClientThreadId clientThreadId) {
32 if(lockHolder == null) {
33 lockHolder = clientThreadId;
34 return true;
35 }
36
37 if(waitingQueue == null)
38 waitingQueue = new LinkedList<ClientThreadId>();
39
40 waitingQueue.add(clientThreadId);
41 return false;
42 }
43
44
45
46
47
48
49
50
51
52 public ClientThreadId releaseLock(ClientThreadId releasingThreadId) {
53 if(lockHolder == null || !lockHolder.equals(releasingThreadId))
54 throw new IllegalStateException("Releasing thread does not hold lock on " + getId());
55
56 if(waitingQueue == null || waitingQueue.isEmpty())
57 lockHolder = null;
58 else
59 lockHolder = waitingQueue.remove();
60
61 return lockHolder;
62 }
63
64
65
66
67
68
69
70
71
72
73 public ClientThreadId releaseOrClearFromQueue(ClientThreadId clientThreadId) {
74 if(lockHolder == clientThreadId)
75 return releaseLock(clientThreadId);
76 else {
77 if(waitingQueue != null)
78 waitingQueue.remove(clientThreadId);
79 return null;
80 }
81 }
82
83
84 public boolean isUnused() {
85 return lockHolder == null;
86 }
87
88 public LinkedList<ClientThreadId> getLockQueue() {
89 return waitingQueue;
90 }
91
92 public ClientThreadId getHolder() {
93 return lockHolder;
94 }
95
96 @Override
97 public int hashCode() {
98 return getId().hashCode();
99 }
100
101 @Override
102 public boolean equals(Object obj) {
103 if (this == obj)
104 return true;
105 if (obj == null)
106 return false;
107 if (getClass() != obj.getClass())
108 return false;
109 UserLockInfo other = (UserLockInfo) obj;
110 return getId().equals(other.getId());
111 }
112
113 public ID getId() {
114 return id;
115 }
116 }