net.sf.jguiraffe.di.impl
Class DefaultBeanStore

java.lang.Object
  extended by net.sf.jguiraffe.di.impl.DefaultBeanStore
All Implemented Interfaces:
BeanStore, MutableBeanStore

public class DefaultBeanStore
extends java.lang.Object
implements MutableBeanStore

A simple yet fully functional default implementation of the BeanStore interface.

This implementation is based on a HashMap. BeanProvider objects can be added to this map using the addBeanProvider() method. They can then be queried through the getBeanProvider method.

Note: The underlying map is not synchronized. This does not cause any problems when used read-only by the dependency injection framework. But if bean providers should be concurrently added, manual synchronization is required. The intended use case is that an instance is created and populated with BeanProvider objects in an initialization phase. Then it should only be accessed in a read-only fashion.

Version:
$Id: DefaultBeanStore.java 156 2009-03-03 21:04:47Z oheger $
Author:
Oliver Heger

Constructor Summary
DefaultBeanStore()
          Creates a new instance of DefaultBeanStore.
DefaultBeanStore(java.lang.String name, BeanStore parent)
          Creates a new instance of DefaultBeanStore and sets the name and the reference to the parent.
 
Method Summary
 java.lang.String addAnonymousBeanProvider(int index, BeanProvider provider)
          Adds an anonymous BeanProvider.
 void addBeanProvider(java.lang.String name, BeanProvider provider)
          Adds the specified BeanProvider to this bean store under the given name.
 void clear()
          Removes all BeanProviders from this bean store.
 BeanProvider getBeanProvider(java.lang.String name)
          Returns the BeanProvider with the specified name.
 java.lang.String getName()
          Returns the name of this bean store.
 BeanStore getParent()
          Returns the parent of this bean store or null if this is a top level store.
 java.util.Set<java.lang.String> providerNames()
          Returns a set with the names of all contained bean providers.
 BeanProvider removeBeanProvider(java.lang.String name)
          Removes the BeanProvider with the specified name from this bean store.
 void setName(java.lang.String n)
          Sets the name of this bean store.
 void setParent(BeanStore p)
          Sets the parent for this bean store.
 java.lang.String toString()
          Returns a string representation of this object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DefaultBeanStore

public DefaultBeanStore()
Creates a new instance of DefaultBeanStore.


DefaultBeanStore

public DefaultBeanStore(java.lang.String name,
                        BeanStore parent)
Creates a new instance of DefaultBeanStore and sets the name and the reference to the parent.

Parameters:
name - the name of this bean store
parent - the reference to the parent bean store
Method Detail

addBeanProvider

public void addBeanProvider(java.lang.String name,
                            BeanProvider provider)
Adds the specified BeanProvider to this bean store under the given name.

Specified by:
addBeanProvider in interface MutableBeanStore
Parameters:
name - the name of the bean provider (must not be null)
provider - the BeanProvider to be registered
Throws:
java.lang.IllegalArgumentException - if the name or the provider is null

addAnonymousBeanProvider

public java.lang.String addAnonymousBeanProvider(int index,
                                                 BeanProvider provider)
Adds an anonymous BeanProvider. This method will generate a special name (which is mainly used internally) for the bean provider to add and store it under this name. The name is returned.

Specified by:
addAnonymousBeanProvider in interface MutableBeanStore
Parameters:
index - the index of the bean provider
provider - the BeanProvider to be added (must not be null)
Returns:
the name used for this BeanProvider
Throws:
java.lang.IllegalArgumentException - if the provider is null

removeBeanProvider

public BeanProvider removeBeanProvider(java.lang.String name)
Removes the BeanProvider with the specified name from this bean store. If this provider cannot be found, this operation has no effect.

Specified by:
removeBeanProvider in interface MutableBeanStore
Parameters:
name - the name of the provider to remove
Returns:
a reference to the removed provider or null if it could not be found

clear

public void clear()
Removes all BeanProviders from this bean store.

Specified by:
clear in interface MutableBeanStore

getBeanProvider

public BeanProvider getBeanProvider(java.lang.String name)
Returns the BeanProvider with the specified name. If no such element exists, null is returned.

Specified by:
getBeanProvider in interface BeanStore
Parameters:
name - the name of the desired provider
Returns:
the BeanProvider with this name

getName

public java.lang.String getName()
Returns the name of this bean store.

Specified by:
getName in interface BeanStore
Returns:
the name of this bean store

setName

public void setName(java.lang.String n)
Sets the name of this bean store.

Specified by:
setName in interface MutableBeanStore
Parameters:
n - the new name

getParent

public BeanStore getParent()
Returns the parent of this bean store or null if this is a top level store.

Specified by:
getParent in interface BeanStore
Returns:
the parent of this bean store

setParent

public void setParent(BeanStore p)
Sets the parent for this bean store.

Specified by:
setParent in interface MutableBeanStore
Parameters:
p - the parent

providerNames

public java.util.Set<java.lang.String> providerNames()
Returns a set with the names of all contained bean providers. This implementation ensures that the names of anonymous bean providers do not appear in the set returned.

Specified by:
providerNames in interface BeanStore
Returns:
the names of the registered bean providers

toString

public java.lang.String toString()
Returns a string representation of this object. This implementation returns a string listing all bean providers that belong to this store.

Overrides:
toString in class java.lang.Object
Returns:
a string for this object


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.