001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.object.config;
006:
007: import org.apache.commons.lang.builder.ToStringBuilder;
008:
009: public class LockDefinitionImpl implements LockDefinition {
010:
011: private String lockName = TC_AUTOLOCK_NAME;
012: private ConfigLockLevel lockLevel;
013:
014: private boolean isCommitted = false;
015: private boolean autolock;
016:
017: public LockDefinitionImpl() {
018: return;
019: }
020:
021: public LockDefinitionImpl(String lockName, ConfigLockLevel lockLevel) {
022: setLockName(lockName);
023: setLockLevel(lockLevel);
024: }
025:
026: public String toString() {
027: return ToStringBuilder.reflectionToString(this );
028: }
029:
030: public void setLockName(String lockName) {
031: commitWriteCheck();
032: this .lockName = lockName;
033: }
034:
035: public String getLockName() {
036: commitReadCheck();
037: return this .lockName;
038: }
039:
040: public void setLockLevel(ConfigLockLevel lt) {
041: commitWriteCheck();
042: this .lockLevel = lt;
043: }
044:
045: public ConfigLockLevel getLockLevel() {
046: commitReadCheck();
047: return this .lockLevel;
048: }
049:
050: public int getLockLevelAsInt() {
051: return getLockLevel().getLevel();
052: }
053:
054: public boolean isAutolock() {
055: commitReadCheck();
056: return autolock;
057: }
058:
059: /**
060: * Creates the serialized lock name which is the lock name encoded with the lock type.
061: */
062: public synchronized void commit() {
063: if (!isCommitted) {
064: normalizeLockName();
065: this .autolock = TC_AUTOLOCK_NAME.equals(this .lockName);
066: }
067: isCommitted = true;
068: }
069:
070: public boolean equals(Object o) {
071: if (o == null) {
072: return false;
073: }
074:
075: if (!(o instanceof LockDefinitionImpl)) {
076: return false;
077: }
078:
079: LockDefinitionImpl compare = (LockDefinitionImpl) o;
080: if (lockName == null && compare.lockName != null) {
081: return false;
082: }
083:
084: if (lockLevel == null && compare.lockLevel != null) {
085: return false;
086: }
087:
088: return lockName.equals(compare.lockName)
089: && lockLevel.equals(compare.lockLevel);
090: }
091:
092: public int hashCode() {
093: if (lockName == null || lockLevel == null) {
094: return 1;
095: }
096: return lockName.hashCode() | lockLevel.toString().hashCode();
097: }
098:
099: private void normalizeLockName() {
100: // Strip all white space.
101: if (this .lockName != null) {
102: this .lockName = this .lockName.replaceAll("\\s*", "");
103: }
104: }
105:
106: private synchronized void commitWriteCheck() {
107: if (isCommitted) {
108: throw new IllegalStateException(
109: "Attempt to alter the state of LockDefinition: "
110: + this + " after committing");
111: }
112: }
113:
114: private synchronized void commitReadCheck() {
115: if (!isCommitted) {
116: throw new IllegalStateException(
117: "Attempt to read an uncommitted LockDefinition: "
118: + this);
119: }
120: }
121:
122: }
|