Interface Factory


  • public interface Factory
    Instance builder for arbitrary Java classes with partly specified parameters.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      <E> CreationResult<E> build​(java.lang.Class<E> clazz, java.lang.Object... parameters)  
      <E> CreationResult<E> build​(java.lang.Class<E> clazz, java.util.List<?> args)  
      <I,​O>
      java.util.Optional<O>
      convert​(java.lang.Class<O> clazz, I target)
      Applies the available implicit conversions and tries to convert the provided object to the provided class.
      <I,​O>
      O
      convertOrFail​(java.lang.Class<O> clazz, I target)
      Applies the available implicit conversions and tries to convert the provided object to the provided class.
      <E> boolean deregisterSingleton​(E object)
      Removes a registered singleton.
      java.util.Map<java.lang.Class<?>,​java.lang.Object> getSingletonObjects()  
      <S,​D>
      void
      registerImplicit​(java.lang.Class<S> source, java.lang.Class<D> destination, java.util.function.Function<? super S,​? extends D> implicit)
      Registers an implicit conversion function.
      <E> void registerSingleton​(E object)
      Registers a singleton.
      <E> void registerSingleton​(java.lang.Class<? super E> bound, E object)
      Registers a singleton.
      <E> void registerSingleton​(java.lang.Class<? super E> lowerBound, java.lang.Class<? super E> upperBound, E object)
      Registers a singleton.
    • Method Detail

      • build

        <E> CreationResult<E> build​(java.lang.Class<E> clazz,
                                    java.util.List<?> args)
        Type Parameters:
        E - the Object type
        Parameters:
        clazz - the Class of which an instance should get built
        args - the arguments. They should be in order. Arguments that can be deduced automatically as singletons must not be included. The implicit conversions are automatically applied, in case multiple of them are available, the shortest path on the conversion graph is used.
        Returns:
        the result of the build attempt.
      • build

        <E> CreationResult<E> build​(java.lang.Class<E> clazz,
                                    java.lang.Object... parameters)
        Type Parameters:
        E - the Object type
        Parameters:
        clazz - the Class of which an instance should get built
        parameters - the arguments. They should be in order. Arguments that can be deduced automatically as singletons must not be included. The implicit conversions are automatically applied, in case multiple of them are available, the shortest path on the conversion graph is used.
        Returns:
        the result of the build attempt.
      • convert

        <I,​O> java.util.Optional<O> convert​(java.lang.Class<O> clazz,
                                                  I target)
        Applies the available implicit conversions and tries to convert the provided object to the provided class.
        Type Parameters:
        I - the input Object type
        O - the output Object type
        Parameters:
        clazz - the target Class
        target - the object that should get converted.
        Returns:
        the converted object if it was possible to convert, or Optional.empty() if the conversion was not possible
      • convertOrFail

        <I,​O> O convertOrFail​(java.lang.Class<O> clazz,
                                    I target)
        Applies the available implicit conversions and tries to convert the provided object to the provided class.
        Type Parameters:
        I - the input Object type
        O - the output Object type
        Parameters:
        clazz - the target Class
        target - the object that should get converted.
        Returns:
        the converted object if it was possible to convert, or Optional.empty() if the conversion was not possible
        Throws:
        java.lang.IllegalArgumentException - if for any reason the class can't get converted with the configured implicit conversions
      • deregisterSingleton

        <E> boolean deregisterSingleton​(E object)
        Removes a registered singleton.
        Type Parameters:
        E - the Object type
        Parameters:
        object - the object that should be forgot
        Returns:
        true if the Object was successfully de-registered
      • getSingletonObjects

        java.util.Map<java.lang.Class<?>,​java.lang.Object> getSingletonObjects()
        Returns:
        the map of registered singleton objects
      • registerImplicit

        <S,​D> void registerImplicit​(java.lang.Class<S> source,
                                          java.lang.Class<D> destination,
                                          java.util.function.Function<? super S,​? extends D> implicit)
        Registers an implicit conversion function. All the superclasses and implemented interfaces of the source are registered as well as convertible.
        Type Parameters:
        S - Input type
        D - Output type
        Parameters:
        source - origin class
        destination - destination class
        implicit - the conversion Function
      • registerSingleton

        <E> void registerSingleton​(java.lang.Class<? super E> lowerBound,
                                   java.lang.Class<? super E> upperBound,
                                   E object)
        Registers a singleton. It gets mapped to every class from the provided lower bound and the provided upper bound (both must be superclasses of the object's own class). If the bound is an interface, all the sub-interfaces that the class implements get registered too.
        Type Parameters:
        E - the Object type
        Parameters:
        lowerBound - the lower bound class
        upperBound - the upper bound class
        object - the object that should be registered
      • registerSingleton

        <E> void registerSingleton​(java.lang.Class<? super E> bound,
                                   E object)
        Registers a singleton. It gets mapped to every class from its own (discovered by using Object.getClass() and the provided upper bound. If the bound is an interface, all the sub-interfaces that the class implements get registered too.
        Type Parameters:
        E - the Object type
        Parameters:
        bound - the upper bound class
        object - the object that should be registered
      • registerSingleton

        <E> void registerSingleton​(E object)
        Registers a singleton. It only gets mapped to its own class (discovered by using Object.getClass().
        Type Parameters:
        E - the Object type
        Parameters:
        object - the object that should be registered