Class ObjectProviderUtils

  • All Implemented Interfaces:
    io.microsphere.util.Utils

    public abstract class ObjectProviderUtils
    extends java.lang.Object
    implements io.microsphere.util.Utils
    The utilities class for ObjectProvider
    Since:
    1.0.0
    Author:
    Mercy
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T getIfAvailable​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
      Retrieve the object managed by the given ObjectProvider if available, without throwing an exception in case of no such bean being present.
      static <T> T getIfAvailable​(org.springframework.beans.factory.ObjectProvider<T> objectProvider, java.util.function.Supplier<T> defaultSupplier)
      Retrieve the object managed by the given ObjectProvider if available, otherwise obtain the object using the provided default supplier.
      static <T> T getIfUnique​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
      Retrieve the object managed by the given ObjectProvider if it is unique, without throwing an exception in case of no such bean being present or multiple candidates found.
      static <T> T getIfUnique​(org.springframework.beans.factory.ObjectProvider<T> objectProvider, java.util.function.Supplier<T> defaultSupplier)
      Retrieve the object managed by the given ObjectProvider if it is unique, otherwise obtain the object using the provided default supplier.
      static <T> void ifAvailable​(org.springframework.beans.factory.ObjectProvider<T> objectProvider, java.util.function.Consumer<T> dependencyConsumer)
      Consume an instance (possibly shared or independent) of the object managed by this factory, if available.
      static <T> void ifUnique​(org.springframework.beans.factory.ObjectProvider<T> objectProvider, java.util.function.Consumer<T> dependencyConsumer)
      Consume the unique object instance managed by the given ObjectProvider if available.
      static <T> java.util.Iterator<T> iterator​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
      Returns an Iterator over all matching object instances managed by the given ObjectProvider, typically in registration order.
      static <T> java.util.stream.Stream<T> orderedStream​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
      Return a sequential Stream over all matching object instances, pre-ordered according to the factory's common order comparator.
      static <T> java.util.stream.Stream<T> stream​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
      Return a sequential Stream over all matching object instances, without specific ordering guarantees (but typically in registration order).
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • getIfAvailable

        @Nullable
        public static <T> T getIfAvailable​(org.springframework.beans.factory.ObjectProvider<T> objectProvider)
        Retrieve the object managed by the given ObjectProvider if available, without throwing an exception in case of no such bean being present.

        This method is useful when you want to safely obtain a bean instance and handle the absence of it gracefully.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         MyService service = ObjectProviderUtils.getIfAvailable(myServiceProvider);
         if (service != null) {
             service.doSomething();
         } else {
             System.out.println("MyService is not available.");
         }
         
        Type Parameters:
        T - the type of the object
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        Returns:
        an instance of the bean, or null if not available
        Throws:
        org.springframework.beans.BeansException - if there is an error creating the bean
        Since:
        Spring Framework 4.3
        See Also:
        ObjectFactory.getObject(), ObjectProvider.getIfAvailable()
      • getIfAvailable

        @Nullable
        public static <T> T getIfAvailable​(@Nullable
                                           org.springframework.beans.factory.ObjectProvider<T> objectProvider,
                                           @Nullable
                                           java.util.function.Supplier<T> defaultSupplier)
                                    throws org.springframework.beans.BeansException
        Retrieve the object managed by the given ObjectProvider if available, otherwise obtain the object using the provided default supplier.

        This method is useful when you want to safely obtain a bean instance and provide a fallback mechanism in case the bean is not available.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         MyService service = ObjectProviderUtils.getIfAvailable(myServiceProvider, () -> new DefaultMyService());
         service.doSomething();
         
        Type Parameters:
        T - the type of the object
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        defaultSupplier - a callback for supplying a default object if none is present in the factory
        Returns:
        an instance of the bean, or the supplied default object if no such bean is available
        Throws:
        org.springframework.beans.BeansException - in case of creation errors
        Since:
        Spring Framework 5.0
        See Also:
        ObjectProvider.getIfAvailable(Supplier)
      • ifAvailable

        public static <T> void ifAvailable​(@Nullable
                                           org.springframework.beans.factory.ObjectProvider<T> objectProvider,
                                           @Nullable
                                           java.util.function.Consumer<T> dependencyConsumer)
                                    throws org.springframework.beans.BeansException
        Consume an instance (possibly shared or independent) of the object managed by this factory, if available.

        This method safely retrieves the object from the provider and passes it to the consumer if both are non-null. It does not throw an exception if no object is available.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         ObjectProviderUtils.ifAvailable(myServiceProvider, service -> {
             service.doSomething();
         });
         
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        dependencyConsumer - a callback for processing the target object if available (not called otherwise)
        Throws:
        org.springframework.beans.BeansException - in case of creation errors
        Since:
        Spring Framework 5.0
        See Also:
        ObjectProvider.getIfAvailable()
      • getIfUnique

        @Nullable
        public static <T> T getIfUnique​(@Nullable
                                        org.springframework.beans.factory.ObjectProvider<T> objectProvider)
                                 throws org.springframework.beans.BeansException
        Retrieve the object managed by the given ObjectProvider if it is unique, without throwing an exception in case of no such bean being present or multiple candidates found.

        This method is useful when you want to safely obtain a unique bean instance and handle the absence or ambiguity of it gracefully.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         MyService service = ObjectProviderUtils.getIfUnique(myServiceProvider);
         if (service != null) {
             service.doSomething();
         } else {
             System.out.println("MyService is either not available or not unique.");
         }
         
        Type Parameters:
        T - the type of the object
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        Returns:
        an instance of the bean if it's uniquely available, or null if not available or not unique
        Throws:
        org.springframework.beans.BeansException - if there is an error creating the bean
        Since:
        Spring Framework 4.3
        See Also:
        ObjectProvider.getIfUnique()
      • getIfUnique

        @Nullable
        public static <T> T getIfUnique​(@Nullable
                                        org.springframework.beans.factory.ObjectProvider<T> objectProvider,
                                        @Nullable
                                        java.util.function.Supplier<T> defaultSupplier)
                                 throws org.springframework.beans.BeansException
        Retrieve the object managed by the given ObjectProvider if it is unique, otherwise obtain the object using the provided default supplier.

        This method is useful when you want to safely obtain a unique bean instance and provide a fallback mechanism in case the bean is either not available or not unique.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         MyService service = ObjectProviderUtils.getIfUnique(myServiceProvider, () -> new DefaultMyService());
         service.doSomething();
         
        Type Parameters:
        T - the type of the object
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        defaultSupplier - a callback for supplying a default object if none is present in the factory or not unique
        Returns:
        an instance of the bean if it's uniquely available, or the supplied default object if no such bean is available or if multiple candidates exist without a primary one
        Throws:
        org.springframework.beans.BeansException - in case of creation errors
        Since:
        Spring Framework 5.0
        See Also:
        getIfUnique(ObjectProvider), ObjectProvider.getIfAvailable(Supplier)
      • ifUnique

        public static <T> void ifUnique​(@Nullable
                                        org.springframework.beans.factory.ObjectProvider<T> objectProvider,
                                        @Nullable
                                        java.util.function.Consumer<T> dependencyConsumer)
                                 throws org.springframework.beans.BeansException
        Consume the unique object instance managed by the given ObjectProvider if available.

        This method safely retrieves the unique object from the provider and passes it to the consumer if both are non-null. It does not throw an exception if no object is available or if multiple candidates exist.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         ObjectProviderUtils.ifUnique(myServiceProvider, service -> {
             service.doSomething();
         });
         
        Parameters:
        objectProvider - the ObjectProvider to retrieve the object from
        dependencyConsumer - a callback for processing the target object if unique (not called otherwise)
        Throws:
        org.springframework.beans.BeansException - in case of creation errors
        Since:
        Spring Framework 5.0
        See Also:
        ObjectProvider.getIfUnique(), ObjectProvider.ifUnique(Consumer)
      • iterator

        @Nonnull
        public static <T> java.util.Iterator<T> iterator​(@Nullable
                                                         org.springframework.beans.factory.ObjectProvider<T> objectProvider)
        Returns an Iterator over all matching object instances managed by the given ObjectProvider, typically in registration order.

        This method is useful when you want to iterate through all available bean instances without using a stream. It provides access to the underlying iterator of the provider.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         Iterator<MyService> serviceIterator = ObjectProviderUtils.iterator(myServiceProvider);
         while (serviceIterator.hasNext()) {
             MyService service = serviceIterator.next();
             service.doSomething();
         }
         
        Type Parameters:
        T - the type of the objects managed by the provider
        Parameters:
        objectProvider - the ObjectProvider to retrieve the iterator from
        Returns:
        an Iterator for iterating through the available objects
        Throws:
        java.lang.UnsupportedOperationException - if the version of Spring Framework is less than 5.1 or if the provider does not support iteration
        Since:
        Spring Framework 5.1
        See Also:
        ObjectProvider.stream()
      • stream

        @Nonnull
        public static <T> java.util.stream.Stream<T> stream​(@Nullable
                                                            org.springframework.beans.factory.ObjectProvider<T> objectProvider)
        Return a sequential Stream over all matching object instances, without specific ordering guarantees (but typically in registration order).

        This method is useful when you want to process all available bean instances in a functional style using streams. It provides a convenient way to access and manipulate the collection of beans managed by the provider.

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         List<MyService> services = ObjectProviderUtils.stream(myServiceProvider)
                                                        .filter(Objects::nonNull)
                                                       .toList();
         services.forEach(service -> {
             service.doSomething();
         });
         
        Type Parameters:
        T - the type of the objects managed by the provider
        Parameters:
        objectProvider - the ObjectProvider to retrieve the stream from
        Returns:
        a sequential Stream for processing the available objects
        Throws:
        java.lang.UnsupportedOperationException - if the version of Spring Framework is less than 5.1
        Since:
        Spring Framework 5.1
        See Also:
        ObjectProvider.iterator(), ObjectProvider.orderedStream()
      • orderedStream

        @Nonnull
        public static <T> java.util.stream.Stream<T> orderedStream​(@Nullable
                                                                   org.springframework.beans.factory.ObjectProvider<T> objectProvider)
        Return a sequential Stream over all matching object instances, pre-ordered according to the factory's common order comparator.

        In a standard Spring application context, this will be ordered according to the following rules:

        • Beans implementing the Ordered interface will be sorted based on their order value.
        • Beans annotated with Order will be sorted based on the specified order value.
        • If no explicit order is defined, the default ordering will be used (typically registration order).

        Example Usage

        
         ObjectProvider<MyService> myServiceProvider = context.getBeanProvider(MyService.class);
         List<MyService> orderedServices = ObjectProviderUtils.orderedStream(myServiceProvider)
                                                             .filter(Objects::nonNull)
                                                             .toList();
         orderedServices.forEach(service -> {
             service.doSomething();
         });
         
        Type Parameters:
        T - the type of the objects managed by the provider
        Parameters:
        objectProvider - the ObjectProvider to retrieve the stream from
        Returns:
        a sequential Stream for processing the available objects in the correct order
        Throws:
        java.lang.UnsupportedOperationException - if the version of Spring Framework is less than 5.1
        Since:
        Spring Framework 5.1
        See Also:
        ObjectProvider.stream(), OrderComparator