1 28 package org.objectweb.carol.rmi.jrmp.interceptor; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 33 39 public class JContextStore { 40 41 private static int counter = 0; 42 43 public static ArrayList lists = new ArrayList (); 44 45 private static final int MAX = 100; 47 48 private static final int MASK = 256; 50 51 static { 52 int i = 0; 53 while (i != MAX) { 54 lists.add(new ArrayList ()); 55 i++; 56 } 57 58 } 59 60 63 public static int storeObject(Object ob) { 64 if (ob == null) { 67 return -1; 68 } 69 70 int i = 0; 71 ArrayList ar; 72 73 synchronized (lists) { 75 counter++; 76 if (counter == MAX) { 77 counter = 0; 78 } 79 i = counter; 80 } 81 82 ar = (ArrayList ) lists.get(i); 83 84 int j; 86 synchronized (ar) { 87 ar.add(ob); 88 j = ar.size() - 1; 89 } 90 91 i = j * MASK + i; 92 return i; 93 } 94 95 99 100 public static Object getObject(int key) { 101 if (key == -1) { 102 return null; 103 } 104 Object ob; 105 int i = key % MASK; 106 int j = key / MASK; 107 ArrayList ar = (ArrayList ) lists.get(i); 108 109 synchronized (ar) { 110 ob = ar.get(j); 111 ar.set(j, Boolean.FALSE); 112 int k = ar.size() - 1; 113 114 while (k != -1 && (ar.get(k) == Boolean.FALSE)) { 117 ar.remove(k); 118 k--; 119 } 120 } 121 return ob; 122 } 123 124 public String toString() { 125 String result = ""; 126 int x = 0; 127 for (Iterator i = lists.iterator(); i.hasNext();) { 128 int y = 0; 129 ArrayList ara = (ArrayList ) i.next(); 130 for (Iterator j = ara.iterator(); j.hasNext();) { 131 result += "object at x=" + x + ", y=" + y + " object:" + j.next(); 132 y++; 133 } 134 x++; 135 } 136 return result; 137 } 138 } 139 140 | Popular Tags |