Class PlanitComponentFactory<T extends PlanitComponent<?>>

  • 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 Detail

      • componentSuperTypeCanonicalName

        protected final String componentSuperTypeCanonicalName
        instance of the super component class this factory creates subclass instances for
      • registeredPlanitComponents

        protected static final HashMap<String,​TreeSet<String>> registeredPlanitComponents
        Register per traffic assignment component type the derived classes that are supported
    • 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 class EventProducerImpl
        Parameters:
        eventListener - to notify for the event
        event - 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
      • createWithListeners

        public static <C extends PlanitComponent<?>,​U extends C> U createWithListeners​(Class<C> clazzCategory,
                                                                                             Class<U> concreteClass,
                                                                                             Object[] constructorParameters,
                                                                                             Object[] eventParameters,
                                                                                             PlanitComponentListener... listeners)
        Simplified factory method which creates the component, registers the provided listeners for the temporary factory instance component and then invokes the creation with those listeners as a one-off call
        Type Parameters:
        C - type of component
        U - specific type of component
        Parameters:
        clazzCategory - the concrete component belongs to
        concreteClass - to create
        constructorParameters - to use
        eventParameters - to apply for the event listeners that are listening (if any)
        listeners - to register (may be null)
        Returns:
        created component
      • createWithListeners

        public static <C extends PlanitComponent<?>> C createWithListeners​(Class<C> clazzCategory,
                                                                           String concreteClassCanonicalName,
                                                                           Object[] constructorParameters,
                                                                           Object[] eventParameters,
                                                                           PlanitComponentListener... listeners)
        Simplified factory method which creates the component, registers the provided listeners for the temporary factory instance component and then invokes the creation with those listeners as a one-off call
        Type Parameters:
        C - type of component
        Parameters:
        clazzCategory - the concrete component belongs to
        concreteClassCanonicalName - to create
        constructorParameters - to use
        eventParameters - to apply for the event listeners that are listening (if any)
        listeners - to register (may be null)
        Returns:
        created component
      • createWithListeners

        public static <C extends PlanitComponent<?>> C createWithListeners​(Class<C> clazzCategory,
                                                                           String concreteClassCanonicalName,
                                                                           Object[] constructorParameters,
                                                                           PlanitComponentListener... listeners)
        Simplified factory method which creates the component, registers the provided listeners for the temporary factory instance component and then invokes the creation with those listeners as a one-off call
        Type Parameters:
        C - type of component
        Parameters:
        clazzCategory - the concrete component belongs to
        concreteClassCanonicalName - to create
        constructorParameters - to use
        listeners - to register (may be null)
        Returns:
        created component
      • create

        public static <C extends PlanitComponent<?>> C create​(Class<C> clazzCategory,
                                                              String concreteClassCanonicalName,
                                                              Object[] constructorParameters)
        Simplified factory method which creates the component (without any listeners)
        Type Parameters:
        C - type of component
        Parameters:
        clazzCategory - the concrete component belongs to
        concreteClassCanonicalName - to create
        constructorParameters - to use
        Returns:
        created component
      • createWithListeners

        public static <C extends PlanitComponent<?>,​U extends C> U createWithListeners​(Class<C> clazzCategory,
                                                                                             Class<U> concreteClass,
                                                                                             Object[] constructorParameters,
                                                                                             PlanitComponentListener... listeners)
        Simplified factory method which creates the component, registers the provided listeners for the temporary factory instance component and then invokes the creation with those listeners as a one-off call
        Type Parameters:
        C - type of component
        U - specific type of component
        Parameters:
        clazzCategory - the concrete component belongs to
        concreteClass - to create
        constructorParameters - to use
        listeners - to register (may be null)
        Returns:
        created component
      • create

        public <C extends PlanitComponent<?>> C create​(String planitComponentClassName,
                                                       Object[] constructorParameters)
        Create PLANit component
        Type Parameters:
        C - type of component
        Parameters:
        planitComponentClassName - the derived class name of the PLANit component (without packages)
        constructorParameters - parameters to pass to the constructor
        Returns:
        the created TrafficAssignmentComponent
      • create

        public <C extends PlanitComponent<?>> C create​(String planitComponentClassName,
                                                       Object[] constructorParameters,
                                                       Object... eventParameters)
        Create PLANit component
        Type Parameters:
        C - type of component
        Parameters:
        planitComponentClassName - the derived class name of the PLANit component (without packages)
        constructorParameters - parameters to pass to the constructor
        eventParameters - object array which contains any additional data that might be required to populate the component
        Returns:
        the created component
      • 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 register
        eventTypes - 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)
        Remove listener for all event types of this producer
        Parameters:
        listener - to remove