KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > locking > FileLock


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.locking;
18
19 /**
20  * File Lock Class
21  * <p>
22  * Contains the details of a single file lock.
23  */

24 public class FileLock
25 {
26
27     // Constants
28

29     public static final long LockWholeFile = 0xFFFFFFFFFFFFFFFFL;
30
31     // Start lock offset and length
32

33     private long m_offset;
34     private long m_length;
35
36     // Owner process id
37

38     private int m_pid;
39
40     /**
41      * Class constructor
42      *
43      * @param offset long
44      * @param len long
45      * @param pid int
46      */

47     public FileLock(long offset, long len, int pid)
48     {
49         setOffset(offset);
50         setLength(len);
51         setProcessId(pid);
52     }
53
54     /**
55      * Get the starting lock offset
56      *
57      * @return long Starting lock offset.
58      */

59     public final long getOffset()
60     {
61         return m_offset;
62     }
63
64     /**
65      * Set the starting lock offset.
66      *
67      * @param long Starting lock offset
68      */

69     public final void setOffset(long offset)
70     {
71         m_offset = offset;
72     }
73
74     /**
75      * Get the locked section length
76      *
77      * @return long Locked section length
78      */

79     public final long getLength()
80     {
81         return m_length;
82     }
83
84     /**
85      * Set the locked section length
86      *
87      * @param long Locked section length
88      */

89     public final void setLength(long len)
90     {
91         if (len < 0)
92             m_length = LockWholeFile;
93         else
94             m_length = len;
95     }
96
97     /**
98      * Get the owner process id for the lock
99      *
100      * @return int
101      */

102     public final int getProcessId()
103     {
104         return m_pid;
105     }
106
107     /**
108      * Deterine if the lock is locking the whole file
109      *
110      * @return boolean
111      */

112     public final boolean isWholeFile()
113     {
114         return m_length == LockWholeFile ? true : false;
115     }
116
117     /**
118      * Set the process id of the owner of this lock
119      *
120      * @param pid int
121      */

122     public final void setProcessId(int pid)
123     {
124         m_pid = pid;
125     }
126
127     /**
128      * Check if the specified locks byte range overlaps this locks byte range.
129      *
130      * @param lock FileLock
131      */

132     public final boolean hasOverlap(FileLock lock)
133     {
134         return hasOverlap(lock.getOffset(), lock.getLength());
135     }
136
137     /**
138      * Check if the specified locks byte range overlaps this locks byte range.
139      *
140      * @param offset long
141      * @param len long
142      */

143     public final boolean hasOverlap(long offset, long len)
144     {
145
146         // Check if the lock is for the whole file
147

148         if (isWholeFile())
149             return true;
150
151         // Check if the locks overlap
152

153         long endOff = getOffset() + getLength();
154
155         if (getOffset() < offset && endOff < offset)
156             return false;
157
158         endOff = offset + len;
159
160         if (getOffset() > endOff)
161             return false;
162
163         // Locks overlap
164

165         return true;
166     }
167
168     /**
169      * Return the lock details as a string
170      *
171      * @return String
172      */

173     public String JavaDoc toString()
174     {
175         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
176
177         str.append("[PID=");
178         str.append(getProcessId());
179         str.append(",Offset=");
180         str.append(getOffset());
181         str.append(",Len=");
182         str.append(getLength());
183         str.append("]");
184
185         return str.toString();
186     }
187 }
Popular Tags