1 28 package org.objectweb.carol.rmi.jrmp.server; 29 30 import java.util.ArrayList ; 31 32 39 public class JLocalObjectStore { 40 41 private static int counter = 0; 42 43 private static final int MAX = 100; 45 46 private static final int MASK = 255; 48 49 private static final int MASK_SIZE = 8; 50 51 private static final Object EMPTY_SLOT = Boolean.FALSE; 52 53 public static ArrayList [] lists = new ArrayList [MAX]; 54 55 static { 56 for (int i = 0; i < MAX; i++) { 57 lists[i] = new ArrayList (); 58 } 59 60 } 61 62 65 public static int storeObject(Object ob) { 66 if (ob == null) { 69 return -1; 70 } 71 72 int i = 0; 74 synchronized (lists) { 75 counter++; 76 if (counter == MAX) { 77 counter = 0; 78 } 79 i = counter; 80 } 81 ArrayList ar = lists[i]; 82 83 int j; 84 synchronized (ar) { 85 j = ar.indexOf(EMPTY_SLOT); 86 if (j == -1) { 87 j = ar.size(); 89 ar.add(ob); 90 } else { 91 ar.set(j, ob); 94 } 95 } 96 return i + (j << MASK_SIZE); 97 } 98 99 105 public static Object getObject(int key) { 106 if (key == -1) { 107 return null; 108 } 109 int j = key >> MASK_SIZE; 110 ArrayList ar = lists[key & MASK]; 111 try { 112 return ar.get(j); 115 } catch (RuntimeException e) { 116 synchronized (ar) { 120 return (ar.size() > j ? ar.get(j) : null); 121 } 122 } 123 } 124 125 129 public static Object removeObject(int key) { 130 if (key < 0) { 131 return null; 132 } 133 Object ob; 134 ArrayList ar = lists[key & MASK]; 135 int j = key >> MASK_SIZE; 136 synchronized (ar) { 137 ob = ar.get(j); 138 ar.set(j, EMPTY_SLOT); 139 int k = ar.size() - 1; 140 141 while (k != -1 && (ar.get(k) == EMPTY_SLOT)) { 143 ar.remove(k); 144 k--; 145 } 146 } 147 return ob; 148 } 149 } | Popular Tags |