01: ///////////////////////////////////////////////////////////////////////////////
02: //
03: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
04: //
05: // All Rights Reserved
06: //
07: // This program is free software; you can redistribute it and/or modify
08: // it under the terms of the GNU General Public License and GNU Library
09: // General Public License as published by the Free Software Foundation;
10: // either version 2, or (at your option) any later version.
11: //
12: // This program is distributed in the hope that it will be useful,
13: // but WITHOUT ANY WARRANTY; without even the implied warranty of
14: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: // GNU General Public License and GNU Library General Public License
16: // for more details.
17: //
18: // You should have received a copy of the GNU General Public License
19: // and GNU Library General Public License along with this program; if
20: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21: // MA 02139, USA.
22: //
23: ///////////////////////////////////////////////////////////////////////////////
24: package org.myoodb.util;
25:
26: public class LockSemaphore {
27: private java.util.concurrent.locks.ReentrantLock m_lock;
28:
29: public LockSemaphore() {
30: m_lock = new java.util.concurrent.locks.ReentrantLock();
31: }
32:
33: public void lock() {
34: m_lock.lock();
35: }
36:
37: public void unlock() {
38: m_lock.unlock();
39: }
40:
41: public void tryLock() {
42: m_lock.tryLock();
43: }
44:
45: public void tryLock(long timeout)
46: throws java.lang.InterruptedException {
47: m_lock.tryLock(timeout,
48: java.util.concurrent.TimeUnit.MILLISECONDS);
49: }
50:
51: public boolean isLocked() {
52: return m_lock.isLocked();
53: }
54:
55: public int hashCode() {
56: return toString().hashCode();
57: }
58:
59: public boolean equals(Object obj) {
60: if (this == obj) {
61: return true;
62: } else if (obj instanceof String) {
63: return toString().equals(obj);
64: } else if (obj instanceof LockSemaphore) {
65: return toString().equals(((LockSemaphore) obj).toString());
66: } else {
67: return false;
68: }
69: }
70:
71: public int compareTo(Object obj) {
72: if (obj instanceof String) {
73: return toString().compareTo((String) obj);
74: } else if (obj instanceof LockSemaphore) {
75: return toString().compareTo(
76: ((LockSemaphore) obj).toString());
77: } else {
78: return -1;
79: }
80: }
81:
82: public String toString() {
83: return m_lock.toString();
84: }
85: }
|