01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.lock;
05:
06: import java.util.ArrayList;
07: import java.util.Iterator;
08:
09: public class LockLevel {
10: // NOTE: The NIL level isn't a valid lock level. It used to indicate the absence of a defined/valid lock level
11: public final static int NIL_LOCK_LEVEL = 0;
12:
13: public final static int READ = 1;
14: public final static int WRITE = 2;
15: public final static int CONCURRENT = 4;
16:
17: private final static int GREEDY = 0x80;
18:
19: private LockLevel() {
20: // not to be instantiated
21: }
22:
23: public static boolean isWrite(int level) {
24: if (level <= 0)
25: return false;
26: return (level & WRITE) == WRITE;
27: }
28:
29: public static boolean isRead(int level) {
30: if (level <= 0)
31: return false;
32: return (level & READ) == READ;
33: }
34:
35: public static boolean isConcurrent(int level) {
36: if (level <= 0)
37: return false;
38: return (level & CONCURRENT) == CONCURRENT;
39: }
40:
41: public static boolean isGreedy(int level) {
42: if (level <= 0)
43: return false;
44: return (level & GREEDY) == GREEDY;
45: }
46:
47: public static String toString(int level) {
48: ArrayList levels = new ArrayList();
49: StringBuffer rv = new StringBuffer();
50:
51: if (isRead(level)) {
52: levels.add("READ");
53: }
54:
55: if (isWrite(level)) {
56: levels.add("WRITE");
57: }
58:
59: if (isConcurrent(level)) {
60: levels.add("CONCURRENT");
61: }
62:
63: if (levels.size() == 0) {
64: levels.add("UNKNOWN:" + level);
65: }
66:
67: for (Iterator iter = levels.iterator(); iter.hasNext();) {
68: rv.append(iter.next()).append(' ');
69: }
70:
71: rv.append('(').append(level).append(')');
72:
73: return rv.toString();
74:
75: }
76:
77: /**
78: * Is this a discrete lock level? A lock level which is a combination (like READ+WRITE) is non-discreet
79: */
80: public static boolean isDiscrete(int lockLevel) {
81: switch (lockLevel) {
82: case WRITE:
83: case READ:
84: case CONCURRENT:
85: return true;
86: default:
87: return false;
88: }
89: }
90:
91: public static int makeGreedy(int level) {
92: return level | GREEDY;
93: }
94:
95: public static int makeNotGreedy(int level) {
96: return level ^ GREEDY;
97: }
98: }
|