1 5 package com.tc.object.lockmanager.api; 6 7 import java.util.ArrayList ; 8 import java.util.Iterator ; 9 10 public class LockLevel { 11 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 } 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 toString(int level) { 58 ArrayList levels = new ArrayList (); 59 StringBuffer rv = new StringBuffer (); 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 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 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 |