Package org.goplanit.component
Class PlanitComponentFactory<T extends PlanitComponent<?>>
- java.lang.Object
-
- org.goplanit.utils.event.EventProducerImpl
-
- org.goplanit.component.PlanitComponentFactory<T>
-
- Type Parameters:
T
- generic type of a type PLANit component for which we construct the eligible derived classes by class name
- All Implemented Interfaces:
Serializable
public class PlanitComponentFactory<T extends PlanitComponent<?>> extends EventProducerImpl implements Serializable
Generic factory class for registered subclasses of predefined PLANit components, so it does not create instances of T but of subclasses of T.- Author:
- markr
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected String
componentSuperTypeCanonicalName
instance of the super component class this factory creates subclass instances forprotected static HashMap<String,TreeSet<String>>
registeredPlanitComponents
Register per traffic assignment component type the derived classes that are supported-
Fields inherited from class org.goplanit.utils.event.EventProducerImpl
listeners
-
-
Constructor Summary
Constructors Constructor Description PlanitComponentFactory(Class<U> componentSuperType)
Constructor.PlanitComponentFactory(String componentSuperTypeCanonicalName)
Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(PlanitComponentListener listener)
Add a listener for all its known supported PLANit component event typesvoid
addListener(PlanitComponentListener listener, PlanitComponentEventType... eventTypes)
Add a listener for PLANit component event types firedT
create(String planitComponentClassName, Object[] constructorParameters)
Create PLANit componentT
create(String planitComponentClassName, Object[] constructorParameters, Object... eventParameters)
Create PLANit componentprotected void
fireEvent(EventListener eventListener, Event event)
Let derived class deal with the handling of the listener, where based on the derived event implementation the listener's concrete class can be determined which in turn allows for calling the right event callback method which is unknown at this base level since this event mechanism does not force a particular notification method signature on its listener interface<U> boolean
isFactoryForDerivedClassesOf(Class<U> superClazz)
Allows one to verify if this factory creates derived classes of the provided class super type.static void
registerPlanitComponentType(Class<? extends PlanitComponent<?>> planitComponent)
Register a component type that one can choose for the given PLANit componentvoid
removeListener(PlanitComponentListener listener)
Remove listener for all event types of this producervoid
removeListener(PlanitComponentListener listener, PlanitComponentEventType eventType)
Remove listener for given event type-
Methods inherited from class org.goplanit.utils.event.EventProducerImpl
addListener, addListener, addListener, addListener, fireEvent, getEventTypesWithListeners, hasListener, hasListeners, numberOfListeners, removeAllListeners, removeListener, removeListener
-
-
-
-
Field Detail
-
componentSuperTypeCanonicalName
protected final String componentSuperTypeCanonicalName
instance of the super component class this factory creates subclass instances for
-
-
Constructor Detail
-
PlanitComponentFactory
public PlanitComponentFactory(Class<U> componentSuperType)
Constructor. Here we make sure it is a type that extends the PLANit component class. We do not do so generally on the class level since this might lead to conflicts when the class has generic arguments itself which leads to issues (that I have not been able to solve). In the latter case use the other constructor which gets around this problem by simply providing the canonical class name corresponding to type T- Type Parameters:
U
- PLANit component type- Parameters:
componentSuperType
- super type for this factory
-
PlanitComponentFactory
public PlanitComponentFactory(String componentSuperTypeCanonicalName)
Constructor. Use this constructor when the component super type that you use is not compatible with ClassT, for example because the super type itself uses generics, i.e., T(U,V), in which case the default constructor does not work. Make sure however, that the provided canonical class name is compatible with T, i.e., it must extend from TrafficAssigmentComponent- Parameters:
componentSuperTypeCanonicalName
- super type's canonical class name for this factory which should be the same as ClassT.getCanonicalName()
-
-
Method Detail
-
fireEvent
protected void fireEvent(EventListener eventListener, Event event)
Let derived class deal with the handling of the listener, where based on the derived event implementation the listener's concrete class can be determined which in turn allows for calling the right event callback method which is unknown at this base level since this event mechanism does not force a particular notification method signature on its listener interface- Specified by:
fireEvent
in classEventProducerImpl
- Parameters:
eventListener
- to notify for the eventevent
- to process for the listener
-
registerPlanitComponentType
public static void registerPlanitComponentType(Class<? extends PlanitComponent<?>> planitComponent)
Register a component type that one can choose for the given PLANit component- Parameters:
planitComponent
- PLANit component to be registered
-
create
public T create(String planitComponentClassName, Object[] constructorParameters) throws PlanItException
Create PLANit component- Parameters:
planitComponentClassName
- the derived class name of the PLANit component (without packages)constructorParameters
- parameters to pass to the constructor- Returns:
- the created TrafficAssignmentComponent
- Throws:
PlanItException
- thrown if there is an error
-
create
public T create(String planitComponentClassName, Object[] constructorParameters, Object... eventParameters) throws PlanItException
Create PLANit component- Parameters:
planitComponentClassName
- the derived class name of the PLANit component (without packages)constructorParameters
- parameters to pass to the constructoreventParameters
- object array which contains any additional data that might be required to populate the component- Returns:
- the created component
- Throws:
PlanItException
- thrown if there is an error
-
isFactoryForDerivedClassesOf
public <U> boolean isFactoryForDerivedClassesOf(Class<U> superClazz)
Allows one to verify if this factory creates derived classes of the provided class super type. Useful in case the generic type parameter is not available at run time for this factory- Type Parameters:
U
- type to verify- Parameters:
superClazz
- class of type- Returns:
- true when factory is compatible, false otherwise
-
addListener
public void addListener(PlanitComponentListener listener, PlanitComponentEventType... eventTypes)
Add a listener for PLANit component event types fired- Parameters:
listener
- to registereventTypes
- to register for
-
addListener
public void addListener(PlanitComponentListener listener)
Add a listener for all its known supported PLANit component event types- Parameters:
listener
- to register, when null nothing is registered
-
removeListener
public void removeListener(PlanitComponentListener listener, PlanitComponentEventType eventType)
Remove listener for given event type- Parameters:
listener
- to removeeventType
- to remove for
-
removeListener
public void removeListener(PlanitComponentListener listener)
Remove listener for all event types of this producer- Parameters:
listener
- to remove
-
-