summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2006-12-22 03:37:46 (EST)
committerEike Stepper2006-12-22 03:37:46 (EST)
commit628b660a146fea7df9679e9e217aeeed301a6bf1 (patch)
treeeeb6d52ee636e375535d072b2e2737007ab95b6c
parentd0682c7d3f294c325b4f5db7c276b52c0792ff75 (diff)
downloadcdo-628b660a146fea7df9679e9e217aeeed301a6bf1.zip
cdo-628b660a146fea7df9679e9e217aeeed301a6bf1.tar.gz
cdo-628b660a146fea7df9679e9e217aeeed301a6bf1.tar.bz2
Added delegating registries.
-rw-r--r--plugins/org.eclipse.net4j/META-INF/MANIFEST.MF1
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractDelegatingRegistry.java149
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java57
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapDelegatingRegistry.java52
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/IRegistryDelta.java6
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java163
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java126
-rw-r--r--plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java47
8 files changed, 225 insertions, 376 deletions
diff --git a/plugins/org.eclipse.net4j/META-INF/MANIFEST.MF b/plugins/org.eclipse.net4j/META-INF/MANIFEST.MF
index dea782a..c31d411 100644
--- a/plugins/org.eclipse.net4j/META-INF/MANIFEST.MF
+++ b/plugins/org.eclipse.net4j/META-INF/MANIFEST.MF
@@ -28,7 +28,6 @@ Export-Package: org.eclipse.internal.net4j.transport;version="0.8.0",
org.eclipse.net4j.util.om.log;version="0.8.0",
org.eclipse.net4j.util.om.trace;version="0.8.0",
org.eclipse.net4j.util.registry;version="0.8.0",
- org.eclipse.net4j.util.registry.delegating;version="0.8.0",
org.eclipse.net4j.util.stream;version="0.8.0"
Eclipse-LazyStart: true
Require-Bundle: org.eclipse.core.runtime;resolution:=optional
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractDelegatingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractDelegatingRegistry.java
new file mode 100644
index 0000000..b4a7b3f
--- /dev/null
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractDelegatingRegistry.java
@@ -0,0 +1,149 @@
+/***************************************************************************
+ * 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 java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Implementation note: AbstractDelegatingRegistry does not preserve the
+ * "modifyable view" contract of {@link Map#entrySet()} as well as of
+ * {@link Map#keySet()}, i.e. they are disconnected sets and modifications
+ * applied to them are not applied to their underlying
+ * AbstractDelegatingRegistry.
+ * <p>
+ *
+ * @author Eike Stepper
+ */
+public abstract class AbstractDelegatingRegistry<K, V> extends AbstractRegistry<K, V>
+{
+ private IRegistry<K, V> delegate;
+
+ public AbstractDelegatingRegistry(IRegistry<K, V> delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public AbstractDelegatingRegistry(IRegistry<K, V> delegate, boolean autoCommit)
+ {
+ super(autoCommit);
+ this.delegate = delegate;
+ }
+
+ @Override
+ public V get(Object key)
+ {
+ V result = getMap().get(key);
+ if (result == null)
+ {
+ result = getDelegate().get(key);
+ }
+
+ return result;
+ }
+
+ @Override
+ public Set<Entry<K, V>> entrySet()
+ {
+ return mergedEntrySet();
+ }
+
+ @Override
+ public Set<K> keySet()
+ {
+ return mergedKeySet();
+ }
+
+ @Override
+ public Collection<V> values()
+ {
+ return mergedValues();
+ }
+
+ @Override
+ protected V register(K key, V value)
+ {
+ V delegated = getDelegate().get(key);
+ V old = getMap().put(key, value);
+ if (old == null)
+ {
+ if (delegated != null)
+ {
+ // Unhidden delegated element now becomes hidden
+ getTransaction().rememberDeregistered(key, delegated);
+ }
+
+ getTransaction().rememberRegistered(key, value);
+ return delegated;
+ }
+ else
+ {
+ getTransaction().rememberDeregistered(key, old);
+ getTransaction().rememberRegistered(key, value);
+ return old;
+ }
+ }
+
+ @Override
+ protected V deregister(Object key)
+ {
+ V delegated = getDelegate().get(key);
+ V old = getMap().remove(key);
+ if (old != null)
+ {
+ getTransaction().rememberDeregistered(key, old);
+ if (delegated != null)
+ {
+ // Hidden delegated element now becomes unhidden
+ getTransaction().rememberRegistered((K)key, delegated);
+ }
+ }
+
+ return old;
+ }
+
+ protected Set<Entry<K, V>> mergedEntrySet()
+ {
+ final Map<K, V> merged = new HashMap<K, V>();
+ merged.putAll(getDelegate());
+ merged.putAll(getMap());
+ return merged.entrySet();
+ }
+
+ protected Set<K> mergedKeySet()
+ {
+ final Set<K> merged = new HashSet<K>();
+ merged.addAll(getDelegate().keySet());
+ merged.addAll(getMap().keySet());
+ return merged;
+ }
+
+ protected Collection<V> mergedValues()
+ {
+ final List<V> result = new ArrayList<V>();
+ for (K key : keySet())
+ {
+ result.add(get(key));
+ }
+
+ return result;
+ }
+
+ protected IRegistry<K, V> getDelegate()
+ {
+ return delegate;
+ }
+}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
index 4f76750..747fc08 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/AbstractRegistry.java
@@ -108,7 +108,7 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
*/
public synchronized V put(K key, V value)
{
- V result = getTransaction().put(key, value);
+ V result = register(key, value);
autoCommit();
return result;
}
@@ -121,12 +121,11 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
{
if (!t.isEmpty())
{
- Transaction transaction = getTransaction();
Iterator<? extends Entry<? extends K, ? extends V>> i = t.entrySet().iterator();
while (i.hasNext())
{
Entry<? extends K, ? extends V> e = i.next();
- transaction.put(e.getKey(), e.getValue());
+ register(e.getKey(), e.getValue());
}
autoCommit();
@@ -139,7 +138,7 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
*/
public synchronized V remove(Object key)
{
- V result = getTransaction().remove(key);
+ V result = deregister(key);
autoCommit();
return result;
}
@@ -152,11 +151,10 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
{
if (isEmpty())
{
- Transaction transaction = getTransaction();
Set<K> keys = keySet();
for (K key : keys)
{
- transaction.remove(key);
+ deregister(key);
}
autoCommit();
@@ -200,12 +198,26 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
protected V register(K key, V value)
{
- return getMap().put(key, value);
+ Transaction transaction = getTransaction();
+ V oldValue = getMap().put(key, value);
+ if (oldValue != null)
+ {
+ transaction.rememberDeregistered(key, oldValue);
+ }
+
+ transaction.rememberRegistered(key, value);
+ return oldValue;
}
protected V deregister(Object key)
{
- return getMap().remove(key);
+ V value = getMap().remove(key);
+ if (value != null)
+ {
+ getTransaction().rememberDeregistered(key, value);
+ }
+
+ return value;
}
protected Transaction getTransaction()
@@ -307,29 +319,6 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
++nesting;
}
- public V put(K key, V value)
- {
- V oldValue = register(key, value);
- if (oldValue != null)
- {
- rememberDeregistering(key, oldValue);
- }
-
- rememberRegistered(key, value);
- return oldValue;
- }
-
- public V remove(Object key)
- {
- V value = deregister(key);
- if (value != null)
- {
- rememberDeregistering(key, value);
- }
-
- return value;
- }
-
public void commit(boolean notifications)
{
if (--nesting == 0)
@@ -343,14 +332,14 @@ public abstract class AbstractRegistry<K, V> implements IRegistry<K, V>
}
}
- protected void rememberRegistered(K key, V value)
+ public void rememberRegistered(K key, V value)
{
deltas.add(new RegistryDelta(key, value, Kind.REGISTERED));
}
- protected void rememberDeregistering(Object key, V value)
+ public void rememberDeregistered(Object key, V value)
{
- deltas.add(new RegistryDelta(key, value, Kind.DEREGISTERING));
+ deltas.add(new RegistryDelta(key, value, Kind.DEREGISTERED));
}
}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapDelegatingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapDelegatingRegistry.java
new file mode 100644
index 0000000..8c1764a
--- /dev/null
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/HashMapDelegatingRegistry.java
@@ -0,0 +1,52 @@
+/***************************************************************************
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Eike Stepper
+ */
+public class HashMapDelegatingRegistry<K, V> extends AbstractDelegatingRegistry<K, V>
+{
+ private Map<K, V> map;
+
+ public HashMapDelegatingRegistry(IRegistry<K, V> delegate)
+ {
+ super(delegate);
+ map = new HashMap();
+ }
+
+ public HashMapDelegatingRegistry(IRegistry<K, V> delegate, int initialCapacity)
+ {
+ super(delegate);
+ map = new HashMap(initialCapacity);
+ }
+
+ public HashMapDelegatingRegistry(IRegistry<K, V> delegate, int initialCapacity, float loadFactor)
+ {
+ super(delegate);
+ map = new HashMap(initialCapacity, loadFactor);
+ }
+
+ public HashMapDelegatingRegistry(IRegistry<K, V> delegate, Map<? extends K, ? extends V> m)
+ {
+ super(delegate);
+ map = new HashMap(m);
+ }
+
+ @Override
+ protected Map<K, V> getMap()
+ {
+ return map;
+ }
+}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/IRegistryDelta.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/IRegistryDelta.java
index 7276619..ae206c2 100644
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/IRegistryDelta.java
+++ b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/IRegistryDelta.java
@@ -17,10 +17,6 @@ import java.util.Map;
*/
public interface IRegistryDelta<K, V> extends Map.Entry<K, V>
{
- // public static final int REGISTERED = 1;
- //
- // public static final int DEREGISTERING = 2;
-
public Kind getKind();
/**
@@ -28,6 +24,6 @@ public interface IRegistryDelta<K, V> extends Map.Entry<K, V>
*/
public enum Kind
{
- REGISTERED, DEREGISTERING
+ REGISTERED, DEREGISTERED
}
}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java
deleted file mode 100644
index 7ea2c84..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/AbstractCachingRegistry.java
+++ /dev/null
@@ -1,163 +0,0 @@
-/***************************************************************************
- * 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.delegating;
-
-import org.eclipse.net4j.util.registry.IRegistry;
-import org.eclipse.net4j.util.registry.IRegistryElement.Descriptor;
-import org.eclipse.net4j.util.registry.IRegistryListener.EventType;
-
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * TODO Check if all methods of {@link DelegatingRegistry} still do what they
- * should. TODO Remove {@link 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);
- }
-
- 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);
-
- 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()
- {
- final Set<ID> delegateIDs = super.getElementIDs();
- final Set<ID> cacheIDs = getCache().keySet();
- final Set<ID> ids = new HashSet(delegateIDs.size() + cacheIDs.size());
-
- ids.addAll(delegateIDs);
- ids.addAll(cacheIDs);
- return ids;
- }
-
- @Override
- public synchronized void dispose()
- {
- for (E element : getCache().values())
- {
- fireElementDeregistering(element);
- }
-
- 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();
-}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java
deleted file mode 100644
index 4ef308a..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/DelegatingRegistry.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/***************************************************************************
- * 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.delegating;
-
-import org.eclipse.net4j.util.registry.AbstractRegistry;
-import org.eclipse.net4j.util.registry.IRegistry;
-import org.eclipse.net4j.util.registry.IRegistryListener;
-import org.eclipse.net4j.util.registry.IRegistryListener.EventType;
-
-import java.util.Set;
-
-/**
- * @author Eike Stepper
- */
-public class DelegatingRegistry<ID, E extends IRegistryElement<ID>> extends AbstractRegistry<ID, E>
-{
- private IRegistry<ID, E> delegate;
-
- private IRegistryListener<ID, E> delegateListener = new IRegistryListener<ID, E>()
- {
- public void notifyRegistryEvent(IRegistry<ID, E> registry, EventType eventType, E element)
- {
- handleDelegateEvent(eventType, element);
- }
- };
-
- public DelegatingRegistry(IRegistry<ID, E> delegate)
- {
- this(delegate, DEFAULT_RESOLVING);
- }
-
- public DelegatingRegistry(IRegistry<ID, E> delegate, boolean resolving)
- {
- super(resolving);
- this.delegate = delegate;
- delegate.addRegistryListener(delegateListener);
- }
-
- public IRegistry<ID, E> getDelegate()
- {
- return delegate;
- }
-
- public void register(E element)
- {
- delegatedRegister(element);
- }
-
- public void deregister(ID id)
- {
- delegatedDeregister(id);
- }
-
- public E lookup(ID id, boolean resolve)
- {
- return delegatedLookup(id, resolve);
- }
-
- public Set<ID> getElementIDs()
- {
- return delegatedGetElementIDs();
- }
-
- @Override
- public void dispose()
- {
- delegate.removeRegistryListener(delegateListener);
- }
-
- protected void delegatedRegister(E element)
- {
- delegate.register(element);
- }
-
- protected void delegatedDeregister(ID id)
- {
- delegate.deregister(id);
- }
-
- protected E delegatedLookup(ID id, boolean resolve)
- {
- return delegate.lookup(id, resolve);
- }
-
- protected Set<ID> delegatedGetElementIDs()
- {
- return delegate.getElementIDs();
- }
-
- protected int delegatedSize()
- {
- return delegate.size();
- }
-
- protected void delegatedDispose()
- {
- delegate.dispose();
- }
-
- protected void handleDelegateEvent(EventType eventType, E element)
- {
- fireRegistryEvent(eventType, element);
- }
-
- @Override
- protected void replaceElement(ID id, E element)
- {
- // Do nothing
- }
-
- /* (non-Javadoc)
- * @see org.eclipse.net4j.util.registry.AbstractRegistry#getMap()
- */
- protected Map<ID, E> getMap()
- {
- return null;
- }
-}
diff --git a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java b/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java
deleted file mode 100644
index 88ec562..0000000
--- a/plugins/org.eclipse.net4j/src/org/eclipse/net4j/util/registry/delegating/HashCacheRegistry.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/***************************************************************************
- * 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.delegating;
-
-import org.eclipse.net4j.util.registry.IRegistry;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * @author Eike Stepper
- */
-public class HashCacheRegistry<ID, E extends IRegistryElement<ID>> extends
- AbstractCachingRegistry<ID, E>
-{
- private Map<ID, E> cache;
-
- public HashCacheRegistry(IRegistry<ID, E> delegate)
- {
- this(delegate, DEFAULT_RESOLVING);
- }
-
- public HashCacheRegistry(IRegistry<ID, E> delegate, boolean resolving)
- {
- super(delegate, resolving);
- cache = createCache();
- }
-
- @Override
- protected Map<ID, E> getCache()
- {
- return cache;
- }
-
- protected Map<ID, E> createCache()
- {
- return new HashMap(0);
- }
-}