Class DelegatingURLStreamHandlerFactory

  • All Implemented Interfaces:
    java.net.URLStreamHandlerFactory
    Direct Known Subclasses:
    ServiceLoaderURLStreamHandlerFactory

    public class DelegatingURLStreamHandlerFactory
    extends java.lang.Object
    implements java.net.URLStreamHandlerFactory
    A delegating implementation of URLStreamHandlerFactory that forwards all calls to a provided delegate factory.

    This class is useful when you want to wrap or modify the behavior of an existing URLStreamHandlerFactory instance, such as adding custom logic before or after delegation, without directly modifying its implementation.

    Example Usage

    
     // Create a custom URLStreamHandlerFactory
     URLStreamHandlerFactory customFactory = protocol -> {
         if ("http".equals(protocol)) {
             return new MyCustomHttpURLStreamHandler();
         }
         return null;
     };
    
     // Wrap it with DelegatingURLStreamHandlerFactory
     DelegatingURLStreamHandlerFactory delegatingFactory = new DelegatingURLStreamHandlerFactory(customFactory);
    
     // Set the delegating factory as the default
     URL.setURLStreamHandlerFactory(delegatingFactory);
     

    In this example, any request for a URL handler will first go through the DelegatingURLStreamHandlerFactory, which in turn delegates the handling to the wrapped custom factory.

    Since:
    1.0.0
    Author:
    Mercy
    • Constructor Summary

      Constructors 
      Constructor Description
      DelegatingURLStreamHandlerFactory​(java.net.URLStreamHandlerFactory delegate)
      Constructs a new instance with the specified delegate factory.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.net.URLStreamHandler createURLStreamHandler​(java.lang.String protocol)  
      protected java.net.URLStreamHandlerFactory getDelegate()
      Get the delegate of URLStreamHandlerFactory
      • Methods inherited from class java.lang.Object

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

      • DelegatingURLStreamHandlerFactory

        public DelegatingURLStreamHandlerFactory​(java.net.URLStreamHandlerFactory delegate)
        Constructs a new instance with the specified delegate factory.
        Parameters:
        delegate - the delegate factory to which calls will be forwarded, must not be null
        Throws:
        java.lang.IllegalArgumentException - if the provided delegate is null
    • Method Detail

      • createURLStreamHandler

        public java.net.URLStreamHandler createURLStreamHandler​(java.lang.String protocol)
        Specified by:
        createURLStreamHandler in interface java.net.URLStreamHandlerFactory
      • getDelegate

        protected final java.net.URLStreamHandlerFactory getDelegate()
        Get the delegate of URLStreamHandlerFactory
        Returns:
        non-null