Author: pete.muir(a)jboss.org
Date: 2008-10-24 09:18:58 -0400 (Fri, 24 Oct 2008)
New Revision: 141
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanMap.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/MapWrapper.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/BasicContext.java
Log:
Add a BeanMap
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/BasicContext.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/BasicContext.java 2008-10-24
12:42:57 UTC (rev 140)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/BasicContext.java 2008-10-24
13:18:58 UTC (rev 141)
@@ -1,14 +1,14 @@
package org.jboss.webbeans;
import java.lang.annotation.Annotation;
-import java.util.HashMap;
-import java.util.Map;
import javax.webbeans.ContextNotActiveException;
import javax.webbeans.manager.Bean;
import javax.webbeans.manager.Context;
import javax.webbeans.manager.Manager;
+import org.jboss.webbeans.util.BeanMap;
+
/**
* Basic implementation of javax.webbeans.Context, backed by a HashMap
*
@@ -17,29 +17,24 @@
*/
public class BasicContext implements Context
{
- private Map<Bean<? extends Object>, Object> beans;
+ private BeanMap<Object> beans;
private Class<? extends Annotation> scopeType;
private boolean active;
public BasicContext(Class<? extends Annotation> scopeType)
{
this.scopeType = scopeType;
- beans = new HashMap<Bean<?>, Object>();
+ beans = new BeanMap<Object>();
active = true;
}
- public void add(Bean<?> component, Object instance)
+ public <T> T get(Bean<T> bean, boolean create)
{
- beans.put(component, instance);
- }
-
- public <T> T get(Bean<T> component, boolean create)
- {
if (!active)
{
throw new ContextNotActiveException();
}
- T instance = (T) beans.get(component);
+ T instance = beans.get(bean);
if (instance != null)
{
return instance;
@@ -52,9 +47,9 @@
// TODO should component creation be synchronized?
- instance = component.create();
+ instance = bean.create();
- beans.put(component, instance);
+ beans.put(bean, instance);
return instance;
}
@@ -65,7 +60,7 @@
public <T> void remove(Manager container, Bean<T> bean)
{
- T instance = (T) beans.get(bean);
+ T instance = beans.get(bean);
if (instance != null)
{
@@ -81,12 +76,10 @@
public void destroy(Manager container)
{
- for (Bean c : beans.keySet())
+ for (Bean<? extends Object> bean : beans.keySet())
{
- c.destroy(beans.get(c));
+ remove(container, bean);
}
- beans.clear();
- active = false;
}
public boolean isActive()
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanMap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanMap.java
(rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanMap.java 2008-10-24
13:18:58 UTC (rev 141)
@@ -0,0 +1,21 @@
+package org.jboss.webbeans.util;
+
+import java.util.HashMap;
+
+import javax.webbeans.manager.Bean;
+
+public class BeanMap<V> extends MapWrapper<Bean<? extends V>, V>
+{
+
+ public BeanMap()
+ {
+ super(new HashMap<Bean<? extends V>, V>());
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T extends V> T get(Bean<? extends T> key)
+ {
+ return (T) super.get(key);
+ }
+
+}
Property changes on:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/BeanMap.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/MapWrapper.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/MapWrapper.java
(rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/MapWrapper.java 2008-10-24
13:18:58 UTC (rev 141)
@@ -0,0 +1,79 @@
+package org.jboss.webbeans.util;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+public class MapWrapper<K, V> implements Map<K, V>
+{
+
+ private Map<K, V> delegate;
+
+
+
+ public MapWrapper(Map<K, V> delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ public void clear()
+ {
+ delegate.clear();
+ }
+
+ public boolean containsKey(Object key)
+ {
+ return delegate.containsKey(key);
+ }
+
+ public boolean containsValue(Object value)
+ {
+ return delegate.containsValue(value);
+ }
+
+ public Set<Entry<K, V>> entrySet()
+ {
+ return delegate.entrySet();
+ }
+
+ public V get(Object key)
+ {
+ return delegate.get(key);
+ }
+
+ public boolean isEmpty()
+ {
+ return delegate.isEmpty();
+ }
+
+ public Set<K> keySet()
+ {
+ return delegate.keySet();
+ }
+
+ public V put(K key, V value)
+ {
+ return delegate.put(key, value);
+ }
+
+ public void putAll(Map<? extends K, ? extends V> t)
+ {
+ delegate.putAll(t);
+ }
+
+ public V remove(Object key)
+ {
+ return delegate.remove(key);
+ }
+
+ public int size()
+ {
+ return delegate.size();
+ }
+
+ public Collection<V> values()
+ {
+ return delegate.values();
+ }
+
+}
Property changes on:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/MapWrapper.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Show replies by date