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.lockmanager.api;
006:
007: import java.util.ArrayList;
008: import java.util.Iterator;
009:
010: /**
011: * Lock level constants
012: */
013: public class LockLevel {
014: /** NOTE: The NIL level isn't a valid lock level. It used to indicate the absence of a defined/valid lock level */
015: public final static int NIL_LOCK_LEVEL = 0;
016:
017: /** READ bit */
018: public final static int READ = 1;
019: /** WRITE bit */
020: public final static int WRITE = 2;
021: /** CONCURRENT bit */
022: public final static int CONCURRENT = 4;
023:
024: /** GREEDY bit */
025: private final static int GREEDY = 0x80;
026: /** SYNCHRONOUS bit, used with WRITE as SYNCHRONOUS_WRITE */
027: private final static int SYNCHRONOUS = 0X40;
028:
029: /** Combination of WRITE with SYNCHRONOUS flag */
030: public final static int SYNCHRONOUS_WRITE = WRITE | SYNCHRONOUS;
031:
032: private LockLevel() {
033: // not to be instantiated
034: }
035:
036: /**
037: * @param level Level
038: * @return Is lock level READ?
039: */
040: public static boolean isRead(int level) {
041: if (level <= 0)
042: return false;
043: return (level & READ) == READ;
044: }
045:
046: /**
047: * @param level Level
048: * @return Is lock level WRITE?
049: */
050: public static boolean isWrite(int level) {
051: if (level <= 0)
052: return false;
053: return (level & WRITE) == WRITE;
054: }
055:
056: /**
057: * @param level Level
058: * @return Is lock level CONCURRENT?
059: */
060: public static boolean isConcurrent(int level) {
061: if (level <= 0)
062: return false;
063: return (level & CONCURRENT) == CONCURRENT;
064: }
065:
066: /**
067: * @param level Level
068: * @return Is lock level SYNCHRONOUS_WRITE?
069: */
070: public static boolean isSynchronousWrite(int level) {
071: if (level <= 0)
072: return false;
073: return (level & SYNCHRONOUS_WRITE) == SYNCHRONOUS_WRITE;
074: }
075:
076: /**
077: * @param level Level
078: * @return Is lock level GREEDY?
079: */
080: public static boolean isGreedy(int level) {
081: if (level <= 0)
082: return false;
083: return (level & GREEDY) == GREEDY;
084: }
085:
086: /**
087: * @param level Level
088: * @return Is SYNCHRONOUS?
089: */
090: public static boolean isSynchronous(int level) {
091: if (level <= 0)
092: return false;
093: return (level & SYNCHRONOUS) == SYNCHRONOUS;
094: }
095:
096: /**
097: * @param level Lock level
098: * @return String representation
099: */
100: public static String toString(int level) {
101: ArrayList levels = new ArrayList();
102: StringBuffer rv = new StringBuffer();
103:
104: if (isRead(level)) {
105: levels.add("READ");
106: }
107:
108: if (isWrite(level)) {
109: levels.add("WRITE");
110: }
111:
112: if (isConcurrent(level)) {
113: levels.add("CONCURRENT");
114: }
115:
116: if (isSynchronousWrite(level)) {
117: levels.add("SYNCHRONOUS_WRITE");
118: }
119:
120: if (levels.size() == 0) {
121: levels.add("UNKNOWN:" + level);
122: }
123:
124: for (Iterator iter = levels.iterator(); iter.hasNext();) {
125: rv.append(iter.next()).append(' ');
126: }
127:
128: rv.append('(').append(level).append(')');
129:
130: return rv.toString();
131:
132: }
133:
134: /**
135: * Is this a discrete lock level? A lock level which is a combination (like READ+WRITE) is non-discreet
136: * @return True if discrete
137: */
138: public static boolean isDiscrete(int lockLevel) {
139: switch (lockLevel) {
140: case READ:
141: case WRITE:
142: case CONCURRENT:
143: return true;
144: default:
145: return false;
146: }
147: }
148:
149: /**
150: * Make lock greedy
151: * @param level Start
152: * @return Greedy version of level
153: */
154: public static int makeGreedy(int level) {
155: return level | GREEDY;
156: }
157:
158: /**
159: * Make lock not greedy
160: * @param level Start
161: * @return Not greedy version of level
162: */
163: public static int makeNotGreedy(int level) {
164: return level & (~GREEDY);
165: }
166:
167: /**
168: * Make lock synchronous
169: * @param level Start
170: * @return Synchronous version of level
171: */
172: public static int makeSynchronous(int level) {
173: return level | SYNCHRONOUS;
174: }
175:
176: /**
177: * Make not synchronous
178: * @param level Start
179: * @return Not synchronous version of level
180: */
181: public static int makeNotSynchronous(int level) {
182: return level & (~SYNCHRONOUS);
183: }
184: }
|