1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.lock.model.impl;
24  
25  import com.liferay.lock.model.Lock;
26  
27  import java.util.Date;
28  
29  /**
30   * <a href="LockImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   *
34   */
35  public class LockImpl implements Lock {
36  
37      public LockImpl(
38          String className, Comparable<?> pk, long companyId, long userId,
39          long expirationTime) {
40  
41          _className = className;
42          _pk = pk;
43          _companyId = companyId;
44          _userId = userId;
45          _expirationTime = expirationTime;
46          _date = new Date();
47      }
48  
49      public String getClassName() {
50          return _className;
51      }
52  
53      public Comparable<?> getPrimaryKey() {
54          return _pk;
55      }
56  
57      public long getCompanyId() {
58          return _companyId;
59      }
60  
61      public long getUserId() {
62          return _userId;
63      }
64  
65      public long getExpirationTime() {
66          return _expirationTime;
67      }
68  
69      public void setExpirationTime(long expirationTime) {
70          _expirationTime = expirationTime;
71          _date = new Date();
72      }
73  
74      public boolean isExpired() {
75          if (_expirationTime <= 0) {
76              return false;
77          }
78          else {
79              Date now = new Date();
80  
81              if (now.getTime() > (_date.getTime() + _expirationTime)) {
82                  return true;
83              }
84              else {
85                  return false;
86              }
87          }
88      }
89  
90      public Date getDate() {
91          return _date;
92      }
93  
94      public int compareTo(Lock lock) {
95          if (lock == null) {
96              return -1;
97          }
98  
99          int value = 0;
100         value = getClassName().compareTo(lock.getClassName());
101 
102         if (value != 0) {
103             return value;
104         }
105 
106         value = ((Comparable<Object>)getPrimaryKey()).compareTo(
107             lock.getPrimaryKey());
108 
109         if (value != 0) {
110             return value;
111         }
112 
113         value = getDate().compareTo(lock.getDate());
114 
115         if (value != 0) {
116             return value;
117         }
118 
119         return 0;
120     }
121 
122     public boolean equals(Lock lock) {
123         if (lock == null) {
124             return false;
125         }
126 
127         if (getClassName().equals(lock.getClassName()) &&
128             getPrimaryKey().equals(lock.getPrimaryKey())) {
129 
130             return true;
131         }
132         else {
133             return false;
134         }
135     }
136 
137     public int hashCode() {
138         return getClassName().hashCode() + getPrimaryKey().hashCode();
139     }
140 
141     private String _className;
142     private Comparable<?> _pk;
143     private long _companyId;
144     private long _userId;
145     private long _expirationTime;
146     private Date _date;
147 
148 }