38
38
package sun.applet;
42
public class PluginObjectStore {
43
private static HashMap<Integer, Object> objects = new HashMap<Integer, Object>();
44
private static HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
45
private static HashMap<Object, Integer> identifiers = new HashMap<Object, Integer>();
48
// IF uniqueID == MAX_LONG, uniqueID =
49
// 0 && wrapped = true
51
// if (wrapped), check if
52
// objects.get(uniqueID) returns null
54
// if yes, use uniqueID, if no,
55
// uniqueID++ and keep checking
57
// stack of available ids:
58
// derefed id -> derefed id -> nextUniqueIdentifier
59
private static int nextUniqueIdentifier = 1;
40
import java.util.HashMap;
43
// Enums are the best way to implement singletons:
44
// Bloch, Joshua. Effective Java, 2nd Edition. Item 3, Chapter 2. ISBN: 0-321-35668-3.
45
enum PluginObjectStore {
48
private HashMap<Integer, Object> objects = new HashMap<Integer, Object>();
49
private HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
50
private HashMap<Object, Integer> identifiers = new HashMap<Object, Integer>();
51
private final Object lock = new Object();
53
private boolean wrapped = false;
54
private int nextUniqueIdentifier = 1;
56
public static PluginObjectStore getInstance() {
61
60
public Object getObject(Integer identifier) {
62
return objects.get(identifier);
62
return objects.get(identifier);
65
66
public Integer getIdentifier(Object object) {
68
return identifiers.get(object);
70
return identifiers.get(object);
71
74
public boolean contains(Object object) {
73
return identifiers.containsKey(object);
77
return identifiers.containsKey(object);
78
83
public boolean contains(int identifier) {
79
return objects.containsKey(identifier);
85
return objects.containsKey(identifier);
89
private boolean checkNeg() {
90
if (nextUniqueIdentifier < 1) {
92
nextUniqueIdentifier = 1;
97
private int getNextID() {
98
while (checkNeg() && objects.containsKey(nextUniqueIdentifier))
99
nextUniqueIdentifier++;
100
return nextUniqueIdentifier++;
82
103
public void reference(Object object) {
83
Integer identifier = identifiers.get(object);
84
if (identifier == null) {
85
objects.put(nextUniqueIdentifier, object);
86
counts.put(nextUniqueIdentifier, 1);
87
identifiers.put(object, nextUniqueIdentifier);
88
nextUniqueIdentifier++;
90
counts.put(identifier, counts.get(identifier) + 1);
105
Integer identifier = identifiers.get(object);
106
if (identifier == null) {
107
int next = getNextID();
108
objects.put(next, object);
110
identifiers.put(object, next);
112
counts.put(identifier, counts.get(identifier) + 1);
94
117
public void unreference(int identifier) {
95
Integer currentCount = counts.get(identifier);
96
if (currentCount == null) {
99
if (currentCount == 1) {
100
Object object = objects.get(identifier);
101
objects.remove(identifier);
102
counts.remove(identifier);
103
identifiers.remove(object);
105
counts.put(identifier, currentCount - 1);
119
Integer currentCount = counts.get(identifier);
120
if (currentCount == null) {
123
if (currentCount == 1) {
124
Object object = objects.get(identifier);
125
objects.remove(identifier);
126
counts.remove(identifier);
127
identifiers.remove(object);
129
counts.put(identifier, currentCount - 1);
109
134
public void dump() {
110
Iterator i = objects.keySet().iterator();
111
while (i.hasNext()) {
112
Object key = i.next();
113
PluginDebug.debug(key + "::" + objects.get(key));
136
if (PluginDebug.DEBUG) {
137
for (Map.Entry<Integer, Object> e : objects.entrySet()) {
138
PluginDebug.debug(e.getKey(), "::", e.getValue());