KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > lockmanager > api > LockLevel


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

5 package com.tc.object.lockmanager.api;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 public class LockLevel {
11   // NOTE: The NIL level isn't a valid lock level. It used to indicate the absence of a defined/valid lock level
12
public final static int NIL_LOCK_LEVEL = 0;
13
14   public final static int READ = 1;
15   public final static int WRITE = 2;
16   public final static int CONCURRENT = 4;
17
18   private final static int GREEDY = 0x80;
19   private final static int SYNCHRONOUS = 0X40;
20
21   public final static int SYNCHRONOUS_WRITE = WRITE | SYNCHRONOUS;
22
23   private LockLevel() {
24     // not to be instantiated
25
}
26
27   public static boolean isRead(int level) {
28     if (level <= 0) return false;
29     return (level & READ) == READ;
30   }
31
32   public static boolean isWrite(int level) {
33     if (level <= 0) return false;
34     return (level & WRITE) == WRITE;
35   }
36
37   public static boolean isConcurrent(int level) {
38     if (level <= 0) return false;
39     return (level & CONCURRENT) == CONCURRENT;
40   }
41
42   public static boolean isSynchronousWrite(int level) {
43     if (level <= 0) return false;
44     return (level & SYNCHRONOUS_WRITE) == SYNCHRONOUS_WRITE;
45   }
46
47   public static boolean isGreedy(int level) {
48     if (level <= 0) return false;
49     return (level & GREEDY) == GREEDY;
50   }
51
52   public static boolean isSynchronous(int level) {
53     if (level <= 0) return false;
54     return (level & SYNCHRONOUS) == SYNCHRONOUS;
55   }
56
57   public static String JavaDoc toString(int level) {
58     ArrayList JavaDoc levels = new ArrayList JavaDoc();
59     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
60
61     if (isRead(level)) {
62       levels.add("READ");
63     }
64
65     if (isWrite(level)) {
66       levels.add("WRITE");
67     }
68
69     if (isConcurrent(level)) {
70       levels.add("CONCURRENT");
71     }
72
73     if (isSynchronousWrite(level)) {
74       levels.add("SYNCHRONOUS_WRITE");
75     }
76
77     if (levels.size() == 0) {
78       levels.add("UNKNOWN:" + level);
79     }
80
81     for (Iterator JavaDoc iter = levels.iterator(); iter.hasNext();) {
82       rv.append(iter.next()).append(' ');
83     }
84
85     rv.append('(').append(level).append(')');
86
87     return rv.toString();
88
89   }
90
91   /**
92    * Is this a discrete lock level? A lock level which is a combination (like READ+WRITE) is non-discreet
93    */

94   public static boolean isDiscrete(int lockLevel) {
95     switch (lockLevel) {
96       case READ:
97       case WRITE:
98       case CONCURRENT:
99         return true;
100       default:
101         return false;
102     }
103   }
104
105   public static int makeGreedy(int level) {
106     return level | GREEDY;
107   }
108
109   public static int makeNotGreedy(int level) {
110     return level & (~GREEDY);
111   }
112
113   public static int makeSynchronous(int level) {
114     return level | SYNCHRONOUS;
115   }
116   
117   public static int makeNotSynchronous(int level) {
118     return level & (~SYNCHRONOUS);
119   }
120 }
121
Popular Tags