1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/***************************************************************************
* Copyright (c) 2004, 2005, 2006 Eike Stepper, Germany.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eike Stepper - initial API and implementation
**************************************************************************/
package org.eclipse.net4j.util.registry;
import org.eclipse.net4j.util.registry.IRegistry.Listener.EventType;
import org.eclipse.net4j.util.registry.IRegistryElement.Descriptor;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
/**
* TODO Check if all methods of DelegatingRegistry still do what they should.
* TODO Remove DelegatingRegistry?
*
* @author Eike Stepper
*/
public abstract class AbstractCachingRegistry<ID, E extends IRegistryElement<ID>> extends
DelegatingRegistry<ID, E>
{
public AbstractCachingRegistry(IRegistry<ID, E> delegate)
{
this(delegate, DEFAULT_RESOLVING);
}
public AbstractCachingRegistry(IRegistry<ID, E> delegate, boolean resolving)
{
super(delegate, resolving);
}
@Override
public synchronized void register(E element)
{
ID id = element.getID();
E delegatedElement = super.lookup(id, false);
E oldElement = getCache().put(id, element);
if (oldElement == null)
{
if (delegatedElement != null)
{
// Unhidden delegated element now becomes hidden
fireElementDeregistering(delegatedElement);
}
}
else
{
fireElementDeregistering(oldElement);
oldElement.dispose();
}
fireElementRegistered(element);
}
@Override
public synchronized void deregister(ID id)
{
E delegatedElement = super.lookup(id, false);
E element = getCache().remove(id);
if (element != null)
{
fireElementDeregistering(element);
element.dispose();
if (delegatedElement != null)
{
// Hidden delegated element now becomes unhidden
fireElementRegistered(delegatedElement);
}
}
}
/**
* Synchronized to support {@link #resolveElement(IRegistryElement)}
*/
@Override
public synchronized E lookup(ID id, boolean resolve)
{
E element = getCache().get(id);
if (element == null)
{
if (resolve)
{
return resolveDelegatedElement(id);
}
else
{
return super.lookup(id, false);
}
}
if (resolve)
{
element = resolveElement(element);
}
return element;
}
@Override
public Set<ID> getElementIDs()
{
Set<ID> ids = new HashSet();
ids.addAll(super.getElementIDs());
ids.addAll(getCache().keySet());
return ids;
}
@Override
public synchronized void dispose()
{
for (E element : getCache().values())
{
fireElementDeregistering(element);
element.dispose();
}
getCache().clear();
super.dispose();
}
@Override
protected void replaceElement(ID id, E element)
{
getCache().put(id, element);
}
@Override
protected synchronized void handleDelegateEvent(EventType eventType, E element)
{
if (!getCache().containsKey(element.getID()))
{
fireRegistryEvent(eventType, element);
}
}
protected E resolveDelegatedElement(ID id)
{
synchronized (getDelegate())
{
boolean wasUnresolved = super.lookup(id, false) instanceof Descriptor;
E e = super.lookup(id, true);
if (wasUnresolved)
{
fireElementResolved(e);
}
return e;
}
}
protected abstract Map<ID, E> getCache();
}
|