KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > config > LockDefinition


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.config;
5
6 import org.apache.commons.lang.builder.ToStringBuilder;
7
8 public class LockDefinition {
9
10   public static final String JavaDoc TC_AUTOLOCK_NAME = "tc:autolock";
11
12   private String JavaDoc lockName = TC_AUTOLOCK_NAME;
13   private ConfigLockLevel lockLevel;
14
15   private boolean isCommitted = false;
16   private boolean autolock;
17
18   public LockDefinition() {
19     return;
20   }
21
22   public LockDefinition(String JavaDoc lockName, ConfigLockLevel lockLevel) {
23     setLockName(lockName);
24     setLockLevel(lockLevel);
25   }
26
27   public String JavaDoc toString() {
28     return ToStringBuilder.reflectionToString(this);
29   }
30
31   public void setLockName(String JavaDoc lockName) {
32     commitWriteCheck();
33     this.lockName = lockName;
34   }
35
36   public String JavaDoc getLockName() {
37     commitReadCheck();
38     return this.lockName;
39   }
40
41   public void setLockLevel(ConfigLockLevel lt) {
42     commitWriteCheck();
43     this.lockLevel = lt;
44   }
45
46   public ConfigLockLevel getLockLevel() {
47     commitReadCheck();
48     return this.lockLevel;
49   }
50
51   public int getLockLevelAsInt() {
52     return getLockLevel().getLevel();
53   }
54
55   public boolean isAutolock() {
56     commitReadCheck();
57     return autolock;
58   }
59
60   /**
61    * Creates the serialized lock name which is the lock name encoded with the lock type.
62    */

63   public synchronized void commit() {
64     if (!isCommitted) {
65       normalizeLockName();
66       this.autolock = TC_AUTOLOCK_NAME.equals(this.lockName);
67     }
68     isCommitted = true;
69   }
70
71   public boolean equals(Object JavaDoc o) {
72     if (o == null) return false;
73     if (!(o instanceof LockDefinition)) return false;
74     LockDefinition compare = (LockDefinition) o;
75     if (lockName == null && compare.lockName != null) return false;
76     if (lockLevel == null && compare.lockLevel != null) return false;
77     return lockName.equals(compare.lockName) && lockLevel.equals(compare.lockLevel);
78   }
79
80   public int hashCode() {
81     if (lockName == null || lockLevel == null) return 1;
82     return lockName.hashCode() | lockLevel.toString().hashCode();
83   }
84
85   private void normalizeLockName() {
86     // Strip all white space.
87
if (this.lockName != null) this.lockName = this.lockName.replaceAll("\\s*", "");
88   }
89
90   private synchronized void commitWriteCheck() {
91     if (isCommitted) throw new IllegalStateException JavaDoc("Attempt to alter the state of LockDefinition: " + this
92                                                      + " after committing");
93   }
94
95   private synchronized void commitReadCheck() {
96     if (!isCommitted) throw new IllegalStateException JavaDoc("Attempt to read an uncommitted LockDefinition: " + this);
97   }
98
99 }
Popular Tags